ZDIFFSTORE(3valkey)
π 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:
Lis the total number of elements in all the sets,Nis the size of the first set,Kis 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 π
bzmpop(3valkey)bzpopmax(3valkey)bzpopmin(3valkey)zadd(3valkey)zcard(3valkey)zcount(3valkey)zdiff(3valkey)zincrby(3valkey)zinter(3valkey)zintercard(3valkey)zinterstore(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)
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
- 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