(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. "All"
(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
redis> sort ml by name*
1. "13"
2. "23"
3. "11"
4. "
A"
* 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. "
a"
Finally there is a special character-> 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 in CL
After the function is introduced, some questions about sorting are discussed. 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 workaround is to replicate the data to multiple slave through a master-slave replication mechanism . 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.
- After the list is inserted, the default is in reverse chronological order of time, Lrange ml 0-1, the result is: 13 23 11 12. This is because the list is inserted when the latest item is inserted into the list header
- The sort M1 by name* determines that it will be sorted according to the value of name*. But when name* corresponding value is not a num type and no alpha is set, the sorting score is the same because the program will attempt to convert the name* corresponding value to the nun type
- This causes the sort ml by name* to be arranged in the natural order of the ML
1234567891011121314 |
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 {
redisAssert(1 != 1);
}
}
|
Redis Data Summary (iii) sorting