SINTERSTORE

Introduction and Use Case(s)

SINTERSTORE is a Redis command used to compute the intersection of multiple sets and store the resulting set in a new key. This is particularly useful when you need to find common elements across several datasets and want to store the result for future use, saving computational overhead.

Syntax

  1. SINTERSTORE destination key [key ...]

Parameter Explanations

  • destination: The key where the result of the set intersection will be stored.
  • key [key …]: One or more keys of the sets to intersect.

Return Values

SINTERSTORE returns the number of elements in the resulting set.

Example Outputs:

  • When there are common elements:

    1. (integer) 2
  • When there are no common elements:

    1. (integer) 0

Code Examples

  1. dragonfly> SADD set1 "a" "b" "c"
  2. (integer) 3
  3. dragonfly> SADD set2 "b" "c" "d"
  4. (integer) 3
  5. dragonfly> SADD set3 "c" "e" "f"
  6. (integer) 3
  7. dragonfly> SINTERSTORE result set1 set2 set3
  8. (integer) 1
  9. dragonfly> SMEMBERS result
  10. 1) "c"

Best Practices

  • Ensure the keys being intersected exist and are of type set to avoid unexpected behavior.
  • Use EXISTS command to check if the destination key already exists, as SINTERSTORE will overwrite any existing key with the same name.

Common Mistakes

  • Using non-set keys with SINTERSTORE will result in an error.
  • Not handling empty intersections can lead to confusion; always check the result count.

FAQs

What happens if one of the keys does not exist?

If one or more keys do not exist, Redis treats them as empty sets, which might affect the intersection result.

Can SINTERSTORE be used with only one set?

Yes, but it is essentially equivalent to copying the set to the destination key.