zunionstore
ZUNIONSTORE
Apply for Redis Developer Course
|
Redis Regular Inspection / Technical Support
|
Redis Enterprise Server
|
|---|
Compute the union and store it in a new set
Usage: zunionstore dest_key 2 src_key1 src_key2.
The result set is stored in dest_key. Each member score is added.
src_key1 = { (1,A), (2,B), (3,C) }
src_key2 = { (4,B), (5,C), (6,D) }
dest_key = { (1,A), (6,B),
(8,C), (6,D)}
Example
| Command> | zadd src_key1 1 A 2 B 3 C |
| Result> | 3 |
| Command> | zadd src_key2 4 B 5 C 6 D |
| Result> | 3 |
| Command> | zunionstore dest_key 2 src_key1 src_key2 |
| Result> | 4 |
| Command> | zrange dest_key 0 -1 withscores |
| Result> |
0) 1 -> A 1) 6 -> B 2) 6 -> D 3) 8 -> C |
Using weights
Usage: zunionstore dest_key 2 src_key1 src_key2 weights 2 3.
The first weight 2 is multiplied by each score of src_key1, and the second weight 3 is multiplied by each score of src_key2.
src_key1 = { (1,A), (2,B), (3,C) } -> { (1*2,A), (2*2,B), (3*2,C) }
src_key2 = { (4,B), (5,C), (6,D) } ->
{ (4*3,B), (5*3,C), (6*3,D) }
dest_key = { (2,A),
(4+12,B),
(6+15,C),
(18,D)}
= { (2,A),
(16,B),
(21,C),
(18,D)}
Example
| Command> | zunionstore dest_key 2 src_key1 src_key2 weights 2 3 |
| Result> | 4 |
| Command> | zrange dest_key 0 -1 withscores |
| Result> |
0) 2 -> A 1) 16 -> B 2) 18 -> D 3) 21 -> C |
Using aggregate
Usage: zunionstore dest_key 2 src_key1 src_key2 aggregate sum|min|max.
sum adds the scores. If aggregate is omitted, sum is the default.
min takes the smaller score of each member, and max takes the larger score.
src_key1 = { (1,A), (2,B), (3,C) }
src_key2 = { (4,B), (5,C), (6,D) }
For min dest_key = { (1,A), (2,B),
(3,C), (6,D)}
For max dest_key = { (1,A), (4,B),
(5,C), (6,D)}
Example
| Command> | zunionstore dest_key 2 src_key1 src_key2 aggregate min |
| Result> | 4 |
| Command> | zrange dest_key 0 -1 withscores |
| Result> |
0) 1 -> A 1) 2 -> B 2) 3 -> C 3) 6 -> D |
| Command> | zunionstore dest_key 2 src_key1 src_key2 aggregate max |
| Result> | 4 |
| Command> | zrange dest_key 0 -1 withscores |
| Result> |
0) 1 -> A 1) 4 -> B 2) 5 -> C 3) 6 -> D |
Compute the union of three sets and store it
Usage: zunionstore dest_key 3 src_key1 src_key2 src_key3.
| src_key1 = | { (1,A), | (2,B), | (3,C) | } | |
| src_key2 = | { | (4,B), | (5,C) | (6,D) | } |
| src_key3 = | { | (7,C) | (8,D) | (9,E) } | |
| dest_key = | { (1,A), | (6,B), | (15,C) | (14,D) | (9,E) } |
A source key can also be used as the destination key
ZUNIONSTORE src_key1 2 src_key1 src_key2
The previous contents of src_key1 are overwritten with the result.
Syntax
ZUNIONSTORE dest_key numkeys src_key [src_key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
- Available since version 2.0.0.
- Time complexity is O(N)+O(M log(M)). N is the total number of members in the input sets, and M is the number of members in the result set.
| Related commands | ZINTERSTORE, SINTERSTORE, SUNIONSTORE, SDIFFSTORE |
| Clients for Java | Jedis, Lettuce, Redisson | Clients for C | Hiredis |
| << ZDIFF | ZUNIONSTORE | ZINTERSTORE >> |
|---|
