58

I'm planning to start using hashes insead of regular keys. But I can't find any information about multi get for hash-keys in Redis wiki. Is this kind of command is supported by Redis?

Thank you.

Kirzilla
  • 16,368
  • 26
  • 84
  • 129

5 Answers5

65

You can query hashes or any keys in pipeline, i.e. in one request to your redis instance. Actual implementation depends on your client, but with redis-py it'd look like this:

pipe = conn.pipeline()
pipe.hgetall('foo')
pipe.hgetall('bar')
pipe.hgetall('zar')
hash1, hash2, hash3 = pipe.execute()

Client will issue one request with 3 commands. This is the same technique that is used to add multiple values to a set at once.

Read more at http://redis.io/topics/pipelining

kmerenkov
  • 2,859
  • 1
  • 19
  • 7
12

No MHGETALL but you can Lua it:

local r = {}
for _, v in pairs(KEYS) do
  r[#r+1] = redis.call('HGETALL', v)
end

return r
Itamar Haber
  • 47,336
  • 7
  • 91
  • 117
11

If SORT let you use multiple GETs with the -> syntax, and all your hashes had the same fields, you could get them in a bulk reply by putting their names into a set and sorting that.

  SORT names_of_hashes GET *->field1 *->field2 *->field3 *->etc

But it doesn't look like you can do that with the hash access. Plus you'd have to turn the return list back into hashes yourself.

UPDATE: Redis seems to let you fetch multiple fields if you name your hashes nicely:

redis> hset hash:1 name fish
(integer) 1
redis> hset hash:2 name donkey
(integer) 1
redis> hset hash:3 name horse
(integer) 1
redis> hset hash:1 type fish
(integer) 1
redis> hset hash:2 type mammal
(integer) 1
redis> hset hash:3 type mammal
(integer) 1
redis> sadd animals 1
(integer) 1
redis> sadd animals 2
(integer) 1
redis> sadd animals 3
(integer) 1
redis> sort animals get # get hash:*->name get hash:*->type
1. "1"
2. "fish"
3. "fish"
4. "2"
5. "donkey"
6. "mammal"
7. "3"
8. "horse"
9. "mammal"
rjp
  • 1,942
  • 12
  • 14
  • This is an excellent answer; `SORT` is hellishly fast, even though it does a lot of "stuff", but it does it on the server without any client round-trips. Even piped commands, if you send a lot, end up being slower than `SORT`. Perhaps the only way to beat it is to write a Lua script that contains your complex query completely. – blitter Dec 28 '19 at 20:56
-1

There is no command to do it on one shot, but there is a way to do it "nicely", using a list (or sorted set) where you would store you hashKeys, and then retrieve them as bulk using multi.

In PHP:

$redis->zAdd("myHashzSet", 1, "myHashKey:1");
$redis->zAdd("myHashzSet", 2, "myHashKey:2");
$redis->zAdd("myHashzSet", 3, "myHashKey:3");

$members = $redis->zRange("myHashzSet", 0, -1);
$redis->multi();
foreach($members as $hashKey) {
    $redis->hGetAll($hashKey);
}
$results = $redis->exec();

I recommand using a sorted set, where you use the score as an ID for your hash, it allows to take advantages of all score based command.

Mikushi
  • 3,311
  • 4
  • 19
  • 22
-3

Redis has a HMGET command, which returns the values of several hash keys with one command.

MyGGaN
  • 1,766
  • 3
  • 30
  • 41
Fabian Jakobs
  • 28,815
  • 8
  • 42
  • 39
  • 4
    Yeah, but HMGET will return multiple values of concrete fields, but I need return whole hashes (like HGETALL do http://code.google.com/p/redis/wiki/HgetallCommand). Anyway, thank you for your answer. – Kirzilla Aug 10 '10 at 10:59
  • 1
    Bump! What do you mean by whole hashes ? With `HMGET` you have all the values for the queried keys, and since you've got the keys already, and each key has a corresponding value in order, you could build a local hash with every key/value pair in your code. Please explain. Thank you. – Niloct Nov 26 '11 at 00:34
  • 5
    If a hash is conceptually used to hold a user, then HMGET would get you for example the username and the password of the single user with the specified ID. An equivalent method analogous to MGET for hashes would, given a set of IDs, get the usernames and passwords of all of those users, all in one go. That is the difference. – majelbstoat Apr 06 '12 at 16:43
  • Hard to know what OP was asking for sure, but this probably wasn't it. Judging by the upvotes to the answers above, most people landing here want to know if there's a Redis command that returns multiple, full Redis hashes. There's not. – kylebebak Apr 29 '21 at 18:26