Skip to main content

SUNIONCARD

Get set union cardinality.
2 min read

Use SUNIONCARD to count the distinct members that appear in at least one of the given sets. It returns only the count; use SUNION to retrieve the members.

Members shared by multiple sets count once. Keys that do not exist count as empty sets, and the command returns 0 if all keys are missing. It does not modify the source sets or store the union.

LIMIT stops counting once that many distinct members have been found and returns the limit. Omitting LIMIT or using LIMIT 0 returns the full count.

Syntax#

Arguments#

ArgumentRequiredRepeatableDescription
<numkeys>YesNoPositive integer specifying how many key arguments follow.
<key>YesYesSet key to include in the union.
APPROXNoNoEstimate the union cardinality using HyperLogLog instead of counting exactly.
LIMIT <limit>NoNoNon-negative integer that caps the returned count; 0 means no limit.

Important points#

  • numkeys must equal the number of key arguments that immediately follow it.
  • The count is exact unless APPROX is specified.
  • APPROX uses a temporary HyperLogLog to reduce the memory needed to track distinct members. The inputs are ordinary sets, and the result may differ from the exact cardinality.
  • APPROX and LIMIT can appear in either order after the keys. With both options, counting can stop when the estimate reaches the limit, and the returned value never exceeds that limit.

Response#

The number of distinct members in the union, or an estimate when APPROX is specified. A positive LIMIT caps the returned value in either mode.

ProtocolReply
RESP2Integer
RESP3Integer

Examples#

TCP examples use the TLS REDIS_URL from the Upstash console. Client examples query set1 and set2 from the Redis CLI example.

Redis CLI

The union contains a, b, c, d, and e. The shared member c counts once.

To estimate the count, optionally capped at a limit:

@upstash/redis
Note

This command is not supported yet in @upstash/redis.

upstash_redis
Note

This command is not supported yet in upstash_redis.

ioredis
node-redis
redis-py
go-redis
jedis
redis-rs