search
HomeDatabaseMysql TutorialNosql之Redis: zset(有序集)数据类型及操作命令

一:概述 zset全称为sorted-sets类型,和set数据类型有极为相似,都是字符串的集合,都不允许重复的成员 出现在一个set中.两者的主要区别是zset的每一个成员都会有一个分数(score)与之关联.redis正是通过分数来为集合中的成员进行从小到大的排序.zset的成员是唯

一:概述
zset全称为sorted-sets类型,和set数据类型有极为相似,都是字符串的集合,都不允许重复的成员
出现在一个set中.两者的主要区别是zset的每一个成员都会有一个分数(score)与之关联.redis正是通过分数来为集合中的成员进行从小到大的排序.zset的成员是唯一的,但分数(score)却可以重复.
在zset中添加、删除或更新一个成员都是非常快速的操作,其时间复杂度为集合中成员数量的对数.

Sorted-Sets中的成员在集合中的位置是有序的.

二:相关命令

1: zadd
命令格式: zadd key score member [[score] [member] …]

描述:将一个或多个 member 元素及其 score 值加入到有序集 key 当中.如果某个 member 已经是有序集的成员,那么更新这个 member 的 score 值,并通过重新插入这个 member 元素,来保证该 member 在正确的位置上。score 值可以是整数值或双精度浮点数。如果 key 不存在,则创建一个空的有序集并执行 ZADD 操作。当 key 存在但不是有序集类型时,返回一个错误。

时间复杂度: O(M*log(N)), N 是有序集的基数, M 为成功添加的新成员的数量
返回值:被成功添加的新成员的数量,不包括那些被更新的、已经存在的成员。

操作命令如下:

#添加一个元素
redis 127.0.0.1:6379> zadd zset_list 11 test1
(integer) 1

#添加多个元素
redis 127.0.0.1:6379> zadd zset_list 9 test2 10 test3
(integer) 2

#查看元素
redis 127.0.0.1:6379> zrange zset_list 0 -1 withscores
1) “test2″
2) “9″
3) “test3″
4) “10″
5) “test1″
6) “11″
redis 127.0.0.1:6379> zrange zset_list 0 -1
1) “test2″
2) “test3″
3) “test1″

# 添加已存在元素,且 score 值不变 操作不成功返回0
redis 127.0.0.1:6379> zadd zset_list 10 test1
(integer) 0

redis 127.0.0.1:6379> zrange zset_list 0 -1 withscores
1) “test2″
2) “9″
3) “test1″
4) “10″
5) “test3″
6) “10″

# 添加已存在元素,但是改变 score 值
redis 127.0.0.1:6379> zadd zset_list 7 test1
(integer) 0
redis 127.0.0.1:6379> zrange zset_list 0 -1 withscores
1) “test1″
2) “7″
3) “test2″
4) “9″
5) “test3″
6) “10″
2:zrem
命令格式: ZREM key member [member ...]

描述:移除有序集 key 中的一个或多个成员,不存在的成员将被忽略。
当 key 存在但不是有序集类型时,返回一个错误。
时间复杂度:O(M*log(N)), N 为有序集的基数, M 为被成功移除的成员的数量。
返回值:被成功移除的成员的数量,不包括被忽略的成员。
操作命令如下:

redis 127.0.0.1:6379> zrange zset_list 0 -1 withscores
1) “test1″
2) “7″
3) “test2″
4) “9″
5) “test3″
6) “10″

#移除单个元素
redis 127.0.0.1:6379> zrem zset_list test1
(integer) 1
redis 127.0.0.1:6379> zrange zset_list 0 -1 withscores
1) “test2″
2) “9″
3) “test3″
4) “10″
#移除多个
redis 127.0.0.1:6379> zrem zset_list test2 test3
(integer) 2
redis 127.0.0.1:6379> zrange zset_list 0 -1 withscores
(empty list or set)

