Redis命令-有序集合-zrangebylex

原文

http://redis.io/commands/zrangebylex

简介

Return a range of members in a sorted set, by lexicographical range.

根据词典范围,返回有序集合中一定范围内的成员。

语法

ZRANGEBYLEX key min max [LIMIT offset count]

版本

Available since 2.8.9.

自2.8.9版本可用。

时间复杂度

Time complexity: 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)).

O(log(N)+M):N是有序集合中元素的数量,M是返回的元素的数量。如果M是常量,你可以认为时间复杂度为O(log(N))。

描述

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.

当有序集合中插入的所有元素的分数都相同时,为了强制词典排序,这个命令返回值在min和max之间的所有元素。

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

如果有序集合中元素有不同的分数,返回的元素是未指定的。

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.

这些元素被认为是从更低到更高字符串排序,通过memcmp()函数按字节比较。如果公共部分是一样的,更长的字符串被认为大于更短的字符串。

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.

LIMIT参数可以被用于仅仅获取一定范围内匹配的元素(类似于SQL语句SELECT LIMIT offset, count)。请牢记,如果offset比较大,在获取返回的元素之前,有序集合需要穿过offset个元素,时间复杂度总计为O(N)。

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.

如果需要指定范围边界是排除在外或包含在内,有效的start和stop必须以(或[开始。如果start和stop指定为+或-,表示正无穷大或负无穷大字符串,例如ZRANGEBYLEX myzset - +,如果所有元素有相同的分数,可以保证返回有序集合中所有元素。

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.

返回值

Array reply: list of elements in the specified score range.

Array:指定范围的元素列表。

例子

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"
redis>

猜你喜欢

转载自gitzhangyl.iteye.com/blog/2290611