Rudra
Rudra

Reputation: 1688

Querying REDIS with HMSET

I am using REDIS data store and I have created HMSET like:

HMSET key:1 source 5 target 2
HMSET key:2 source 3 target 1
HMSET key:3 source 3 target 5
HMSET key:4 source 6 target 2
HMSET key:5 source 2 target 3    

Now, I want to query these keys based on the provided source and target list. Suppose, the list of source and target is [2, 3, 6]

I want to have a query like

select from key where source in[2, 3, 6] and traget in[2, 3, 6]

which will give me the result like

key:4 source 6 target 2 
key:5 source 2 target 3

Upvotes: 1

Views: 658

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230441

With a dataset like this (only a few sets), your only option is to iterate them (either in a lua script or by fetching into the app) and do the filtering yourself by inspecting the hashes.

To speed things up, you could maintain the secondary indexes (again, the effort is yours). Something like:

SADD source:3 key:2 key:3
SADD target:2 key:1 key:4

Then you can relatively quickly find all matching keys by issuing SINTERSTORE command

SINTERSTORE found_keys source:2 source:3 source:6 target:2 target:3 target:6

You'll have the keys you seek under the found_keys name.

Although, if you find yourself do this, you should ask yourself: why don't I just give up and use an SQL-capable database, because I clearly want one.

Upvotes: 3

Related Questions