The Redis SUNIONSTORE command stores the union of the given sets in the specified destination set. If the destination set already exists, it will be overwritten.

Syntax

The basic syntax of the SUNIONSTORE command is as follows:

1
SUNIONSTORE destination key [key ...]

This command is equal to SUNION, but instead of returning the resulting set, it is stored in destination.

If destination already exists, it is overwritten.

Available Since

Redis version >= 1.0.0

Time Complexity

O(N) where N is the total number of elements in all given sets.

ACL Categories

@write, @set, @slow

Return Value

Returns the number of elements in the resulting set.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
127.0.0.1:6379> SADD key1 a b c d
(integer) 4
127.0.0.1:6379> SADD key2 c
(integer) 1
127.0.0.1:6379> SADD key3 a c e
(integer) 3
127.0.0.1:6379> SUNIONSTORE key key1 key2 key3
(integer) 5
127.0.0.1:6379> SMEMBERS key
1) "b"
2) "d"
3) "e"
4) "c"
5) "a"
127.0.0.1:6379>