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#
| Argument | Required | Repeatable | Description |
|---|---|---|---|
<numkeys> | Yes | No | Positive integer specifying how many key arguments follow. |
<key> | Yes | Yes | Set key to include in the union. |
APPROX | No | No | Estimate the union cardinality using HyperLogLog instead of counting exactly. |
LIMIT <limit> | No | No | Non-negative integer that caps the returned count; 0 means no limit. |
Important points#
numkeysmust equal the number of key arguments that immediately follow it.- The count is exact unless
APPROXis specified. APPROXuses 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.APPROXandLIMITcan 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.
| Protocol | Reply |
|---|---|
| RESP2 | Integer |
| RESP3 | Integer |
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
This command is not supported yet in @upstash/redis.
upstash_redis
This command is not supported yet in upstash_redis.