本文内容为 http://redis.io/commands/zrangebylex 的翻译、注解、例子扩充及其它修改。感谢 Redis 作者 Antirez 为开源社区作出的贡献,本文保证最新、最准、最全以表示对其的敬意。欢迎留言纠错、提示更新或支持。
ZRANGEBYLEX key min max [LIMIT offset count]

当插入到有序集合(Sorted set)中的所有元素的分值(Score)都相同时,使用当前命令可以认为存储在键 key 中的有序集合中的元素是按字典序排序(Lexicographical ordering)的,然后返回值在最小值 min 及最大值 max 之间的所有元素。

When all the elements in a sorted set are inserted with the same score, in order to force lexicographical ordering, this command returns all the elements in the sorted set at key with a value between min and max.

如果有序集合中的元素存在不同的分值,所返回的元素将不确定。

If the elements in the sorted set have different scores, the returned elements are unspecified.

所有元素由这样一种方式确定排序:通过 C 函数 memcmp 逐字节(Byte-by-byte)比较,确定从小到大的字符串排序。如果共有部分完全相同,则长字符串被认为比短字符串来大。

The elements are considered to be ordered from lower to higher strings as compared byte-by-byte using the memcmp() C function. Longer strings are considered greater than shorter strings if the common part is identical.

可选参数 LIMIT 可以用来获取所匹配元素的某个范围(类似于 SQL 语法中的 SELECT LIMIT offset,count 语句)。必须要注意的是,如果偏移 offset 很大,在获得所需返回元素之前,需要遍历有序集合以定位偏移 offset 处的元素,时间复杂度总计为 O(N)

The optional LIMIT argument can be used to only get a range of the matching elements (similar to SELECT LIMIT offset, count in SQL). Keep in mind that if offset is large, the sorted set needs to be traversed for offset elements before getting to the elements to return, which can add up to O(N) time complexity.
  • 指定分值范围中的元素列表。

    List of elements in the specified score range.
  • redis> ZADD myzset 0 a 0 b 0 c 0 d 0 e 0 f 0 g
    (integer) 7
    redis> ZRANGEBYLEX myzset - [c
    1) "a"
    2) "b"
    3) "c"
    redis> ZRANGEBYLEX myzset - (c
    1) "a"
    2) "b"
    redis> ZRANGEBYLEX myzset [aaa (g
    1) "b"
    2) "c"
    3) "d"
    4) "e"
    5) "f"
  • How to specify intervals

    Valid start and stop must start with ( or [, in order to specify if the range item is respectively exclusive or inclusive. The special values of + or - for start and stop have the special meaning or positively infinite and negatively infinite strings, so for instance the command ZRANGEBYLEX myzset - + is guaranteed to return all the elements in the sorted set, if all the elements have the same score.

  • Details on strings comparison

    Strings are compared as binary array of bytes. Because of how the ASCII character set is specified, this means that usually this also have the effect of comparing normal ASCII characters in an obvious dictionary way. However this is not true if non plain ASCII strings are used (for example utf8 strings).

    However the user can apply a transformation to the encoded string so that the first part of the element inserted in the sorted set will compare as the user requires for the specific application. For example if I want to add strings that will be compared in a case-insensitive way, but I still want to retrieve the real case when querying, I can add strings in the following way:

    ZADD autocomplete 0 foo:Foo 0 bar:BAR 0 zap:zap

    Because of the first normalized part in every element (before the colon character), we are forcing a given comparison, however after the range is queries using ZRANGEBYLEX the application can display to the user the second part of the string, after the colon.

    The binary nature of the comparison allows to use sorted sets as a general purpose index, for example the first part of the element can be a 64 bit big endian number: since big endian numbers have the most significant bytes in the initial positions, the binary comparison will match the numerical comparison of the numbers. This can be used in order to implement range queries on 64 bit values. As in the example below, after the first 8 bytes we can store the value of the element we are actually indexing.

  • 版本支持

    2.8.9+

    时间复杂度(Time complexity)

    O(log(N)+M)N 为有序集合中元素的个数,M 为返回的元素个数。

    O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned. If M is constant (e.g. always asking for the first 10 elements with LIMIT), you can consider it O(log(N)).