SUNIONSTORE

Introduction and Use Case(s)

The SUNIONSTORE command in Redis is used to perform a union of multiple sets and store the result in a new set. This is particularly useful when you need to combine different groups of elements into a single collection without duplicates and reuse it later.

Syntax

  1. SUNIONSTORE destination key [key ...]

Parameter Explanations

  • destination: The key where the resulting set will be stored.
  • key: One or more keys representing the sets to be unioned.

Return Values

  • (Integer): The number of elements in the resulting set.

Example:

  1. (integer) 3

Code Examples

  1. dragonfly> SADD set1 "a" "b" "c"
  2. (integer) 3
  3. dragonfly> SADD set2 "c" "d" "e"
  4. (integer) 3
  5. dragonfly> SUNIONSTORE resultset set1 set2
  6. (integer) 5
  7. dragonfly> SMEMBERS resultset
  8. 1) "a"
  9. 2) "b"
  10. 3) "c"
  11. 4) "d"
  12. 5) "e"

Best Practices

  • Ensure that the destination key is different from the input keys to avoid overwriting data unintentionally.

Common Mistakes

  • Trying to use SUNIONSTORE with non-set keys will result in an error. Make sure all specified keys point to sets.

FAQs

What happens if one or more of the input keys do not exist?

If any of the input keys do not exist, they are treated as empty sets and the union operation proceeds with the existing sets.

Can I use SUNIONSTORE with only one set?

Yes, but it’s redundant. Using SUNIONSTORE with a single set simply copies the set to the destination.