ZINTERSTORE(3valkey)
π ZINTERSTORE π
okay bestie, gather round, because this one is the intersection queen of the sorted-set world and she is lowkey iconic. it's ZINTERSTORE, she lives in the Valkey Command Manual, and she's about to make two (or twenty) sorted sets hold hands and keep only the friends they all have in common. no cap.
β¨ NAME β¨
zinterstore β stores the intersect of multiple sorted sets in a key. π
πΈ SYNOPSIS πΈ
ZINTERSTORE destination numkeys key [key...] [WEIGHTS weight
[weight...]] [AGGREGATE <SUM | MIN | MAX>]
π¦ DESCRIPTION π¦
so here's the vibe: it computes the intersection of numkeys sorted sets, given by the specified keys, and then it stores the result in destination. that's the whole personality. π
and listen, you MUST give it the number of input keys (numkeys) before you hand over the input keys and the other (optional) arguments. it's mandatory. it's not a suggestion. it's giving "order matters, bestie". π
by default, the resulting score of an element is the sum of its scores in the sorted sets where it exists. and because intersection literally requires an element to be a member of every single given sorted set, this results in the score of every element in the resulting sorted set being equal to the number of input sorted sets. math ate, honestly. β¨
for a description of the WEIGHTS and AGGREGATE options, go peek at ZUNIONSTORE β she's the sibling, iykyk.
and if destination already exists? it gets overwritten. like, gone. replaced. no warning, no goodbye text. π
π REPLY π
valkey-protocol(7) Integer reply: the number of members in the resulting sorted set at the destination. π«Ά
π₯ COMPLEXITY π₯
O(NK)+O(Mlog(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. slay, but make it algorithmic. π
π ACL CATEGORIES π
@slow @sortedset @write
π°οΈ HISTORY π°οΈ
- Available since:
2.0.0β she's been here since forever, a true veteran main character. π
π EXAMPLES π
127.0.0.1:6379> ZADD zset1 1 "one"
(integer) 1
127.0.0.1:6379> ZADD zset1 2 "two"
(integer) 1
127.0.0.1:6379> ZADD zset2 1 "one"
(integer) 1
127.0.0.1:6379> ZADD zset2 2 "two"
(integer) 1
127.0.0.1:6379> ZADD zset2 3 "three"
(integer) 1
127.0.0.1:6379> ZINTERSTORE out 2 zset1 zset2 WEIGHTS 2 3
(integer) 2
127.0.0.1:6379> ZRANGE out 0 -1 WITHSCORES
1) "one"
2) "5"
3) "two"
4) "10"
look at "one" getting 1*2 + 1*3 = 5 and "two" getting 2*2 + 2*3 = 10. the WEIGHTS said "multiply, girl" and she multiplied. "three" didn't make the cut because intersection is exclusive like that. πβ¨
π SEE ALSO π
bzmpop(3valkey)bzpopmax(3valkey)bzpopmin(3valkey)zadd(3valkey)zcard(3valkey)zcount(3valkey)zdiff(3valkey)zdiffstore(3valkey)zincrby(3valkey)zinter(3valkey)zintercard(3valkey)zlexcount(3valkey)zmpop(3valkey)zmscore(3valkey)zpopmax(3valkey)zpopmin(3valkey)zrandmember(3valkey)zrange(3valkey)zrangebylex(3valkey)zrangebyscore(3valkey)zrangestore(3valkey)zrank(3valkey)zrem(3valkey)zremrangebylex(3valkey)zremrangebyrank(3valkey)zremrangebyscore(3valkey)zrevrange(3valkey)zrevrangebylex(3valkey)zrevrangebyscore(3valkey)zrevrank(3valkey)zscan(3valkey)zscore(3valkey)zunion(3valkey)zunionstore(3valkey)
okay that's the whole page, bestie, go forth and intersect responsibly π§π
sign-off time: this was ZINTERSTORE(3valkey) from the Valkey Command Manual, version 9.0.0, dated 2025-10-21. periodt. π
π
π¦ where this page comes from
- fedora 44: valkey-doc 9.0.3-2.fc44 Β· CC-BY-SA-4.0
- fedora 44: valkey-doc 9.0.6-1.fc44 Β· CC-BY-SA-4.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms