Skip to main content

ZINTERCARD

Introduction

In Dragonfly, as well as in Redis and Valkey, the ZINTERCARD command performs an intersection of multiple sorted sets. This command is similar to ZINTER, but instead of returning the result set, it returns just the cardinality of the result.

By default, the command calculates the cardinality of the intersection of all given sets. When provided with the optional LIMIT argument (which defaults to 0 and means unlimited), if the intersection cardinality reaches the limit partway through the computation, the algorithm will exit and yield the limit value as the cardinality.

Syntax

ZINTERCARD numkeys key [key ...] [LIMIT limit]
  • Time complexity: O(N*K) worst case with N being the smallest input sorted set, K being the number of input sorted sets.
  • ACL categories: @read, @sortedset, @slow

Parameter Explanations

  • numkeys: The number of sorted sets to be intersected.
  • key [key ...]: The list of sorted sets to intersect.
  • LIMIT limit (optional): Set the cardinality limit.

Return Values

  • Integer reply the cardinality (number of elements) of members in the resulting intersection.

Code Examples

dragonfly$> ZADD zset1 1 a 2 b 3 c
(integer) 3

dragonfly$> ZADD zset2 1 a 2 b 3 c 4 d 5 e
(integer) 5

dragonfly$> ZINTER 2 zset1 zset2
1) "a"
2) "b"
3) "c"

dragonfly$> ZINTERCARD 2 zset1 zset2
(integer) 3

dragonfly$> ZINTERCARD 2 zset1 zset2 LIMIT 2
(integer) 2