# 移除不存在元素
redis 127.0.0.1:6379> zrem zset_list non-exists-element
(integer) 0

3:zcard
描述:返回zset集合的成员数
时间复杂度:O(1)
返回值:当 key 存在且是有序集(zset)类型时,返回集合内的成员数。不存在返回0。
操作命令如下:
redis 127.0.0.1:6379> zcard zset_list
(integer) 0
redis 127.0.0.1:6379> zadd zset_list 1 test1
(integer) 1
redis 127.0.0.1:6379> zcard zset_list
(integer) 1

4:zcount
命令格式:ZCOUNT key min max
描述:返回有序集 key 中, score 值在 min 和 max 之间(默认包括 score 值等于 min 或 max )的成员的数量。
时间复杂度: O(log(N)+M), N 为有序集的基数, M 为值在 min 和 max 之间的元素的数量。
返回值:score 值在 min 和 max 之间的成员的数量。
操作命令如下:
redis 127.0.0.1:6379> zrange zset_list 0 -1 withscores
1) “test1″
2) “1″
3) “test2″
4) “100″
5) “test3″
6) “200″
7) “test4″
8) “300″
redis 127.0.0.1:6379> zcount zset_list 100 200
(integer) 2
redis 127.0.0.1:6379> zcount zset_list 100 300
(integer) 3
5: zscore
命令格式:ZSCORE key member
描述:返回有序集 key 中,成员 member 的 score 值。
如果 member 元素不是有序集 key 的成员,或 key 不存在,返回 nil 。
时间复杂度:O(1)
操作命令如下:
redis 127.0.0.1:6379> zrange zset_list 0 -1 withscores
1) “test1″
2) “1″
3) “test2″
4) “100″
5) “test3″
6) “200″
7) “test4″
8) “300″
redis 127.0.0.1:6379> zscore zset_list test2
“100″

6:zincrby
命令格式:ZINCRBY key increment member
描述:为有序集 key 的成员 member 的 score 值加上增量 increment 。
时间复杂度:O(log(N))
返回值: 返回member 成员的新 score 值,以字符串形式表示。
操作命令如下:
redis 127.0.0.1:6379> zscore zset_list test2
“100″
redis 127.0.0.1:6379>
redis 127.0.0.1:6379> zincrby zset_list 10 test2
“110″
redis 127.0.0.1:6379> zincrby zset_list -6 test2
“104″
7:zrange
命令格式: ZRANGE key start stop [WITHSCORES]
描述:返回指定区间的成员。其中成员位置按 score 值递增(从小到大)来排序。 WITHSCORES选项是用来让成员和它的score值一并返回.(在前面我们已经用到过)
时间复杂度:O(log(N)+M), N 为有序集的基数,而 M 为结果集的基数。
返回值:返回指定区间的成员列表.
操作命令如下:

redis 127.0.0.1:6379> zrange zset_list 0 -1
1) “test1″
2) “test2″
3) “test3″
4) “test4″
redis 127.0.0.1:6379> zrange zset_list 0 -1 withscores
1) “test1″
2) “1″
3) “test2″
4) “104″
5) “test3″
6) “200″
7) “test4″
8) “300″

#当给定区间不存在于有序集时的情况
redis 127.0.0.1:6379> zrange zset_list 10000 30000 withscores
(empty list or set)

7:zrevrange
命令格式:ZREVRANGE key start stop [WITHSCORES]
描述:和zrange一样使用,唯一不同是其成员位置按 score 值递减(从大到小)来排列。
8:zrangebyscore
命令格式:ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
描述:返回有序集key中所有score值介于min与max之间(包括等于)的成员.成员按score值递增(从小到大)排列 。min 和 max 可以是 -inf 和 +inf
可选limit参数指定返回结果的数量及区间。
时间复杂度:O(log(N)+M), N 为有序集的基数, M 为被结果集的基数。
返回值:指定区间内,带有 score 值(可选)的有序集成员的列表。
操作命令如下:
redis 127.0.0.1:6379> zrangebyscore zset_list -inf +inf
1) “test1″
2) “test2″
3) “test3″
4) “test4″
redis 127.0.0.1:6379> zrangebyscore zset_list -inf +inf withscores
1) “test1″
2) “1″
3) “test2″
4) “104″
5) “test3″
6) “200″
7) “test4″
8) “300″
redis 127.0.0.1:6379> zrangebyscore zset_list -inf 100 withscores
1) “test1″
2) “1″

