0
0
Redisquery~5 mins

SUNIONSTORE for storing results in Redis

Choose your learning style9 modes available
Introduction
SUNIONSTORE helps you combine multiple sets into one and save the combined result for later use.
You want to merge friends lists from different groups into one list.
You need to find all unique tags from several articles and keep them together.
You want to combine different product categories into one big category.
You want to save the combined result of multiple sets for quick future access.
Syntax
Redis
SUNIONSTORE destination key [key ...]
The command combines all members from the given sets (keys) into one set stored at the destination key.
If the destination key exists, it will be overwritten with the new combined set.
Examples
Combine sets 'friends1' and 'friends2' into a new set 'all_friends'.
Redis
SUNIONSTORE all_friends friends1 friends2
Merge three sets of tags into one set called 'combined_tags'.
Redis
SUNIONSTORE combined_tags tags1 tags2 tags3
Sample Program
First, we add members to two sets 'friends1' and 'friends2'. Then, we combine them into 'all_friends'. Finally, we get all members of 'all_friends'.
Redis
SADD friends1 Alice Bob
SADD friends2 Bob Charlie
SUNIONSTORE all_friends friends1 friends2
SMEMBERS all_friends
OutputSuccess
Important Notes
SUNIONSTORE returns the number of elements in the resulting set stored at the destination.
The order of elements in the resulting set is not guaranteed.
If any source key does not exist, it is treated as an empty set.
Summary
SUNIONSTORE merges multiple sets into one and saves the result.
It overwrites the destination set if it already exists.
Useful for combining related data and keeping it ready for quick access.