Redis Detailed: Sorted sets data types and operations

Source: Internet
Author: User

Sorted set is an upgraded version of Set, which adds a sequential attribute to the set, which can be specified when adding a modified element, and Zset automatically re-adjusts the order of the new values after each assignment. It can be understood that there are two columns of MySQL table, one column in value, and one in the order of storage. Key in operation is understood as the name of Zset.

Series Articles:

  Redis Detailed: Strings data types and operations

  Redis Detailed: Hashes data types and operations

  Redis Detailed: Lists data types and operations

  Redis Detailed: Sets data types and operations

As with set sorted set is also a collection of elements of type string, but each element is associated with a double type of score. The implementation of the sorted set is a mixture of skip list and hash table.

When an element is added to the collection, an element-to-score mapping is added to the hash table, so the cost of getting score for a given element is O (1), and the other score-to-element mapping is added to the skip list and sorted by score, So you can get the elements in the collection in an orderly fashion. Add, the cost of the delete operation is the same as the cost of O (log (N)) and skip list, and the Redis skip list implementation uses a doubly linked list so that the elements can be taken from the tail in reverse order. The most common use of sorted set is to use it as an index. We can store the fields to be sorted as score, the object's ID when the element is stored. Here is the sorted set related command

1, Zadd

add element Member,score to the Zset name key for sorting. If the element already exists, the order of the element is updated according to score

Redis127.0.0.1:6379>Zadd Myzset1"One"
(integer)1
Redis127.0.0.1:6379>Zadd Myzset2"Both"
(integer) 1
Redis 127.0.0.1:6379 > zadd myzset 3  "both"
(integer)  0
Redis 127.0.0.1:6379> zrange Myzset 0 -1  withscores
1)   "one"
2 )   "1"
3)   ""
4)   "3"
Redis 127.0.0.1:6379>

In this example we have added one and two to Myzset, and both are set 2 times, then the last setting will prevail, and finally we will display all the elements and show the score of the elements.

 2, Zrem

Delete the element in the Zset named Key member

Redis127.0.0.1:6379>Zrange Myzset0-1Withscores
1)"One"
2) "1"
3) "both"
4) "3"
Redis 127.0.0.1:6379> zrem myzset
(integer) 1
Redis 127.0.0.1:6379> zrange myzset 0 -1 withscores
1) "one"
2) "1"
Redis 127.0.0.1:6379>

You can see that both are deleted.

3, Zincrby

If the element member is already present in the Zset with the name key, the score of the element increases increment; otherwise, the element is added to the collection and its score value is increment

Redis127.0.0.1:6379>Zadd Myzset21"One"
(integer)1
Redis127.0.0.1:6379>Zadd Myzset22  "both"
(integer)  1
Redis 127.0.0.1:6379>  Zincrby myzset2 2  "One"
"3 "
Redis 127.0.0.1:6379>  zrange myzset2 0 -1< Span class= "Apple-converted-space" > withscores
1)   "2"
Span class= "Apple-converted-space" >  "2"
3)   "one"
4)   "3"
Redis 127.0.0.1:6379>

This example adds one's score from 1 to 2, to 3.

  4, Zrank

Returns the rank of the member element in the Zset named key (sorted by score from small to large) that is subscript

Redis127.0.0.1:6379>Zrange Myzset30 -1 withscores
1) "one"
2) "1"
3) "both"
4) "2"
5) "three"
6) "3"
7) "Five"
8) "5"
Redis 127.0.0.1:6379> zrank myzset3
(integer) 1
Redis 127.0.0.1:6379>

In this case, the subscript is 1, and I'm taking the subscript instead of the score.

 5, Zrevrank

Returns the rank of the member element in the Zset with the name key (sort by score from large to small) that is subscript

Redis127.0.0.1:6379>Zrange Myzset30 -1 withscores
1) "one"
2) "1"
3) "both"
4) "2"
5) "three"
6) "3"
7) "Five"
8) "5"
Redis 127.0.0.1:6379> zrank myzset3
(integer) 1
Redis 127.0.0.1:6379>