#显示大于100 小于等于700的成员
redis 127.0.0.1:6379> zrangebyscore zset_list (100 700
1) “test2″
2) “test3″
3) “test4″

#显示条件 100 redis 127.0.0.1:6379> zrangebyscore zset_list (100 (700
1) “test2″
2) “test3″
3) “test4″
8:zrevrangebyscore
命令格式: zrevrangebyscore key max min [WITHSCORES] [LIMIT offset count]
描述:和zrangebyscoreg一样,唯一不同的是成员按 score 值递减(从大到小)的次序排列。

9:zrank
命令格式: zrank key member
描述:返回有序集key中成员member的排名。成员按 score 值递增(从小到大)顺序排列。
排名以0开始,也就是说score 值最小的为0.
时间复杂度:O(log(N))
返回值:返回成员排名,member不存在返回nil.

9:zrevrank
命令格式: zrevrank key member
描述:返回有序集key中成员member的排名。成员按 score 值递增(从大到小)顺序排列。
排名以0开始,也就是说score 值最大的为0.
时间复杂度:O(log(N))
返回值:返回成员排名,member不存在返回nil.
10:zremrangebyrank
命令格式: ZREMRANGEBYRANK key start stop
描述:移除有序集 key 中,指定排名(rank)区间内的所有成员。区间分别以下标参数 start 和 stop 指出,包含 start 和 stop 在内。
下标参数 start 和 stop 都以 0 为底,也就是说,以 0 表示有序集第一个成员,以 1 表示有序集第二个成员,以此类推。
你也可以使用负数下标,以 -1 表示最后一个成员, -2 表示倒数第二个成员,以此类推。
时间复杂度:O(log(N)+M), N 为有序集的基数,而 M 为被移除成员的数量。
返回值:被移除成员的数量。

11:zremrangebyscore
命令格式:zremrangebyscore key min max
描述:移除score值介于min和max之间(等于)的成员
时间复杂度:O(log(N)+M), N 为有序集的基数,而 M 为被移除成员的数量。
返回值:被移除成员的数量。
操作如下:
# 移除所有score在 150 到 350 内的数据
redis> zremrangebyscore zset_list 100 200
(integer) 1

12:zunionstore
命令格式:ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
描述:计算给定的一个或多个有序集的并集,其中给定 key 的数量必须以 numkeys 参数指定,并将该并集(结果集)储存到 destination 。默认情况下,结果集中某个成员的 score 值是所有给定集下该成员 score 值之 和 。
12: zinterstore
命令格式:ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
描述:计算给定的一个或多个有序集的交集。其中给定 key 的数量必须以 numkeys 参数指定,并将该交集(结果集)储存到 destination 。默认情况下,结果集中某个成员的 score 值是所有给定集下该成员 score 值之 和 。
时间复杂度:
O(N*K)+O(M*log(M)), N 为给定 key 中基数最小的有序集, K 为给定有序集的数量, M 为结果集的基数。
返回值:保存到 destination 的结果集成员数。
操作命令如下:
redis 127.0.0.1:6379> zrange z_ulist_1 0 -1 withscores
1) “jack”
2) “20″
3) “abc”
4) “30″
5) “bb”
6) “50″
7) “cc”
8) “50″
redis 127.0.0.1:6379> zadd z_ulist_2 20 bb 40 789 48 a980
(integer) 3
redis 127.0.0.1:6379> zinterstore z_ulist_x 2 z_ulist_1 z_ulist_2
(integer) 1
redis 127.0.0.1:6379> zrange z_ulist_x 0 -1 withscores
1) “bb”
2) “70″

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does MySQL's licensing compare to other database systems?How does MySQL's licensing compare to other database systems?Apr 25, 2025 am 12:26 AM

