Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating through keys in Redis

Tags:

redis

I have just started with Redis. My DB contains about 1 billion records. Using HKEYS * results in an out of memory error.

Is there a way to iterate through keys? Something like HKEYS * but with a limit n?

Edit:

I am now using a loop which matches a pattern

for c in '1234567890abcedf':
    r.keys(c + '*')
like image 737
Prav Avatar asked Feb 02 '12 12:02

Prav


1 Answers

Available since Redis 2.8.0 are the cursor based Redis iteration commands (SCAN, HSCAN etc) that let you iterate efficiently over billions of keys. For your specific case, the start using HSCAN instead of HKEYS/HGETALL. It is efficient, cheap on server resources and scales very well. You can even add a pattern to HSCAN unlike HKEYS.

e.g.

127.0.0.1:6379> HMSET hash0 key0 value0 key1 value1 entry0 data0 entry1 data1
OK
127.0.0.1:6379> HSCAN hash0 0 MATCH key* 
1) "0"
2) 1) "key0"
   2) "value0"
   3) "key1"
   4) "value1"
127.0.0.1:6379> HSCAN hash0 0 
1) "0"
2) 1) "key0"
   2) "value0"
   3) "key1"
   4) "value1"
   5) "entry0"
   6) "data0"
   7) "entry1"
   8) "data1"
like image 120
Vaibhaw Avatar answered Oct 20 '22 02:10

Vaibhaw