ZINTERCARD(3valkey)

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

✨ ZINTERCARD ✨

valkey manual page, bestie β€” and yes we're doing the whole thing, so grab a snack πŸ“


NAME πŸ’…

zintercard β€” returns the number of members of the intersect of multiple sorted sets. that's it, that's the girl. she counts. she does not return. iconic behavior honestly πŸ‘‘


SYNOPSIS 🌸

ZINTERCARD numkeys key [key...] [LIMIT limit]

DESCRIPTION πŸ¦‹

okay so. this command is basically ZINTER's sibling, but she's the low-key one β€” instead of handing you the whole result set, she just gives you the cardinality of the result. just the number. no extra drama. πŸ’…

keys that don't exist? consider them empty sets, bestie. and if even one of your keys is an empty set, the resulting set is empty too β€” because set intersection with an empty set always gives you an empty set. math said no. 😭

by default, the command calculates the cardinality of the intersection of all the sets you gave it. but when you pass the optional LIMIT argument β€” which defaults to 0 and means unlimited β€” something cute happens: if the intersection cardinality reaches limit partway through the computation, the algorithm just exits and yields limit as the cardinality. and that implementation? it ensures a significant speedup for queries where the limit is lower than the actual intersection cardinality. queen of not doing extra work, we love to see it πŸ”₯


REPLY πŸ’–

valkey-protocol(7) Integer reply: the number of members in the resulting intersection.


COMPLEXITY 🧚

O(N*K) worst case, with N being the smallest input sorted set and K being the number of input sorted sets.


ACL CATEGORIES πŸŽ€</bad>

@read @slow @sortedset


HISTORY ⭐

  • Available since: 7.0.0

EXAMPLES 🌈

okay watch this, fr fr:

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> ZINTER 2 zset1 zset2
1) "one"
2) "two"
127.0.0.1:6379> ZINTERCARD 2 zset1 zset2
(integer) 2
127.0.0.1:6379> ZINTERCARD 2 zset1 zset2 LIMIT 1
(integer) 1

see how ZINTER spills the whole tea and ZINTERCARD is like "it's 2, bye"? and with LIMIT 1 she doesn't even finish counting β€” she already knows it's 1 and dips. main character energy, no cap 🫢


SEE ALSO πŸ‘‘


and that's the whole page, bestie!! πŸ¦‹ this was Valkey manual version 9.0.0, dated 2025-10-21, and you were reading ZINTERCARD(3valkey). go count something. slay πŸ’–βœ¨πŸ‘‘

πŸ“¦ where this page comes from