Redis Senior sort of sort

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/lianghecai52171314/article/details/102771652

Redis sort Senior

sort sort syntax:

SORT key [BY pattern] [LIMIT offset count] [GET pattern [GET pattern ...]] 
[ASC | DESC] [ALPHA] [STORE destination]

To return or save the given list, collection, sorting through the elements of an ordered set.

  • [BY pattern]: sort order sort default collection element, by "BY pattern" key using external data ordering as a weight.
  • [LIMIT offset count]: Returns the number after the sorting element may be limited by LIMIT modifiers, modifier accepted offset (the number of elements to be skipped, i.e., the starting position) and a count (number of elements) return two parameters.
  • [GET pattern [GET pattern ...]]: get ordering may be taken based on the result of the corresponding key, "get #" represents a return to the element itself, "get pattern" can return the data to the external key.
  • [ASC | DESC] [ALPHA]: select the order, or reverse ordered string, SET collection (itself without an index value) must be specified sorting operations ALPHA.
  • [STORE destination]: By default, sort sorting operation simply returns a result, the save operation does not perform any. By giving store option to specify a key parameter, to sort the results can be saved on a given key.

sort Redis is one of the most powerful and most complex command, if not very easy to become a performance bottleneck. SORT command time complexity is O (n + mlogm), where n denotes the list to be sorted (ordered set or set) the number of elements, m represents a number of elements to be returned. When n relatively low performance large sort command, and creates Redis before ordering a length of n (An exception is when the key type is an ordered set and the reference key is the key name constants m and a size of the container It is not n) to store the containers to be sorted elements, although a temporary process, but if large amounts of data more sorting operation while it will seriously affect the performance.

Here Insert Picture Description
Note the following when used in the development SORT command.
(1) reducing the number of elements to be as sort key (the n as small as possible).
(2) using only the data acquired LIMIT parameters required (as small as possible to make m).
(3) If the number of data to be sorted is large, using as parameter the result STORE Cache.

Example 1, Common ascending descending order

  • Sort key sorted in ascending key
  • sort key desc descending order according to key
    Here Insert Picture Description

Example 2, using the ALPHA modifiers sort strings

default sort sort data, if the value is a string, then the need to increase alpha key behind the parameters, or error.
Here Insert Picture Description

Example 3, using the restriction limit return result

limit can accept two parameters:

  • offset specifies the number of elements to be skipped
  • After the skip count specified offset a specified element, how many objects to be returned
    Here Insert Picture Description

Example 4, using an external sort key

External data key may be used as weights, instead of a direct comparison of the default sort key.
data
Here Insert Picture Description

Numbering Full name age
1 zhangsan 18
2 lysis 17
3 wanger 19
4 mazi 20

4.1 Use BY parameter

BY 参数的语法为“BY参考键”。其中参考键可以是字符串类型键或者是散列类型键的某个字段(表示为键名—>字段名)。如果提供了BY参数,SORT命令将不再依据元素自身的值进行排序,而是对每个元素使用元素的值替换参考键中的第一个“*”并获取其值,然后依据该值对元素排序。
Here Insert Picture Description

4.2、使用GET参数

GET参数不影响排序,它的作用是使SORT命令的返回结果不再是元素自身的值,而是GET参数中指定的键值。GET参数的规则和BY参数一样,GET参数也支持字符串类型和散列类型的键,并使用“*”作为占位符。
在一个SORT命令中可以有多个GET参数,而BY参数只能有一个,有N个GET参数,每个元素返回的结果就有N行。
Here Insert Picture Description

4.3、组合使用BY和GET参数,同时可以获取多个字段

Here Insert Picture Description

4.4、获取外部键,但不进行排序

将一个不存在的键作为参数传给 BY 选项, 可以让 SORT 跳过排序操作。通过将这种用法和 GET 选项配合, 可以在不排序的情况下, 获取多个外部键, 相当于执行一个整合的获取操作(类似于 SQL 数据库的 join 关键字)
当参考键名不包含“*”时(即常量键名,与元素值无关),SORT命令将不会执行排序操作,因为Redis认为这种情况是没有意义的(因为所有要比较的值都一样)
Here Insert Picture Description
GET # 可以获取排序的外键

5、将哈希表作为get或by的参数

数据:
Here Insert Picture Description

5.1、示例:按照name进行排序,并输出id,name,age

Here Insert Picture Description
BY 和 GET 选项都可以用 key->field 的格式来获取哈希表中的域的值, 其中 key 表示哈希表键, 而 field 则表示哈希表的域
参考键虽然支持散列类型,但是“*”只能在“->”符号前面( 即键名部分)才有用,在“->”后( 即字段名部分) 会被当成字段名本身而不会作为占位符被元素的值替換,即常量键名。

5.2、保存排序结果

The default sort only used to sort, sort result is not stored, it may be used STORE store the result in the specified key. Type key after saving a list type, if the key already exists it will be overwritten. After adding SORT STORE command parameter is the number of results to return.
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/lianghecai52171314/article/details/102771652