Three. Redis sort

Source: Internet
Author: User

This article describes the following Redis sort commands. Redis supports ordering of List,set and sorted set elements. The sort command is the sort complete command format as follows:
SORT key [by pattern] [LIMIT start count] [GET pattern] [asc| DESC] [ALPHA] [STORE Dstkey]
Here's a description of the various command options
(1) sort key
This is the simplest case, and there is no option to simply sort the elements of the collection itself and return the sort results. Here's an example.

redis> Lpush ml 12
(integer) 1
redis> Lpush ml 11
(integer) 2
redis> Lpush ml 23
(integer) 3
redis> Lpush ml 13
(integer) 4
redis> sort ml
1. "11"
2. "12"
3. "13"
4. "23"

(2) [asc| DESC] [ALPHA]
Sort By default (ASC) is small to large, and of course it can be sorted in reverse or in alphabetical order. Reverse order can be added with the DESC option, you want to alphabetical row can be added alpha option, of course, alpha can be used with Desc. Here's an alphabetical example.

Redis> Lpush mylist Baidu
(integer) 1
redis> Lpush mylist Hello
(integer) 2
redis> Lpush mylist Xhan
(integer) 3
redis> Lpush mylist Soso
(integer) 4
Redis> Sort MyList
1. "Soso"
2. "Xhan"
3. "Hello"
4. "Baidu"
redis> Sort MyList Alpha
1. "Baidu"
2. "Hello"
3. "Soso"
4. "Xhan"
redis> Sort mylist desc Alpha
1. "Xhan"
2. "Soso"
3. "Hello"
4. "Baidu"
(3) [by pattern]
In addition to being able to sort by the collection element's own values, you can combine the contents of the collection elements with a given pattern group to synthesize a new key and sort by the corresponding content in the new key. The following example then uses the ML collection in the first example to do a demonstration:
Redis> Set Name11 Nihao
Ok
redis> set Name12 wo
Ok
redis> set Name13 Shi
Ok
Redis> Set Name23 lala
Ok

