views:

128

answers:

1
+1  Q: 

Querying in redis

Recently I am learning redis and honestly very impressed and dying to use it. One of the things that keep bothering me is "how do I query redis". To be specific I am trying to resolve following

Say I have a millions of hashes stored as below

usage:1 = {created: 20100521, quantity:9, resource:1033, user:1842, ...}
usage:2 = {created: 20100812, quantity:3, resource:7233, user:1842, ...}
usage:3 = {created: 20100927, quantity:4, resource:1031, user:76, ...}

Please note that there are many keys in hashes I have shown only 4. Now that I want to find records in specific date range, by user, by resource or for a user in given period.

I suspect that there are redis specific patterns to retrieve such data. I am a python programmer. I did look at redisco(ohm port) which supports some quering but I am not sure if it does get all the data and then filters in python.

+3  A: 

For Redis, it's best to understand what sort of query patterns you want over your data before you decide how you're going to store it.

For example, if you want to do a date range query over a set of data, you can store that data as a sorted set where the keys are the data items you want to query over, and the score is a unix timestamp.

In your example above, I might store your example hash as:

 user_to_resource:i = user:j                   # key -> value forward map
 resources => (resource:i, created_timestamp)  # sorted set
 count_resource:i = quantity                   # key -> value quantity map

That is, I'd have many forward and reverse maps depending on the query patterns I'd like to support.

rlotun