Redis tutorial of vi: Sorted Sets data types

  • 2020-05-06 11:59:15
  • OfStack

Overview:

The       Sorted-Sets and Sets types are very similar in that they are collections of strings and do not allow duplicate members to appear in an Set. The main difference between them is that each member of Sorted-Sets has a score associated with it (score), which is how Redis sorts the members of the set from smallest to largest. However, it is important to note that although members of Sorted-Sets must be unique, the score (score) is repeatable.
Adding, deleting, or updating a member in Sorted-Set is a very fast operation with a time complexity of logarithm of the number of members in the collection. Because the members in Sorted-Sets are in an ordered position in the collection, even accessing members in the middle of the collection is still very efficient. In fact, this characteristic of Redis is difficult to achieve in many other types of databases, in other words, it is very difficult to model in other databases to achieve the same efficiency as Redis at this point.

ii. List of related commands:

T

命令原型 时间复杂度 命令描述 返回值
ZADD key score member [score] [member]  O(log(N)) 时间复杂度中的N表示Sorted-Sets中成员的数量。添加参数中指定的所有成员及其分数到指定key的Sorted-Set中,在该命令中我们可以指定多组score/member作为参数。如果在添加时参数中的某一成员已经存在,该命令将更新此成员的分数为新值,同时再将该成员基于新值重新排序。如果键不存在,该命令将为该键创建一个新的Sorted-Sets Value,并将score/member对插入其中。如果该键已经存在,但是与其关联的Value不是Sorted-Sets类型,相关的错误信息将被返回。 本次操作实际插入的成员数量。
ZCARD key  O(1) 获取与该Key相关联的Sorted-Sets中包含的成员数量。 返回Sorted-Sets中的成员数量,如果该Key不存在,返回0。
ZCOUNTkey min max O(log(N)+M)  时间复杂度中的N表示Sorted-Sets中成员的数量,M则表示min和max之间元素的数量。该命令用于获取分数(score)在min和max之间的成员数量。针对min和max参数需要额外说明的是,-inf+inf分别表示Sorted-Sets中分数的最高值和最低值。缺省情况下,min和max表示的范围是闭区间范围,即min <= score <= max内的成员将被返回。然而我们可以通过在min和max的前面添加"("字符来表示开区间,如(min max表示min < score <= max,而(min (max表示min < score < max 分数指定范围内成员的数量。
ZINCRBYkey increment member  O(log(N)) 时间复杂度中的N表示Sorted-Sets中成员的数量。该命令将为指定Key中的指定成员增加指定的分数。如果成员不存在,该命令将添加该成员并假设其初始分数为0,此后再将其分数加上increment。如果Key不存,该命令将创建该Key及其关联的Sorted-Sets,并包含参数指定的成员,其分数为increment参数。如果与该Key关联的不是Sorted-Sets类型,相关的错误信息将被返回。 以字符串形式表示的新分数。
ZRANGEkey start stop [WITHSCORES]  O(log(N)+M) 时间复杂度中的N表示Sorted-Set中成员的数量,M则表示返回的成员数量。该命令返回顺序在参数start和stop指定范围内的成员,这里start和stop参数都是0-based,即0表示第一个成员,-1表示最后一个成员。如果start大于该Sorted-Set中的最大索引值,或start > stop,此时一个空集合将被返回。如果stop大于最大索引值,该命令将返回从start到集合的最后一个成员。如果命令中带有可选参数WITHSCORES选项,该命令在返回的结果中将包含每个成员的分数值,如value1,score1,value2,score2...。   返回索引在start和stop之间的成员列表。
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]  O(log(N)+M) 时间复杂度中的N表示Sorted-Set中成员的数量,M则表示返回的成员数量。该命令将返回分数在min和max之间的所有成员,即满足表达式min <= score <= max的成员,其中返回的成员是按照其分数从低到高的顺序返回,如果成员具有相同的分数,则按成员的字典顺序返回。可选参数LIMIT用于限制返回成员的数量范围。可选参数offset表示从符合条件的第offset个成员开始返回,同时返回count个成员。可选参数WITHSCORES的含义参照ZRANGE中该选项的说明。最后需要说明的是参数中min和max的规则可参照命令ZCOUNT。 返回分数在指定范围内的成员列表。
ZRANK key member  O(log(N))   时间复杂度中的N表示Sorted-Set中成员的数量。Sorted-Set中的成员都是按照分数从低到高的顺序存储,该命令将返回参数中指定成员的位置值,其中0表示第一个成员,它是Sorted-Set中分数最低的成员。 如果该成员存在,则返回它的位置索引值。否则返回nil。
ZREM key member [member ...] O(M log(N)) 时间复杂度中N表示Sorted-Set中成员的数量,M则表示被删除的成员数量。该命令将移除参数中指定的成员,其中不存在的成员将被忽略。如果与该Key关联的Value不是Sorted-Set,相应的错误信息将被返回。 实际被删除的成员数量。
ZREVRANGE key startstop[WITHSCORES]   O(log(N)+M)  时间复杂度中的N表示Sorted-Set中成员的数量,M则表示返回的成员数量。该命令的功能和ZRANGE基本相同,唯一的差别在于该命令是通过反向排序获取指定位置的成员,即从高到低的顺序。如果成员具有相同的分数,则按降序字典顺序排序。 返回指定的成员列表。
ZREVRANKkey member  O(log(N)) 时间复杂度中的N表示Sorted-Set中成员的数量。该命令的功能和ZRANK基本相同,唯一的差别在于该命令获取的索引是从高到低排序后的位置,同样0表示第一个元素,即分数最高的成员。 如果该成员存在,则返回它的位置索引值。否则返回nil。 
ZSCOREkey member O(1) 获取指定Key的指定成员的分数。 如果该成员存在,以字符串的形式返回其分数,否则返回nil。
ZREVRANGEBYSCOREkey max min [WITHSCORES] [LIMIT offset count]  O(log(N)+M)  时间复杂度中的N表示Sorted-Set中成员的数量,M则表示返回的成员数量。该命令除了排序方式是基于从高到低的分数排序之外,其它功能和参数含义均与ZRANGEBYSCORE相同。 返回分数在指定范围内的成员列表。 
ZREMRANGEBYRANKkey start stop  O(log(N)+M) 时间复杂度中的N表示Sorted-Set中成员的数量,M则表示被删除的成员数量。删除索引位置位于start和stop之间的成员,start和stop都是0-based,即0表示分数最低的成员,-1表示最后一个成员,即分数最高的成员。   被删除的成员数量。
ZREMRANGEBYSCOREkey min max  O(log(N)+M) 时间复杂度中的N表示Sorted-Set中成员的数量,M则表示被删除的成员数量。删除分数在min和max之间的所有成员,即满足表达式min <= score <= max的所有成员。对于min和max参数,可以采用开区间的方式表示,具体规则参照ZCOUNT。  被删除的成员数量。

Example of command:

    1. ZADD/ZCARD/ZCOUNT/ZREM/ZINCRBY/ZSCORE/ZRANGE/ZRANK:
   


    # in Shell Start from the command line Redis Client tools.
    /> redis-cli
    # Add a score of 1 A member of.
    redis 127.0.0.1:6379> zadd myzset 1 "one"
    (integer) 1
    # Let's add two fractions 2 and 3 Two members of.
    redis 127.0.0.1:6379> zadd myzset 2 "two" 3 "three"
    (integer) 2
    #0 Is the first member, -1 Represents the last member. WITHSCORES The option indicates that each member and its score are included in the result returned, otherwise only members are returned.
    redis 127.0.0.1:6379> zrange myzset 0 -1 WITHSCORES
    1) "one"
    2) "1"
    3) "two"
    4) "2"
    5) "three"
    6) "3"
    # Get member one in Sorted-Set The position index value in. 0 That's the first position.
    redis 127.0.0.1:6379> zrank myzset one
    (integer) 0
    # Members of the four Does not exist, so returns nil .
    redis 127.0.0.1:6379> zrank myzset four
    (nil)
    # To obtain myzset The number of members in the key.    
    redis 127.0.0.1:6379> zcard myzset
    (integer) 3
    # Return with myzset The associated Sorted-Set Where the fraction satisfies the expression 1 <= score <= 2 The number of members.
    redis 127.0.0.1:6379> zcount myzset 1 2
    (integer) 2
    # Delete members one and two , returns the number of members actually deleted.
    redis 127.0.0.1:6379> zrem myzset one two
    (integer) 2
    # Check to see if the deletion succeeded.
    redis 127.0.0.1:6379> zcard myzset
    (integer) 1
    # Get member three Scores. The return value is in the form of a string.
    redis 127.0.0.1:6379> zscore myzset three
    "3"
    # Due to the members two Has been deleted, so the command returns nil .
    redis 127.0.0.1:6379> zscore myzset two
    (nil)
    # Members will be one Score increase of 2 , and returns the updated score for that member.
    redis 127.0.0.1:6379> zincrby myzset 2 one
    "3"
    # Members will be one Score increase of -1 , and returns the updated score for that member.
    redis 127.0.0.1:6379> zincrby myzset -1 one
    "2"
    # See if the member's score is correct after the update.
    redis 127.0.0.1:6379> zrange myzset 0 -1 WITHSCORES
    1) "one"
    2) "2"
    3) "two"
    4) "2"
    5) "three"
    6) "3"

    2. ZRANGEBYSCORE/ZREMRANGEBYRANK/ZREMRANGEBYSCORE
   

    redis 127.0.0.1:6379> del myzset
    (integer) 1
    redis 127.0.0.1:6379> zadd myzset 1 one 2 two 3 three 4 four
    (integer) 4
    # Get the score that satisfies the expression 1 <= score <= 2 A member of.
    redis 127.0.0.1:6379> zrangebyscore myzset 1 2
    1) "one"
    2) "two"
    # Get the score that satisfies the expression 1 < score <= 2 A member of.
    redis 127.0.0.1:6379> zrangebyscore myzset (1 2
    1) "two"
    #-inf Is the first member, +inf Is the last member, limit The following parameter is used to restrict the return member's self,
    #2 Represents a slave position index (0-based) Is equal to the 2 The members start and go to the back 3 A member.
    redis 127.0.0.1:6379> zrangebyscore myzset -inf +inf limit 2 3
    1) "three"
    2) "four"
    # Delete the fraction that satisfies the expression 1 <= score <= 2 And returns the actual number of deletions.
    redis 127.0.0.1:6379> zremrangebyscore myzset 1 2
    (integer) 2
    # See if the deletion above was successful.
    redis 127.0.0.1:6379> zrange myzset 0 -1
    1) "three"
    2) "four"
    # Drop the position index to satisfy the expression 0 <= rank <= 1 A member of.
    redis 127.0.0.1:6379> zremrangebyrank myzset 0 1
    (integer) 2
    # Check to see if the previous command was deleted successfully.
    redis 127.0.0.1:6379> zcard myzset
    (integer) 0
 

    3. ZREVRANGE/ZREVRANGEBYSCORE/ZREVRANK:
   

    # Prepare the test data for the following examples.
    redis 127.0.0.1:6379> del myzset
    (integer) 0
    redis 127.0.0.1:6379> zadd myzset 1 one 2 two 3 three 4 four
    (integer) 4
    # Retrieves and returns the members in this interval from high to low in the position index.
    redis 127.0.0.1:6379> zrevrange myzset 0 -1 WITHSCORES
    1) "four"
    2) "4"
    3) "three"
    4) "3"
    5) "two"
    6) "2"
    7) "one"
    8) "1"
    # Because it's sort from high to low, the position is equal to 0 is four . 1 is three And so on.
    redis 127.0.0.1:6379> zrevrange myzset 1 3
    1) "three"
    2) "two"
    3) "one"
    # Because it's sort from high to low, so one The position is 3 .
    redis 127.0.0.1:6379> zrevrank myzset one
    (integer) 3
    # Because it's sort from high to low, so four The position is 0 .
    redis 127.0.0.1:6379> zrevrank myzset four
    (integer) 0
    # Get the score that satisfies the expression 3 >= score >= 0 And output in the reverse order, that is, from the height to the bottom.
    redis 127.0.0.1:6379> zrevrangebyscore myzset 3 0
    1) "three"
    2) "two"
    3) "one"
    # This command supports limit Option, which means the same thing zrangebyscore The option is simply computed and retrieved in the reverse order when calculating the position.
    redis 127.0.0.1:6379> zrevrangebyscore myzset 4 0 limit 1 2
    1) "three"
    2) "two"
   

4. Scope of application:

      1). Can be used for a large online game points ranking. Whenever a player's score changes, the ZADD command can be executed to update the player's score, and then the ZRANGE command can be used to obtain the user information of TOP TEN. Of course, we can also use ZRANK command to get the ranking information of players through username. Finally, we will combine the ZRANGE and ZRANK commands to quickly retrieve information about other users who have similar credits to a player.
      2). The Sorted-Sets type can also be used to build index data.


Related articles: