SUNIONSTORE for storing results in Redis - Time & Space Complexity
We want to understand how the time needed to combine sets grows when using SUNIONSTORE in Redis.
Specifically, how does the work change as the sets get bigger?
Analyze the time complexity of the following code snippet.
# Combine two sets and store the result
SUNIONSTORE resultSet set1 set2
# Retrieve the combined set
SMEMBERS resultSet
This code takes two sets, merges their unique members into a new set, and stores it under a new key.
Look for repeated actions that take time.
- Primary operation: Scanning all members of each input set to combine them.
- How many times: Once for each member in each input set.
As the sets get bigger, the work grows roughly with the total number of unique members.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 to 20 operations |
| 100 | About 100 to 200 operations |
| 1000 | About 1000 to 2000 operations |
Pattern observation: The work grows roughly in direct proportion to the total number of elements in the input sets combined.
Time Complexity: O(n)
This means the time to combine sets grows linearly with the total number of elements in all input sets.
[X] Wrong: "SUNIONSTORE runs instantly no matter how big the sets are."
[OK] Correct: The command must look at every element in the input sets to combine them, so bigger sets take more time.
Understanding how set operations scale helps you reason about performance in real applications using Redis sets.
"What if we used SUNIONSTORE with three or more sets instead of two? How would the time complexity change?"
Practice
SUNIONSTORE destination key1 key2 do?Solution
Step 1: Understand SUNIONSTORE purpose
The command merges multiple sets into one combined set.Step 2: Identify storage behavior
The result is stored in the specified destination key, overwriting if it exists.Final Answer:
It merges the sets stored at key1 and key2 and stores the result in destination. -> Option AQuick Check:
SUNIONSTORE merges and stores = C [OK]
- Confusing SUNIONSTORE with SUNION (which does not store)
- Thinking it deletes original sets
- Assuming it copies only one set
setA and setB into resultSet using Redis?Solution
Step 1: Recall SUNIONSTORE syntax
The correct syntax isSUNIONSTORE destination key1 key2 ....Step 2: Match keys to syntax
Here,resultSetis destination, followed by setssetAandsetB.Final Answer:
SUNIONSTORE resultSet setA setB -> Option BQuick Check:
Destination first, then sets = A [OK]
- Placing destination key last
- Using SUNION instead of SUNIONSTORE for storing
- Mixing order of keys
setX = {1, 2, 3}setY = {3, 4, 5}setZ = {5, 6}You want to create a new set
combined that contains the union of all three sets. Which command correctly does this and stores the result in combined?Solution
Step 1: Recall SUNIONSTORE usage for multiple sets
SUNIONSTORE can take multiple keys after the destination key.Step 2: Verify correct order and keys
Destination iscombined, followed bysetX,setY, andsetZ.Final Answer:
SUNIONSTORE combined setX setY setZ -> Option AQuick Check:
Destination first, then all sets = B [OK]
- Using SUNION instead of SUNIONSTORE to store
- Placing destination key last
- Omitting one or more sets
SADD set1 a b cSADD set2 b c dWhat will be the members of
resultSet after executing SUNIONSTORE resultSet set1 set2?Solution
Step 1: Identify members of each set
set1has {a, b, c},set2has {b, c, d}.Step 2: Compute union of both sets
The union is {a, b, c, d} combining all unique members.Final Answer:
["a", "b", "c", "d"] -> Option CQuick Check:
Union of set1 and set2 = ["a", "b", "c", "d"] [OK]
- Forgetting to include all unique elements
- Confusing union with intersection
- Assuming order matters in sets
SUNIONSTORE result set1 set2 but get an error. What is the most likely cause?Solution
Step 1: Understand data type requirements
SUNIONSTORE requires the destination key to be a set or not exist.Step 2: Identify error cause
Ifresultexists as a string or other type, Redis throws a type error.Final Answer:
The key result already exists as a string, not a set. -> Option DQuick Check:
Destination key type mismatch causes error [OK]
- Assuming missing keys cause errors (they don't)
- Thinking SUNIONSTORE limits number of sets
- Ignoring key data types
Ready to prove it?
Pass the test and this pattern moves unknown
