本文内容为 http://redis.io/commands/zinterstore 的翻译、注解、例子扩充及其它修改。感谢 Redis 作者 Antirez 为开源社区作出的贡献,本文保证最新、最准、最全以表示对其的敬意。欢迎留言纠错、提示更新或支持。
ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]

计算由给定键指定的 numkeys 个有序集合的交集(Intersection),然后将结果存储在键 destination 中。

Computes the intersection of numkeys sorted sets given by the specified keys, and stores the result in destination.

该命令强制在传递输入键及其它(可选)参数前提供输入键的数量。

It is mandatory to provide the number of input keys (numkeys) before passing the input keys and the other (optional) arguments.

默认的,一个元素的最终分值(Score)为所有存在该元素的有序集合中对应分值的总和(Sum)

By default, the resulting score of an element is the sum of its scores in the sorted sets where it exists.

对 WEIGHTS 及 AGGREGATE 选项的详细描述请参考 ZUNIONSTORE

如果目标键 destination 已经存在,将会被覆盖。

If destination already exists, it is overwritten.
  • 返回键 destination 对应的有序集合中元素的个数。

    The number of elements in the resulting sorted set at destination.
  • redis> ZADD zset1 1 "one"
    (integer) 1
    redis> ZADD zset1 2 "two"
    (integer) 1
    redis> ZADD zset2 1 "one"
    (integer) 1
    redis> ZADD zset2 2 "two"
    (integer) 1
    redis> ZADD zset2 3 "three"
    (integer) 1
    redis> ZINTERSTORE out 2 zset1 zset2 WEIGHTS 2 3
    (integer) 2
    redis> ZRANGE out 0 -1 WITHSCORES
    1) "one"
    2) "5"
    3) "two"
    4) "10"
  • 版本支持

    2.0.0+

    时间复杂度(Time complexity)

    最坏情况为 O(N*K)+O(M*log(M))

    O(N*K)+O(M*log(M)) worst case with N being the smallest input sorted set, K being the number of input sorted sets and M being the number of elements in the resulting sorted set.