Skip to main content
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

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.

Examples

TCP examples use the TLS REDIS_URL from the Upstash console. Client examples query set1 and set2 from the Redis CLI example.
The union contains a, b, c, d, and e. The shared member c counts once.To estimate the count, optionally capped at a limit:
This command is not supported yet in @upstash/redis.
This command is not supported yet in upstash_redis.

Related topics

Changelog