ZINTERSTORE(3valkey)

πŸ“¦
version 9.0.6 Β· 2 versions
, in fedora 44

πŸ’– 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 πŸ’œ


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