This article describes Redis sort commands
Redis supports ordering of list,set,sorted set elements
Sort ordering Command format:
Sort key [by mode] [LIMIT start count] [get pattern] [asc| DESC] [ALPHA] [STORE Dstkey]
1) sort key (list)
This is the simplest case where there is no option to sort the collection's own elements and return the sort result, which defaults to value ascending.
Example:
Copy Code code as follows:
127.0.0.1:6379> Lpush MIMVP 12
(integer) 1
127.0.0.1:6379> Lpush MIMVP 11
(integer) 2
127.0.0.1:6379> Lpush MIMVP 13
(integer) 3
127.0.0.1:6379> Lpush MIMVP 10
(integer) 4
127.0.0.1:6379> Lrange MIMVP 0-1
1) "10"
2) "13"
3) "11"
4) "12"
127.0.0.1:6379> Sort MIMVP
1) "10"
2) "11"
3) "12"
4) "13"
127.0.0.1:6379> Sort MIMVP desc
1) "13"
2) "12"
3) "11"
4) "10"
2) [asc| DESC] [ALPHA] (list)
Sort default sorting (ASC) is from small to large, and of course it can be sorted in reverse or in character order.
The reverse order can be added with the DESC option, and the alpha option can be added alphabetically, and alpha can be used with Desc.
Sort defaults to fractions (numeric values), and the letters use the default sort sort, which is an error!
Example:
Copy Code code as follows:
127.0.0.1:6379> Lpush mylist Forum
(integer) 1
127.0.0.1:6379> Lpush MyList Proxy
(integer) 2
127.0.0.1:6379> Lpush MyList Blog
(integer) 3
127.0.0.1:6379> Lpush mylist Apptop
(integer) 4
127.0.0.1:6379> Sort MyList
(Error) ERR one or more scores can ' t is converted into double
127.0.0.1:6379> Sort MyList Alpha
1) "Apptop"
2) "Blog"
3) "Forum"
4) "Proxy"
127.0.0.1:6379> Sort MyList Alpha desc
1) "Proxy"
2) "Forum"
3) "Blog"
4) "Apptop"
127.0.0.1:6379> Sort mylist desc Alpha
1) "Proxy"
2) "Forum"
3) "Blog"
4) "Apptop"
3) [by pattern] (set)
In addition to sorting by the set element's own values (numbers, letters), you can also synthesize the element contents of the collection into a new key according to the given pattern group, and sort the corresponding content in the new key.
Example:
Copy Code code as follows:
127.0.0.1:6379> Set Mimvp_12 Mimvp_12
Ok
127.0.0.1:6379> Set Mimvp_11 mimvp_11
Ok
127.0.0.1:6379> Set Mimvp_13 mimvp_13
Ok
127.0.0.1:6379> Set Mimvp_10 mimvp_10
Ok
127.0.0.1:6379> sort MIMVP by mimvp_*//mimvp_* is a string and therefore requires alpha
(Error) ERR one or more scores can ' t is converted into double
127.0.0.1:6379> sort MIMVP by mimvp_* Alpha
1) "10"
2) "11"
3) "12"
4) "13"
127.0.0.1:6379> sort MIMVP by mimvp_* Alpha Desc
1) "13"
2) "12"
3) "11"
4) "10"
* For example 1 above) MIMVP has given the element value, so this sort is sorted by Mimvp_12, mimvp_11,mimvp_13,mimvp_10 these four key corresponding values, but returns the elements in the sorted MIMVP collection, that is, the value 13, 11,12,10, rather than a mimvp_* string element.
4) [Get pattern]
The example above is the numeric element in the returned MIMVP collection, or the Get option to obtain a string value that specifies pattern as the new key (mimvp_*).
Example:
Copy Code code as follows:
127.0.0.1:6379> sort MIMVP by mimvp_* get mimvp_* Alpha
1) "Mimvp_10″
2) "Mimvp_11″
3) "Mimvp_12″
4) "Mimvp_13″
127.0.0.1:6379> sort MIMVP by mimvp_* get mimvp_* Alpha desc
1) "Mimvp_13″
2) "Mimvp_12″
3) "Mimvp_11″
4) "Mimvp_10″
This return is not the elements in the MIMVP, but Mimvp_12, mimvp_11,mimvp_13,mimvp_10 corresponding values, of course, the sort is in accordance with Mimvp_12, MIMVP_11,MIMVP_13,MIMVP_ 10 values and sorted alphabetically.
In addition, get options can have multiple, #特殊符号引用的是原始集合也就是mimvp (similar to Sorted-set withscores)
Example:
Copy Code code as follows:
127.0.0.1:6379> sort MIMVP by mimvp_* get mimvp_* Get # Alpha
1) "Mimvp_10″
2) "10"
3) "Mimvp_11″
4) "11"
5) "Mimvp_12″
6) "12"
7) "Mimvp_13″
8) "13"
127.0.0.1:6379> sort MIMVP by mimvp_* get mimvp_* get # Alpha Desc
1) "Mimvp_13″
2) "13"
3) "Mimvp_12″
4) "12"
5) "Mimvp_11″
6) "11"
7) "Mimvp_10″
8) "10"
And finally, there is a special character-> (hash) that references the hash type field.
Example:
Copy Code code as follows:
127.0.0.1:6379> hset User_12 name Yanggang
(integer) 1
127.0.0.1:6379> hset user_11 name Yangjie
(integer) 1
127.0.0.1:6379> hset user_13 name Yangliang
(integer) 1
127.0.0.1:6379> hset user_10 name Yangchuang
(integer) 1
127.0.0.1:6379> sort MIMVP Get User_*->name
1) "Yangchuang"
2) "Yangjie"
3) "Yanggang"
4) "Yangliang"
127.0.0.1:6379> sort MIMVP Get user_*->name desc
1) "Yangliang"
2) "Yanggang"
3) "Yangjie"
4) "Yangchuang"
It is easy to understand that if MIMVP when the corresponding value does not exist, then the corresponding return is nil
5) [LIMIT Start Count] (LIMIT)
The example above returns all the elements, and the limit option can limit the number of returned results.
Example:
Copy Code code as follows:
127.0.0.1:6379> sort MIMVP Get mimvp_* limit 1 2
1) "Mimvp_11″
2) "Mimvp_12″
127.0.0.1:6379> sort MIMVP Get mimvp_* limit 0 3
1) "Mimvp_10″
2) "Mimvp_11″
3) "Mimvp_12″
The start subscript is starting at 0, where the limit option (limit 1 2) means getting 2 from the second element.
6) [Store Dstkey] (store)
If the collection is often sorted according to a fixed pattern, then caching the sort results will reduce CPU overhead, and using the store option you can save the sorted content to the specified key with the type list
Copy Code code as follows:
127.0.0.1:6379> sort MIMVP Get mimvp_* limit 0 3 store Mimvp_store
(integer) 3
127.0.0.1:6379> type Mimvp_store
List
127.0.0.1:6379> Lrange Mimvp_store 0-1
1) "Mimvp_10″
2) "Mimvp_11″
3) "Mimvp_12″
This example saves the sorting result to Mimvp_store, which can be extracted directly from the Mimvp_store after use.
Summary
After the function is introduced, then discuss some questions about sorting.
If there are multiple REDIS servers, different keys may exist on different servers. For example, Mimvp_12, mimvp_11,mimvp_13,mimvp_10, are likely to be stored on four different servers, which can have a significant effect on sorting performance.
The Redis author mentions the solution to this problem on his blog, that is, key tag will put the key that needs to be sorted on the same server. Because the specific decision which key exists which server is generally on the client side hash method to do, we can by only the part of the key to hash
As an example:
If our client discovers that the key contains [], then only hash the contents of the key [], we will name the four name-related key, named [NAME]12 [name]13] [name]11 [name]10], so the client The program will put them all on the same server (I don't know if Jredis is implemented).
There's one more serious problem.
Sorting can take a long time if the collection is very large. Because Redis is single-threaded, a lengthy sort operation blocks other client requests. The solution is to replicate the data to multiple slave through the master-slave replication mechanism. Then we only do sort operations on the slave and cache the sort results as much as possible. Another option is to use the sorted set to index a collection that needs to be accessed in one order.
Example:
Copy Code code as follows:
127.0.0.1:6379> Sadd tom:friend:list 123 # Tom's buddy list is a buddy's UID.
(integer) 1
127.0.0.1:6379> Sadd Tom:friend:list 456
(integer) 1
127.0.0.1:6379> Sadd tom:friend:list 789
(integer) 1
127.0.0.1:6379> Sadd Tom:friend:list 101
(integer) 1
127.0.0.1:6379> set uid:sort:123 1000 # UID corresponding results
Ok
127.0.0.1:6379> Set UID:SORT:456 6000
Ok
127.0.0.1:6379> Set uid:sort:789 100
Ok
127.0.0.1:6379> Set uid:sort:101 5999
Ok
127.0.0.1:6379> set uid:123 "{' UID ': 123, ' name ': ' Lucy '}" # Add UID to friend information
Ok
127.0.0.1:6379> set uid:456 "{' uid ': 456, ' name ': ' Jack '}"
Ok
127.0.0.1:6379> set uid:789 "{' uid ': 789, ' name ': ' Marry '}"
Ok
127.0.0.1:6379> set uid:101 "{' uid ':", ' name ': ' Icej '} "
Ok
127.0.0.1:6379> sort tom:friend:list by uid:sort:* get uid:* # Gets the ID from the buddy list that matches the Uid:sort field, and then gets the information in the UID table based on the order in which they are sorted.
1 "{' UID ': 789, ' name ': ' Marry '}"
2 "{' UID ': 123, ' name ': ' Lucy '}"
3 "{' UID ':", "name": ' Icej '} "
4 "{' UID ': 456, ' name ': ' Jack '}"
127.0.0.1:6379> 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 ':", "name": ' Icej '} "
6) "5999"
7 "{' UID ': 456, ' name ': ' Jack '}"
8) "6000"
Problems and analysis
1. Sort MIMVP by mimvp_* gets mimvp_* Get # alpha is sorted in 10–13 order, unlike the simple sort name* and name * Alpha results
This problem should be analyzed from the logic of Redis's realization.
A list after insertion, the default is sorted by time sequence (advanced, FILO), Lrange ml 0-1, the result is: 12 13 11 10. This is because the list inserts the latest item into the list header
b the Sort MIMVP by mimvp_* determines that they are sorted according to the mimvp_* value. But when the mimvp_* corresponding value is not a num and does not set alpha, the sorting score is the same because the program will put the mimvp_* The corresponding value attempts to convert to the nun type
c) This will cause the sort MIMVP by mimvp_* to be arranged in the natural order of ML
Copy Code code as follows:
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)
* FAR. We can just cast it
*/
Vector[j].u.score = (long) byval->ptr;
}
else {
Redisassert (1!= 1);
}
}