0

I want to get key matching pattern.

ex) Test:*

I consider Keys command. but Its time complexity is too long.

ex) keys Test: * => It occurs overhead.

My Redis version is 2.6.16

There is another way?

sona
  • 109
  • 1
  • 3
  • 11
  • Can you explain what you need this for, so we can provide you a better answer? `keys Test:*` is the way to get keys with your prefix, but if you tell us what you need them for, we can give you alternative ways of doing what you want. – Eli Jul 15 '14 at 13:48
  • After I get keys matching pattern, I wanted to delete specific keys. and now I found easy way. http://stackoverflow.com/questions/4006324/how-to-atomically-delete-keys-matching-a-pattern-using-redis Thank you for your advice. – sona Jul 16 '14 at 02:54

2 Answers2

6

Please upgrade to latest Redis version (2.8.13) and use the SCAN command instead that allows you to do the same as KEYS but without blocking the server. Also it is a good idea to think at alternative designs not involving full-scan of the key space. For example the new ZRANGEBYLEX command is able to perform lexicographic range queries on sorted sets.

Scan command documentation: http://redis.io/commands/scan

antirez
  • 18,314
  • 5
  • 50
  • 44
2

It depends on the problem you are trying to solve. I do something similar where I keep a SET of the keys I am interested in and first read that SET with smembers then read the data for those keys. As far as search keys after the fact, you are already doing it as fast as redis can.

Of course you can update your code like I describe, generate that list via keys Test:* and add each to the existing SET.

sberry
  • 128,281
  • 18
  • 138
  • 165