StackExchange.Redis icon indicating copy to clipboard operation
StackExchange.Redis copied to clipboard

Question: Get multiple hash keys

Open aberbegall opened this issue 8 years ago • 2 comments

Having the following hash entries

myHash:1 
                  fieldA:valueA
                  fieldB:valueAA
myHash:2
                  fieldA:valueB
                  fieldB:valueBB
myHash:3
                  fieldA:valueC
                  fieldB:valueCC

What is the best/efficient way to retrieve all keys matching myHash and each fields and values?

Should I use HashScan() and then HashGetAllAsync() for each HashEntry? If yes, what arguments should I pass to the HashScan() method?

Would it be better to keep each of myHash:# in a separated SET collection and get all its members to avoid HashScan() call?

aberbegall avatar Jan 07 '18 19:01 aberbegall

There is literally no good way to get all keys matching a pattern. The KEYS and SCAN commands should not be used routinely - they are mainly intended for tools like database analysis sets.

Usually, your best bet would be to put the keys of the hashes: in a set. Then you can iterated that outer set. There is also a way to use the SORT command to perform joins via key patterns, but I don't know whether this supports hashes.

On 7 Jan 2018 7:40 p.m., "Agustin Berbegall" [email protected] wrote:

Having the following hash entries

myHash:1 fieldA:valueA myHash:2 fieldA:valueB myHash:3 fieldA:valueC

What is the best/efficient way to retrieve all keys matching "myHash" and each fields and values?

Should I use HashScan() and then HashGetAllAsync() for each HashEntry? If yes, what argument should I pass to HashScan()?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/StackExchange/StackExchange.Redis/issues/765, or mute the thread https://github.com/notifications/unsubscribe-auth/AABDsFD2kks-90ZD9Fj2m6ZlaNY569O2ks5tIR26gaJpZM4RVwju .

mgravell avatar Jan 07 '18 20:01 mgravell

@mgravell I will go with the Set solution as you suggested. Thanks for your quick reply

aberbegall avatar Jan 07 '18 22:01 aberbegall