47

How I can find keys matching a pattern like this:

Eg:

I have some keys:

abc:parent1

abc:parent2

abc:parent1:child1

abc:parent2:child2

How can I find only

abc:parent1

abc:parent2

Hoang Tuan
  • 507
  • 1
  • 4
  • 7

2 Answers2

53

Keys is specifically noted as a command not to be run in production due to the way it works. What you need here is to create an index of your keys. Use a set for storing the key names of the pattern you want. When you add a new we key, add the name of it to the set. For example:

Set abc:parent1:child1 breakfast
Sadd abc:parent1:index abc:parent1

Then when you need the list:

Smembers abc:parent1:index

Will give you the list, without the penalties and problems associated with using the "evil" keys command. Additionally you would remove an entry with sremove on key deletion. You also get as a benefit the ability to know how many keys are in the index with a single call.

If you absolutely, positively, MUST avoid using an index use SCAN instead of keys. The only time you should even consider keys is if you are running a debug slave where the only process using it is your debugging process.

bdphilly
  • 3
  • 2
The Real Bill
  • 14,884
  • 8
  • 37
  • 39
  • I don't quite understand how this is supposed to work. In your example, should it in reality be: `set abc:parent1:child1 breakfast` and then `sadd abc:parent1 child1`? – Automatico Sep 28 '17 at 11:03
  • No, because the OP only wanted to the level of parent1 not child. – The Real Bill Sep 28 '17 at 13:52
  • I misunderstood the question, thanks for clarifying. – Automatico Sep 28 '17 at 16:44
  • im still confused. OP wants to find "abc:parent1 abc:parent2" shouldnt it be sadd abc abc:parent1 and sadd abc abc:parent2 and smembers abc? – ealeon Mar 27 '18 at 03:40
  • @TheRealBill could you please be more descriptive in your examples? :) I don't really understand it? How to fetch all keys and values with the pattern `abc:parent1` ? – clarkk Aug 30 '18 at 08:23
  • Thanks for sharing. Interesting that "Keys is specifically noted as a command not to be run in production due to the way it works." Would you be able to provide a link to the source that says this? – mc9 Aug 05 '20 at 00:46
  • @SungCho https://redis.io/commands/keys ```Warning: consider KEYS as a command that should only be used in production environments with extreme care. It may ruin performance when it is executed against large databases. This command is intended for debugging and special operations, such as changing your keyspace layout. Don't use KEYS in your regular application code. If you're looking for a way to find keys in a subset of your keyspace, consider using SCAN or sets.``` – snyderxc Jan 26 '22 at 15:36
  • It seems there are some naming confusions in the provided example and more clear version of it can look like this: `SET abc:parent1:child1 breakfast` `SADD abc:parent:index abc:parent1` `SET abc:parent2:child1 dinner` `SADD abc:parent:index abc:parent2` `SMEMBERS abc:parent:index`. Output will be `abc:parent1` and `abc:parent2` – debugger Jun 26 '23 at 06:38
30

Command KEYS pattern will help you for the same, if it is not a production environment. (Never use keys in production)

ex:

redis> MSET one 1 two 2 three 3 four 4
OK
redis> KEYS *o*
1) "two"
2) "one"
3) "four"

For your specific example, the below command will work:

redis 127.0.0.1:6379> keys *parent[0-9]
1) "abc:parent2"
2) "abc:parent1"

Here's the detailed description of the command.

Update: Though the suggestion above helps you get the desired output, the redis KEYS command is evil as the others mentioned. KEYS is blocking and can consume a lot of RAM while preparing the response.

Don't use KEYS in your regular application code. If you're looking for a way to find keys in a subset of your keyspace, consider using SCAN or sets.

Thanks The Real Bill and Itamar, I got a better understanding.

Bivil M Jacob
  • 471
  • 4
  • 9
  • How do you do with `permission:/url1/id/sub:write` and `permission:/url1/id:write` to match only `permission:/url1/[^/]+` – Natim Feb 23 '17 at 10:55
  • 11
    @ItamarHaber `KEYS` is *not* evil. Using it in production code is. As a development tool it's invaluable. – Madbreaks Apr 04 '18 at 17:40