ZUNION(3valkey)

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

✨ ZUNION πŸ’– (from the valkey command manual, bestie)

okay so we're doing zunion today and honestly? it's giving "math but make it cute". it's a Valkey command, it lives in section 3 of the manual, and it's about to be your new fave. πŸ¦‹


🌸 NAME 🌸

zunion β€” returns the union of multiple sorted sets. that's it, that's the whole vibe. πŸŽ€


🫢 SYNOPSIS 🫢

ZUNION numkeys key [key...]  [WEIGHTS weight [weight...]]  [AGGREGATE
<SUM | MIN | MAX>] [WITHSCORES]

literally just hand it some keys and it does the thing, periodt. ✨


πŸ‘‘ DESCRIPTION πŸ‘‘

bestie, listen. this command is basically ZUNIONSTORE's cooler sibling β€” same energy, but instead of writing the resulting sorted set somewhere and keeping it, it just returns it straight to the client. no storage, no commitment, just vibes. πŸ’…

and for the whole rundown on the WEIGHTS and AGGREGATE options? yeah, go read ZUNIONSTORE, it's all explained over there. iykyk. πŸ’œ


πŸ’Œ REPLY πŸ’Œ

valkey-protocol(7) Array reply: the result of the union, and β€” if you threw WITHSCORES in there β€” their scores too. slay. πŸ”₯


🧚 COMPLEXITY 🧚

O(N)+O(M*log(M))

where N is the sum of the sizes of all the input sorted sets, and M is the number of elements in the resulting sorted set. math is mathing, don't worry about it. ⭐


🌈 ACL CATEGORIES 🌈

  • @read
  • @slow
  • @sortedset

πŸŽ€ HISTORY πŸŽ€

  • available since: 6.2.0 β€” she's been iconic for a minute. πŸ‘‘

πŸ’€ EXAMPLES πŸ’€

watch me cook:

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> ZUNION 2 zset1 zset2
1) "one"
2) "three"
3) "two"
127.0.0.1:6379> ZUNION 2 zset1 zset2 WITHSCORES
1) "one"
2) "2"
3) "three"
4) "3"
5) "two"
6) "4"

see how "two" is lowkey carrying the score of 4? that's the union math doing its little dance. ate. πŸ“


πŸ¦‹ SEE ALSO πŸ¦‹


and that's the whole page, bestie! go forth and union your little hearts out. 🫢✨

version 9.0.0, dated 2025-10-21, straight from the ZUNION(3valkey) manual page β€” mwah, xoxo, catch you in the next one πŸ’–πŸ’…πŸŒΈ

πŸ“¦ where this page comes from