MySQL uses a GPL license. 1) The GPL license allows the free use, modification and distribution of MySQL, but the modified distribution must comply with GPL. 2) Commercial licenses can avoid public modifications and are suitable for commercial applications that require confidentiality.

When would you choose InnoDB over MyISAM, and vice versa?When would you choose InnoDB over MyISAM, and vice versa?Apr 25, 2025 am 12:22 AM

The situations when choosing InnoDB instead of MyISAM include: 1) transaction support, 2) high concurrency environment, 3) high data consistency; conversely, the situation when choosing MyISAM includes: 1) mainly read operations, 2) no transaction support is required. InnoDB is suitable for applications that require high data consistency and transaction processing, such as e-commerce platforms, while MyISAM is suitable for read-intensive and transaction-free applications such as blog systems.

Explain the purpose of foreign keys in MySQL.Explain the purpose of foreign keys in MySQL.Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

What are the different types of indexes in MySQL?What are the different types of indexes in MySQL?Apr 25, 2025 am 12:12 AM

There are four main index types in MySQL: B-Tree index, hash index, full-text index and spatial index. 1.B-Tree index is suitable for range query, sorting and grouping, and is suitable for creation on the name column of the employees table. 2. Hash index is suitable for equivalent queries and is suitable for creation on the id column of the hash_table table of the MEMORY storage engine. 3. Full text index is used for text search, suitable for creation on the content column of the articles table. 4. Spatial index is used for geospatial query, suitable for creation on geom columns of locations table.

How do you create an index in MySQL?How do you create an index in MySQL?Apr 25, 2025 am 12:06 AM

TocreateanindexinMySQL,usetheCREATEINDEXstatement.1)Forasinglecolumn,use"CREATEINDEXidx_lastnameONemployees(lastname);"2)Foracompositeindex,use"CREATEINDEXidx_nameONemployees(lastname,firstname);"3)Forauniqueindex,use"CREATEU

How does MySQL differ from SQLite?How does MySQL differ from SQLite?Apr 24, 2025 am 12:12 AM

The main difference between MySQL and SQLite is the design concept and usage scenarios: 1. MySQL is suitable for large applications and enterprise-level solutions, supporting high performance and high concurrency; 2. SQLite is suitable for mobile applications and desktop software, lightweight and easy to embed.

What are indexes in MySQL, and how do they improve performance?What are indexes in MySQL, and how do they improve performance?Apr 24, 2025 am 12:09 AM

Indexes in MySQL are an ordered structure of one or more columns in a database table, used to speed up data retrieval. 1) Indexes improve query speed by reducing the amount of scanned data. 2) B-Tree index uses a balanced tree structure, which is suitable for range query and sorting. 3) Use CREATEINDEX statements to create indexes, such as CREATEINDEXidx_customer_idONorders(customer_id). 4) Composite indexes can optimize multi-column queries, such as CREATEINDEXidx_customer_orderONorders(customer_id,order_date). 5) Use EXPLAIN to analyze query plans and avoid

Explain how to use transactions in MySQL to ensure data consistency.Explain how to use transactions in MySQL to ensure data consistency.Apr 24, 2025 am 12:09 AM

Using transactions in MySQL ensures data consistency. 1) Start the transaction through STARTTRANSACTION, and then execute SQL operations and submit it with COMMIT or ROLLBACK. 2) Use SAVEPOINT to set a save point to allow partial rollback. 3) Performance optimization suggestions include shortening transaction time, avoiding large-scale queries and using isolation levels reasonably.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download

Atom editor mac version download

The most popular open source editor