SUNIONCARD

SUNIONCARD numkeys key [key ...] [APPROX] [LIMIT limit]
Available since:
Redis Open Source 8.10.0
Time complexity:
O(N) where N is the total number of elements in all given sets.
ACL categories:
@read, @set, @slow,
Compatibility:
Redis Software and Redis Cloud compatibility
Note:
This command's behavior varies in clustered Redis environments. See the multi-key operations page for more information.

Returns the cardinality of the union of the given sets. This is the count-only counterpart of SUNION: it returns just the number of distinct elements in the union, not the members themselves.

Required arguments

numkeys

The number of keys that follow.

key [key ...]

One or more set keys to union.

Optional arguments

APPROX

Return an approximate cardinality instead of an exact count.

LIMIT limit

Stop counting once the cardinality reaches limit. 0 (the default) means no limit.

Examples

> SADD key1 "a"
(integer) 1
> SADD key1 "b"
(integer) 1
> SADD key1 "c"
(integer) 1
> SADD key2 "c"
(integer) 1
> SADD key2 "d"
(integer) 1
> SADD key2 "e"
(integer) 1
> SUNION key1 key2
1) "a"
2) "c"
3) "d"
4) "b"
5) "e"
> SUNIONCARD 2 key1 key2
(integer) 5
> SUNIONCARD 2 key1 key2 LIMIT 3
(integer) 3

Details

Keys that do not exist are considered to be empty sets.

When provided with the optional LIMIT argument (which defaults to 0, meaning unlimited), if the union cardinality reaches limit partway through the computation, the command stops and returns limit as the cardinality. This ensures a significant speedup for queries where the limit is lower than the actual union cardinality. LIMIT works in both exact and approximate modes.

Approximate cardinality

By default, SUNIONCARD returns the exact union cardinality. With the APPROX option, it instead uses HyperLogLog internally to estimate the cardinality with a standard error of about 0.81%, without materializing the full union. This is useful for very large unions, where computing an exact count is expensive in both time and memory.

Redis Software and Redis Cloud compatibility

Redis
Software
Redis
Cloud
Notes
❌ Standard
❌ Active-Active
❌ Standard
❌ Active-Active

Return information

Integer reply: the number of elements in the resulting union.

See also

SUNION | SUNIONSTORE

RATE THIS PAGE
Back to top ↑