You have to work really hard to look effortless!
Wechat search public number [long Coding road], together From Zero To Hero!
preface
A Set, or Set, is consistent with the concept of a Set in mathematics, except that Redis sets contain elements of type String. The values of elements in a Set are unique and cannot be duplicated. The previous article introduced operations on a single Set of the Set type. Now let’s take a look at the instructions related to operations on multiple sets.
SINTER
Available versions: >= 1.0.0
Time complexity: O(N*M), where N is the smallest set length of any given set, and M is the number of given sets
The command format
SINTER key [key ...]
Copy the code
Command description
- Returns the intersection of the given set
Such as:
key1 = {a,b,c,d}
key2 = {c}
key3 = {a,c,e}
SINTER key1 key2 key3 = {c}
Copy the code
- When a given set contains an empty set, the result returned must also be an empty set, because the intersection of an empty set with another set must be an empty set
The return value
Array: a list of elements where all collections intersect
The sample
127.0.0.1:6379> sadd myset hello world
(integer) 2
127.0.0.1:6379> sadd otherset hello
(integer) 1
127.0.0.1:6379> sinter myset otherset
1) "hello"
Copy the code
SINTERSTORE
Available versions: >= 1.0.0
Time complexity: O(N*M), where N is the smallest set length of any given set, and M is the number of given sets
The command format
SINTERSTORE destination key [key ...]
Copy the code
Command description
-
The SINTER command is similar to the SINTER function in that it takes the intersection of multiple collections, but instead of returning the result, it saves the result to the destination
-
If the destination set already exists, the data will be overwritten
The return value
Integer value: length of the result set
The sample
#Collection of dest127.0.0.1:6379> sadd dest test (INTEGER) 1 127.0.0.1:6379> smembers dest 1)
#Two sets
127.0.0.1:6379> sadd myset hello world
(integer) 2
127.0.0.1:6379> sadd myset2 hello world lifelmy
(integer) 3
#Save dest after the intersection
127.0.0.1:6379> sinterstore dest myset myset2
(integer) 2
#Dest is the original"test"Is covered
127.0.0.1:6379> smembers dest
1) "hello"
2) "world"
Copy the code
SUNION
Available versions: >= 1.0.0
Time complexity: O(N), N the total number of elements in a given set
The command format
SUNION key [key ...]
Copy the code
Command description
- Returns the union of the given set
Such as:
key1 = {a,b,c,d}
key2 = {c}
key3 = {a,c,e}
SUNION key1 key2 key3 = {a,b,c,d,e}
Copy the code
The return value
Array: A list of elements in the union of all sets
The sample
127.0.0.1:6379> sadd uset hello world
(integer) 2
127.0.0.1:6379> sadd uset1 lifelmy test
(integer) 2
127.0.0.1:6379> sunion uset uset1
1) "lifelmy"
2) "hello"
3) "test"
4) "world"
Copy the code
SUNIONSTORE
Available versions: >= 1.0.0
Time complexity: O(N), N the total number of elements in a given set
The command format
SUNIONSTORE destination key [key ...]
Copy the code
Command description
-
The SUNION command is similar to the SUNION function in that it takes the union of multiple sets, but instead of returning the result, it saves the result to the destination
-
If the destination set already exists, the data will be overwritten
The return value
Integer value: length of the result set
The sample
127.0.0.1:6379> sadd uset hello world
(integer) 2
127.0.0.1:6379> sadd uset1 lifelmy test
(integer) 2
127.0.0.1:6379> sunion uset uset1
1) "lifelmy"
2) "hello"
3) "test"
4) "world"
Copy the code
SDIFF
Available versions: >= 1.0.0
Time complexity: O(N), N the total number of elements in a given set
The command format
SDIFF key [key ...]
Copy the code
Command description
- Returns the set of differences between the first set and all subsequent sets
Such as:
key1 = {a,b,c,d}
key2 = {c}
key3 = {a,c,e}
SDIFF key1 key2 key3 = {b,d}
Copy the code
The return value
Array: A list of the elements of a result set
The sample
127.0.0.1:6379> sadd key1 a b c d
(integer) 4
127.0.0.1:6379> sadd key2 a b
(integer) 2
127.0.0.1:6379> sadd key3 a b c
(integer) 3
#The difference set between the set key1 and the set key2 and key3
127.0.0.1:6379> sdiff key1 key2 key3
1) "d"
Copy the code
SDIFFSTORE
Available versions: >= 1.0.0
Time complexity: O(N), N the total number of elements in a given set
The command format
SDIFFSTORE destination key [key ...]
Copy the code
Command description
-
The SDIFF command is similar to the SDIFF function in that it evaluates the difference set between the first set and the following given set, but instead of returning the result, it saves the result to destination
-
If the destination set already exists, the data will be overwritten
The return value
Integer value: length of the result set
The sample
127.0.0.1:6379> sadd key1 a b c d
(integer) 4
127.0.0.1:6379> sadd key2 a b
(integer) 2
127.0.0.1:6379> sadd key3 a b c
(integer) 3
#After the difference set is taken, the result is saved to the KEY4 set127.0.0.1:6379> sDiffStore key4 key1 key2 key3 (INTEGER) 1 127.0.0.1:6379> smembers key4Copy the code
More and more
Personal blog: lifelmy.github. IO /
Wechat official Account: Long Coding road