Redis Tutorial 5--redis Sorting

Source: Internet
Author: User
Tags redis tutorial

Redis supports ordering of the 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]

1.SORT Key:

Sort key This is the simplest case, and no option is simply to sort the elements of the collection itself and return the sort results. Here's an example:

Redis 127.0.0.1:6379> Lpush ml 12

(integer) 1

Redis 127.0.0.1:6379> Lpush ml 11

(integer) 2

Redis 127.0.0.1:6379> Lpush ml 23

(integer) 3

Redis 127.0.0.1:6379> Lpush ml 13

(integer) 4

Redis 127.0.0.1:6379> 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 127.0.0.1:6379> Lpush mylist Baidu

(integer) 1

Redis 127.0.0.1:6379> Lpush mylist Hello

(integer) 2

Redis 127.0.0.1:6379> Lpush mylist Xhan

(integer) 3

Redis 127.0.0.1:6379> Lpush mylist Soso

(integer) 4

Redis 127.0.0.1:6379> Sort MyList

1. "Soso"

2. "Xhan"

3. "Hello"

4. "Baidu"

Redis 127.0.0.1:6379> Sort MyList Alpha

1. "Baidu"

2. "Hello"

3. "Soso"

4. "Xhan"

Redis 127.0.0.1:6379> Sort mylist desc Alpha

1. "Xhan"

2. "Soso"

3. "Hello"

4. "Baidu"

3.[by pattern] [GET pattern]:

In addition to the ability to sort by the collection element's own values, you can combine the contents of a collection element into a new key by a given pattern group 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 127.0.0.1:6379> set name11 nihao

ok

redis 127.0.0.1:6379> set name12 wo

ok

redis 127.0.0.1:6379> set name13 shi

ok

redis 127.0.0.1:6379> set Name23 lala

ok

redis 127.0.0.1:6379> sort ml by name*

1. "

2. "All"

3. "One"

4. "A"

* Represents the element value in ml, so this sort is sorted according to the four key corresponding values of Name12 name13 Name23name23, and of course returns the elements in the sorted ML collection.

The above example is the element in the returned ML collection. A get operation can make a new key with the value in list and the number of list lengths obtained by the new key (sort is the value of the get out, not the value in the list), or return (nil) if the new key does not exist.

Redis 127.0.0.1:6379> 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 sorted according to Name12 Name13 Name23name23 values and arranged alphabetically. Additionally, the GET option can have more than one. See Example (#特殊符号引用的是原始集合也就是ml)

Redis 127.0.0.1:6379> 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 refers to the hash type field, and the following is an example

redis 127.0.0.1:6379> hset user1 name Hanjie

(integer) 1

redis 127.0.0.1:6379> hset user11 Name Hanjie

(integer) 1

redis 127.0.0.1:6379> hset user12 name

(integer) 1

redis 127.0.0.1:6379> hset user13 name LXL

(integer) 1

redis 127.0.0.1:6379> sort ml get user*->name

" Hanjie "

" ""

"LXL"

(nil)

Note that when the corresponding user23 does not exist, it returns nil.

4.[LIMIT start Count]:

The above example returns all of the results. The limit option can limit the number of results returned.

Redis 127.0.0.1:6379> sort ml get name* limit 1 2

1. "Wo"

2. "Shi"

The start subscript starts at 0, where the limit option means to get 2 starting from the second element.

5.[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 saved type is list, and the following example saves the sorted result in CL:

Redis 127.0.0.1:6379> sort ml get name* limit 1 2 store CL

(integer) 2

Redis 127.0.0.1:6379> Type cl

List

Redis 127.0.0.1:6379> Lrange cl 0-1

1. "Wo"

2. "Shi"

6. 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. 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 a key that contains {}. Then hash only the contents of {} contained in the key. We named {name}12{name}13 {name}23 {name}23] Four name-related keys so that the client program would put them all on the same server, and Redis's Java client Jedis already implemented that functionality.

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 SortedSet to index a collection that needs to be accessed in a sequential order.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Redis Tutorial 5--redis Sorting

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.