1. "13"
2. "23"
3. "11"
4. "12"
* Represents the element value in ml, so this sort is sorted according to the four key corresponding values of Name12 name13 Name23 Name23, and of course the returned elements in the sorted ML collection
(Questions about sorting results can be found at the bottom of the FAQ)
(4) [GET pattern]
The above example is the element in the returned ML collection. We can also get the value of the specified pattern corresponding to the new key with the Get option. Look at a combination of examples
redis> sort ml by name* get name*  Alpha
1. "Lala"
2. "Nihao"
3. "Shi"
4. "Wo"
This time the return is not the element in ML, but the name12 name13 name23 name23 corresponding value. Of course the sort is name23 name23 values according to Name12 Name13 and sorted alphabetically. Additionally, the GET option can have more than one. See Example (#特殊符号引用的是原始集合也就是ml)
redis> sort ml by name* get name* get #  Alpha
1. "Lala"
2. "23"
3. "Nihao"
4. "11"
5. "Shi"
6. "13"
7. "Wo"
8. "12"
Finally there is a special character-&gt that refers to the hash type field, and the following is an example
redis> Hset user1 name Hanjie
(integer) 1
redis> hset user11 Name Hanjie
(integer) 1
redis> Hset user12 Name 86
(integer) 1
redis> hset user13 name LXL
(integer) 1
redis> sort ml get user*->name
1. "Hanjie"
2. "86"
3. "LXL"
4. (nil)
It is easy to understand that when the corresponding user23 does not exist, it returns nil.

(5) [LIMIT start Count]

The above example returns all of the results. The limit option can limit the number of results returned. Example
redis> sort ml get name* limit 1 2
1. "Wo"
2. "Shi"
Start subscript is starting from 0, where the limit option means to get 2 starting from the second element

(6) [STORE Dstkey]
If the collection is often sorted in a fixed pattern, caching the sorting results can reduce CPU overhead. Use the store option to save the sorted content to the specified key. The type of Save is list
redis> sort ml get name* limit 1 2 store CL
(integer) 2
Redis> Type CL
List
Redis> Lrange CL 0-1
1. "Wo"
2. "Shi"
In this example, we save the sorting results to CL and then discuss some questions about sorting. If we have multiple REDIS servers, different keys may exist on different servers. Name12 name13 Name23 Name23, for example, are likely to be stored on four separate servers. This situation can have a significant impact on sorting performance. The Redis author mentions on his blog that the solution to this problem is to put the key that needs to be sorted on the same server by key tag. Because of the specific decision which key exists on which server is generally on the client side hash method to do. We can hash only the part of the key. For example, if our client finds that the key contains []. Then hash only the contents of [] contained in key. We named the four name-related keys [name]12 [name]13] [name]23 [name]23], so the client program put them all on the same server. I don't know if Jredis realized it.
There is also a more serious problem. Sorting can take a long time if the set of sort is very large. Because Redis is single-threaded, a lengthy sort operation can block requests from other clients. The solution is through a master-slave copy mechanism willThe data is copied to multiple slave. Then we only do a sort operation on the slave. The possibility of a sort result cache. Another option is to use the sorted set to index the set that needs to be accessed in a sequential order. Example:
Redis> Sadd tom:friend:list 123 #tom的好友列表 a friend's uid.
1
Redis> Sadd Tom:friend:list 456
1
Redis> Sadd tom:friend:list 789
1
Redis> Sadd Tom:friend:list 101
1
Redis> Set uid:sort:123 #uid对应的成绩
Ok
Redis> Set UID:SORT:456 6000
Ok
Redis> Set uid:sort:789 100
Ok
Redis> Set uid:sort:101 5999
Ok
Redis> set uid:123 "{' UID ': 123, ' name ': ' Lucy '}" #增加uid对应好友信息
Ok
Redis> set uid:456 "{' uid ': 456, ' name ': ' Jack '}"
Ok
Redis> set uid:789 "{' uid ': 789, ' name ': ' Marry '}"
Ok
Redis> set uid:101 "{' UID ': 101, ' name ': ' Icej '}"
Ok
Redis> sort tom:friend:list by uid:sort:* get uid:* #从好友列表中获得id与uid: Sort field matches after sorting, and based on the order of sorting, use key to obtain information in the UID table
1. {' UID ': 789, ' name ': ' Marry '}
2. {' UID ': 123, ' name ': ' Lucy '}
3. {' UID ': 101, ' name ': ' Icej '}
4. {' UID ': 456, ' name ': ' Jack '}
Redis> sort tom:friend:list by uid:sort:* get uid:* get uid:sort:*
1. {' UID ': 789, ' name ': ' Marry '}
2.100
3. {' UID ': 123, ' name ': ' Lucy '}
4.1000
5. {' UID ': 101, ' name ': ' Icej '}
6.5999
7. {' UID ': 456, ' name ': ' Jack '}
8.6000
FAQ: 1.sort ml by name* get name* Get # Why does it come down in the order of Shi Lala Nihao wo, which is different from the simple sort name* and name * Alpha results ?This problem is analyzed from the implementation logic of Redis .A) list after insertion, the default is in chronological order, Lrange ml 0-1, the result is: 13 23 11 12. This is because the list is inserted with the latest item inserted into the list header B) Sort M1 by name* determines that it will be sorted according to the value of name*. But when the name* value is not a num type and no alpha is set, it causes the sort score to be the same. Because the program will attempt to convert the value of name* to nun C) This causes the sort ml by name* to be arranged in the natural order of ML.
if (Alpha) {
if (sortby) Vector[j].u.cmpobj = Getdecodedobject (ByVal);
} else {
if (byval->encoding = = Redis_encoding_raw) {
Vector[j].u.score = strtod (byval->ptr,null);
} else if (byval->encoding = = Redis_encoding_int) {
/* Don ' t need to decode the object if it ' s
* Integer-encoded (the only encoding supported) so
* FAR. We can just cast it */
Vector[j].u.score = (long) byval->ptr;
} else {

}
}

Reference: http://www.cnblogs.com/xhan/archive/2011/02/03/1948970.htmlhttp://icej.javaeye.com/blog/517795

Three. Redis sort

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.