ZDIFFSTORE(3valkey)

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

πŸ’– ZDIFFSTORE πŸ’–

okay so bestie, welcome to the Valkey Command Manual ✨ this one lives in section 3valkey and it's genuinely one of my faves, no cap.


✨ NAME ✨

zdiffstore β€” stores the difference of multiple sorted sets in a key. like, it does the math and then parks the answer in a whole new key. obsessed. πŸŽ€


🌸 SYNOPSIS 🌸

ZDIFFSTORE destination numkeys key [key...]

πŸ¦‹ DESCRIPTION πŸ¦‹

So here's the vibe: ZDIFFSTORE computes the difference between the first input sorted set and all the successive input sorted sets, then stores the result in destination. πŸ’…

The total number of input keys is specified by numkeys. yes girl, you have to tell it how many. it can't read your mind (yet). 😭

Keys that don't exist? they're considered to be empty sets. no drama, no error, just an empty vibe. 🫢

And if destination already exists, it gets overwritten. so like… gone. replaced. main character energy only. πŸ‘‘


πŸ’Œ REPLY πŸ’Œ

valkey-protocol(7) Integer reply: the number of members in the resulting sorted set at destination. so you get a lil count back, cute. ⭐


πŸ”₯ COMPLEXITY πŸ”₯

O(L + (N-K)log(N)) worst case, where:

  • L is the total number of elements in all the sets,
  • N is the size of the first set,
  • K is the size of the result set.

not me actually respecting the worst case 😭 it's giving "we've thought about this" fr fr.


πŸŽ€ ACL CATEGORIES πŸŽ€

@slow @sortedset @write

three whole categories, slay. πŸ’–


🌈 HISTORY 🌈

  • Available since: 6.2.0

an icon from day one honestly.


✨ EXAMPLES ✨

okay bestie, watch this, it's so satisfying:

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 zset1 3 "three"
(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> ZDIFFSTORE out 2 zset1 zset2
(integer) 1
127.0.0.1:6379> ZRANGE out 0 -1 WITHSCORES
1) "three"
2) "3"

see how "three" was the only one that survived the difference?? the way it ate and left no crumbs πŸ“


πŸ’œ SEE ALSO πŸ’œ

like a whole friend group of sorted-set commands, iykyk. 🧚


go read the whole squad, bestie. sorted sets are lowkey the moment. πŸ™ˆ

okay byeeee, this page was Valkey 9.0.0, dated 2025-10-21 β€” drink water, use ZDIFFSTORE responsibly, and remember: overwriting a key is forever πŸ’€πŸ«Ά

πŸ“¦ where this page comes from