In order from big to small, two is the third element and the subscript is 2.

 6, Zrevrange

Returns all elements of index from start to end in Zset (sorted by score from large to small) with the name key

Redis127.0.0.1:6379> zrevrange myzset3 0 -1 withscores
1) "Five"
2) "5"
3) "three"
4) "3"
5) "both"
6) "2"
7) "one"
8) "1"
Redis 127.0.0.1:6379>

First sort by score from big to small, then take out all the elements

  7, Zrangebyscore

Returns the elements of a set in a given interval score

Redis127.0.0.1:6379>Zrange Myzset30-1Withscores
1)"One"
2)"1"
3)  "both"
4)   "2"
5)   "three"
6)   "3"
7)   "five"
8)   "5"
Redis 127.0.0.1:6379> zrangebyscore myzset3< Span class= "Apple-converted-space" > 2 3  withscores
1)   "2"
)   "2"
3)   "three"
4) Span class= "Apple-converted-space" >  "3"
Redis 127.0.0.1:6379

In this example, the elements of score are returned in the range of

8, Zcount

Returns the number of score in a set in a given interval

Redis127.0.0.1:6379>Zrange Myzset30 -1 withscores
1)   "one"
2)   "1"
3)  " ""
4)   "2"
5)  " three "
6)  " 3 "
7)  " five "
8)  " 5 "
Redis 127.0.0.1:6379> zcount Myzset3 2 3
(integer)  2
Redis&NBSP;127.0.0.1:6379>

In this example, the number of elements between the score is calculated

  9, Zcard

Returns the number of elements in the collection

Redis127.0.0.1:6379> zrange myzset3 0 -1 withscores
1)  " one "
2)  " 1 "
3)  " ""
4)   "2"
5)  " three "
6)  " 3 "
7)  " five "
8)  " 5 "
Redis 127.0.0.1:6379> zcard myzset3
( Integer)  4
Redis  127.0.0.1:6379>

From this example, we see that the number of elements in this set of Myzset3 is 4.

10, Zscore

Returns the score corresponding to the given element

Redis127.0.0.1:6379>Zrange myzset3 0 -1 withscores
1) "one"
2) "1"
3) "both"
4) "2"
5) "three"
6) "3"
7) "Five"
8) "5"
Redis 127.0.0.1:6379> zscore myzset3
"2"
Redis 127.0.0.1:6379>

In this example we succeeded in taking out the score of the other.

  11, Zremrangebyrank

Deletes an element in a collection that is ranked in a given interval

Redis127.0.0.1:6379>Zrange Myzset30-1Withscores
1)"One"
2)"1"
3)"Both"
4)"2"
5)"Three"
6)"3"
7)"Five"
8)"5"
Redis127.0.0.1:6379> zremrangebyrank myzset3 3< Span class= "Apple-converted-space" > 3
(integer)  1
redis< Span class= "Apple-converted-space" > 127.0.0.1:6379> zrange Myzset3 0 -1 withscores
1)  " one "
2)  " 1 "
3)  " both "
4)  " 2 "
5)  " three "
6)  " 3 "
Redis&NBSP;127.0.0.1:6379>

In this example, we removed the element with subscript 3 As the result of a small to large order in Myzset3.

  12, Zremrangebyscore

Deletes an element in a collection that score at a given interval

Redis127.0.0.1:6379>Zrange Myzset30-1Withscores
1)"One"
2)"1"
3)"Both"
4)  "2"
5)   "three"
6) Span class= "Apple-converted-space" >  "3"
Redis 127.0.0.1:6379 > zremrangebyscore myzset3   1 2
(integer)  2
Redis 127.0.0.1:6379>  Zrange myzset3 0 -1 withscores
1)   "three"
2)   "3"
Redis 127.0.0.1:6379>

In this example, we have removed the element from the Myzset3 in the order of score from small to large.

Redis Detailed: Sorted sets data types and operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.