0
0
Redisquery~10 mins

SUNIONSTORE for storing results in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - SUNIONSTORE for storing results
Start with multiple sets
SUNIONSTORE command runs
Calculate union of all sets
Store union result in destination set
Return number of elements in destination set
SUNIONSTORE takes multiple sets, finds their union, stores it in a new set, and returns the count of elements stored.
Execution Sample
Redis
SADD set1 a b c
SADD set2 b c d
SUNIONSTORE dest set1 set2
SMEMBERS dest
This code creates two sets, unions them into 'dest', and retrieves all members of 'dest'.
Execution Table
StepCommandActionResultNotes
1SADD set1 a b cAdd elements a,b,c to set13set1 = {a,b,c}
2SADD set2 b c dAdd elements b,c,d to set23set2 = {b,c,d}
3SUNIONSTORE dest set1 set2Union set1 and set2, store in dest4dest = {a,b,c,d}
4SMEMBERS destRetrieve all members of dest[a,b,c,d]Shows union result
5EndNo more commandsExecution complete
💡 All commands executed; union stored in 'dest' with 4 elements.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
set1empty{a,b,c}{a,b,c}{a,b,c}{a,b,c}
set2emptyempty{b,c,d}{b,c,d}{b,c,d}
destemptyemptyempty{a,b,c,d}{a,b,c,d}
Key Moments - 2 Insights
Why does SUNIONSTORE overwrite the destination set instead of adding to it?
SUNIONSTORE replaces the destination set with the union result each time, as shown in step 3 of the execution_table where 'dest' is set to the union of 'set1' and 'set2'. It does not append but overwrites.
What happens if one of the source sets does not exist?
If a source set does not exist, SUNIONSTORE treats it as an empty set. The union will be the union of existing sets only, so the destination will contain elements from existing sets as shown in the concept_flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, how many elements does 'dest' contain after SUNIONSTORE runs?
A4
B3
C2
D5
💡 Hint
Check row 3 in execution_table where SUNIONSTORE stores the union with count 4.
At which step is the 'dest' set created and populated?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in execution_table; union and store happen at step 3.
If 'set2' was empty, what would be the size of 'dest' after SUNIONSTORE?
A0
B4
C3
DDepends on 'set1'
💡 Hint
Refer to variable_tracker and concept_flow: union with empty set equals the other set.
Concept Snapshot
SUNIONSTORE destination key sourcekey [sourcekey ...]
- Computes union of all source sets
- Stores result in destination key (overwrites existing)
- Returns number of elements in destination
- If source set missing, treated as empty
- Use SMEMBERS to view stored union
Full Transcript
SUNIONSTORE command in Redis takes multiple sets, finds their union, and stores the result in a destination set. It overwrites any existing data in the destination. The command returns the number of elements stored. For example, if set1 has {a,b,c} and set2 has {b,c,d}, SUNIONSTORE dest set1 set2 will store {a,b,c,d} in dest. If a source set does not exist, it is treated as empty. You can retrieve the union with SMEMBERS dest. This visual trace showed each step: adding elements to sets, running SUNIONSTORE, and checking the result. Key points include understanding that the destination is overwritten and missing sets count as empty.