# Iii. Sorting of redis Study Notes

Source: Internet
Author: User
After learning about various redis types, this article introduces the redis sorting command. redis supports sorting list, set, and sorted set elements. The complete command format of the sort command is as follows:
Sort key [by pattern] [limit start count] [get pattern] [ASC | DESC] [Alpha] [store dstkey]
The following describes the various command options.
(1) Sort key
This is the simplest case. No option is to simply sort the elements of the set and return the sorting result. The following is 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]
The default sort sorting method (ASC) is from small to large. Of course, it can also be sorted in reverse or character order. The DESC option can be added in reverse order, and the Alpha option can be added in alphabetical order. Of course, Alpha can be used with DESC. The following is an example of sorting in alphabetical order.
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 sorting by the value of the Set element, you can also combine the content of the Set element into a new key based on the given pattern and sort the content corresponding to the new key. The following example uses the ml set in the first example for 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. "12"
* Indicates the element values in ml. Therefore, the sorting is based on the values corresponding to the four keys name12 name13 name23 name23. Of course, the elements in the sorted ml set are returned.

(4) [get pattern]
The above examples are all elements in the returned ml set. We can also use the get option to obtain the value of the specified pattern as the new key. Let's look at a combination of examples.
Redis> sort mL by name * get name * alpha
1. "Lala"
2. "nihao"
3. "Shi"
4. "wo"
The returned result is not an element in ml, but a value corresponding to name12 name13 name23 name23. Of course, the sorting is based on the name12 name13 name23 name23 value and in alphabetical order. In addition, there can be multiple get options. Let's look at the example (# The special symbol references the original set, that is, 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 that references the hash type field->. 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 nil is returned when the corresponding user23 does not exist.

(5) [limit start count]

The results returned in the preceding example are all. The limit option can limit the number of returned results. Example

Redis> sort ml get name * limit 1 2
1. "wo"
2. "Shi"

The START subscript starts from 0. Here, the limit option means to get 2 from the second element.

(6) [store dstkey]
If the set is frequently sorted in a fixed mode, caching the sorting results reduces CPU overhead. You can use the store option to save the sorting content to the specified key. The storage type 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 result to Cl.

after the function introduction, we will discuss some sorting issues. If we have multiple redis servers, different keys may exist on different servers. For example, name12 name13 name23 name23 may be stored on four different servers. This will have a great impact on the sorting performance. The author of redis mentioned the solution to this problem on his blog, that is, to put all the keys to be sorted on the same server through the key tag. It is generally implemented by means of client-side hash to determine which key exists on which server. We can hash the key only. For example, if our client finds that the key contains []. Then, only the [] Contents in the key are hashed. We name the keys related to four names in this way [name] 12 [name] 13 [name] 23 [name] 23, therefore, the client Program puts all of them on the same server. I don't know if jredis has been implemented.
another problem is serious. If the sort set is large, sorting takes a long time. Because redis is single-threaded, long sorting operations will block requests from other clients. The solution is to copy data to multiple slave instances through the master-slave replication mechanism. Then we only perform sorting on slave. Cache the sorting result. Another solution is to use sorted set to create an index for the set that needs to be accessed in a certain order.

Related Keywords:
Related Article

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.