3

Is there any command in Redis to get multiple key's values in a single query?

Actually, My Keys are all SETS so I want to get all of their value but as MEMBERS only take one KEY in argument, is this possible in a single query.

6
  • What do you worry about multiple queries? Commented Jan 22, 2019 at 13:16
  • Suppose I need values of 5000 keys for example, now I need to fire 5000 redis queries, don't you think it would be much better if the result would have come in just a single query. Commented Jan 22, 2019 at 15:28
  • 1
    It's a bad idea to get so many data in a single query. Because Redis is single-threaded when processing the request. If you try to get members of 5000 sets in a single query, it will block Redis for a long time, and other client won't be able to read from or write to Redis. Also it will make Redis to take too much memory to prepare this huge response. Commented Jan 23, 2019 at 1:54
  • @for_stack never thought about it, then what can be an optimal solution firing 100 queries may be 50 times? Will it be better? Commented Jan 23, 2019 at 7:58
  • As @Supermacy mentioned in his answer, there's no such command. I still don't think it's a good idea to get all members of multiple sets. In fact, it's also a bad idea to get all members of a single set, if the set is very big. Instead, you might need SSCAN command. Commented Jan 23, 2019 at 13:31

3 Answers 3

2

You cannot get the value of the multiple sets in one query. You have to query the database multiple times.
However, you can do operation which involves multiple sets using single query. The commands for this type of operations are:

  • SDIFF- Returns the members of the set resulting from the difference between the first set and all the successive sets.

  • SINTER- Returns the members of the set resulting from the intersection of all the given sets.

  • SUNION- Returns the members of the set resulting from the union of all the given sets.
Sign up to request clarification or add additional context in comments.

Comments

2

I think following docs may help you: http://redis.io/commands/sunion

I faced such problem and found this ability of Redis. If you need only values and don't need to know the key for value it is what you need.

I checked perfomance for more than 667 keys. And there are results below:

enter image description here

Method 1 is 667 sequential requests

Method 2 is 667 concurrent requests

Method 3 is using sunion

Comments

0

Not technically a single 'query'.

If you don't want a union, and you just want to save round-trip time, use pipelining. For example, transactions like-

multi
   smembers mykey1
   smembers mykey2
exec

Redis clients have pipeline mechanism, where all the queries will be queued in your application buffer and will be sent to redis server at once.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.