*Redis ZINTERSTORE 命令 计算交集并存储到新有序集合
*语法
ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM | MIN | MAX]
*说明
计算多个有序集合的交集,并将结果存储到 destination 中。
选项:
WEIGHTS:为每个集合指定权重系数,计算前 score 会先乘以权重AGGREGATE:指定交集结果的 score 计算方式(SUM 求和、MIN 最小值、MAX 最大值)
*返回值
- Integer:存储到
destination中的成员数量
*时间复杂度
- O(NK)+O(Mlog(M)),其中
N是最小集合的成员数,K是集合数量,M是结果集合的成员数。
*版本兼容性
| Redis 版本 | 变更说明 |
|---|---|
| >= 2.0.0 | 可用 |
*示例
redis-cli ZADD zset1 1 "a" 2 "b" 3 "c"
redis-cli ZADD zset2 1 "a" 2 "b" 4 "d"
# 基本交集存储
redis-cli ZINTERSTORE result 2 zset1 zset2
redis-cli ZRANGE result 0 -1 WITHSCORES
# 带权重的交集
redis-cli ZINTERSTORE wresult 2 zset1 zset2 WEIGHTS 2 3
redis-cli ZRANGE wresult 0 -1 WITHSCORES
# 使用 MIN 聚合
redis-cli ZINTERSTORE minresult 2 zset1 zset2 AGGREGATE MIN