排序
SORT
sort命令的格式:sort key [BY pattern] [LIMIT start count] [GET pattern] [ASC|DESC] [ALPHA] [STORE dstkey]
用来对列表类型、集合类型、有序集合类型键类型进行排序;默认是按照数值类型排序的,并且按照两个元素的双精度浮点数类型值进行比较
127.0.0.1:6379> lpush listsort 1 5 2 6 9
(integer) 5
127.0.0.1:6379> sort listsort
1) "1"
2) "2"
3) "5"
4) "6"
5) "9"
#如果时字符类型会报错
127.0.0.1:6379> lrange list 0 4
1) "a1"
2) "b"
3) "a"
127.0.0.1:6379> sort list
(error) ERR One or more scores can\'t be converted into double
如果时字符串值并且是按照字典顺序排序,可以使用alpha参数修饰
127.0.0.1:6379> sort list ALPHA
1) "a"
2) "a1"
3) "b"
[BY pattern]
除了可以按集合元素自身值(数字,字母)排序外,还可以将集合元素内容按照给定pattern组合成新的key,并按照新key中对应的内容进行排序。
#根据自身的key排序
127.0.0.1:6379> lrange mylist 0 4
1) "a4"
2) "a2"
3) "a3"
4) "a1"
127.0.0.1:6379> sort mylist by a* desc
1) "a4"
2) "a3"
3) "a2"
4) "a1"
#根据外部的key进行排序
127.0.0.1:6379> lrange mylist1 0 -1
1) "4"
2) "2"
3) "1"
4) "3"
127.0.0.1:6379> mget a1 a2 a3 a4
1) "a1"
2) "a2"
3) "a3"
4) "a4"
127.0.0.1:6379> sort mylist1 by a* alpha
1) "1"
2) "2"
3) "3"
4) "4"
[GET pattern]
从上面的实例来看,sort通过外部的key a1,a2,a3,a4 对mylist1进行了排序,那么也可以通过get 去获取指定pattern作为新key去获取值
127.0.0.1:6379> sort mylist1 by a* get a* alpha
1) "a1"
2) "a2"
3) "a3"
4) "a4"
在get中还有特殊的符号#,表示获取原始集合
127.0.0.1:6379> sort mylist1 by a* get a* get # alpha
1) "a1"
2) "1"
3) "a2"
4) "2"
5) "a3"
6) "3"
7) "a4"
8) "4"
还有在hash类型中的 ->符号
127.0.0.1:6379> hset user_1 name xiaozhang
(integer) 1
127.0.0.1:6379> hset user_2 name xiaoliu
(integer) 1
127.0.0.1:6379> hset user_3 name xiaowang
(integer) 1
127.0.0.1:6379> hset user_4 name xiaowu
(integer) 1
127.0.0.1:6379> lrange mylist1 0 3
1) "4"
2) "2"
3) "1"
4) "3"
127.0.0.1:6379> sort mylist1 get user_*->name
1) "xiaozhang"
2) "xiaoliu"
3) "xiaowang"
4) "xiaowu"
127.0.0.1:6379> sort mylist1 get user_*->name desc
1) "xiaowu"
2) "xiaowang"
3) "xiaoliu"
4) "xiaozhang"
[LIMIT offset count]
限定排序后的返回结果
127.0.0.1:6379> sort mylist1 get user_*->name desc
1) "xiaowu"
2) "xiaowang"
3) "xiaoliu"
4) "xiaozhang"
127.0.0.1:6379> sort mylist1 get user_*->name limit 0 2 desc
1) "xiaowu"
2) "xiaowang"
[STORE dstkey]
将排序后的结果保存起来,保存数据类型的是list类型
127.0.0.1:6379> sort mylist1 get user_*->name limit 0 2 desc
1) "xiaowu"
2) "xiaowang"
127.0.0.1:6379> sort mylist1 get user_*->name limit 0 2 desc store sortresult
(integer) 2
127.0.0.1:6379> lrange sortresult 0 2
1) "xiaowu"
2) "xiaowang"
hsah中使用get和by
127.0.0.1:6379> lrange mylist1 0 3
1) "4"
2) "2"
3) "1"
4) "3"
127.0.0.1:6379> hmset hash:1 name zhang age 18
OK
127.0.0.1:6379> hmset hash:2 name zhao age 19
OK
127.0.0.1:6379> hmset hash:3 name qian age 15
OK
127.0.0.1:6379> hmset hash:4 name sun age 20
OK
127.0.0.1:6379> sort mylist1 by hash:*->age desc
1) "4"
2) "2"
3) "1"
4) "3"
127.0.0.1:6379> sort mylist1 by hash:*->age desc get hash:*->age
1) "20"
2) "19"
3) "18"
4) "15"
127.0.0.1:6379> sort mylist1 by hash:*->age desc get hash:*->name
1) "sun"
2) "zhao"
3) "zhang"
4) "qian"
127.0.0.1:6379> sort mylist1 by hash:*->age desc get hash:*->name get hash:*->age
1) "sun"
2) "20"
3) "zhao"
4) "19"
5) "zhang"
6) "18"
7) "qian"
8) "15"
性能问题
![image-20200708145223788](
image-20200708145223788.png)