How to count the number of keys matching a pattern?

108.9k views Asked by At

How can I find the count of all the keys that has a matching pattern.

For example, there are two keys abc:random-text-1 and abc:random-text-2 . The common pattern here isabc: . So, here the count is 2.

How can I do this in redis?

5

There are 5 answers

8
x_maras On BEST ANSWER

DISCLAIMER I hope this old answer haven't damaged any production systems, with millions of keys. If you still want to still count the matching keys of redis in production for some reason, better use scan with a match pattern.

If you simply search with KEYS, with your redis client, you will get a number list of all you matching keys, right?

e.g.

KEYS abc:*

will give you

1) abc:random-text-1
2) abc:random-text-2

or you can run the following:

./redis-cli KEYS "abc:*" | wc -l

and you will get 2 as an output.

0
ben On

If it's a one-time thing, you can use KEYS as described by x_maras, but you shouldn't use that in your code since KEYS will scan every key in the entire database each time it's called.

If you want to do it frequently, there is no "good" way exactly as you've written because it will always be fairly inefficient to scan every key (even using SCAN, since it would be doing the same thing as KEYS just in a safer manner).

However, if the patterns you need are known ahead of time, you can keep a set of every key that matches the pattern.

SET abc:random-text-1 "blah"
SADD patterns:abc abc:randomtext-1

SET abc:random-text-2 "more blah"
SADD patterns:abc abc:randomtext-2

SCARD patterns:abc
// (integer) 2

SORT patterns:abc BY nosort GET *
// 1) "blah"
// 2) "more blah"
7
warvariuc On

From here:

eval "return #redis.pcall('keys', 'abc:*')" 0

It's not O(1), but at least the count is done on the server side.

3
mushuweasel On

From the command line, redis-cli --scan --pattern 'abc:*' | wc -l

6
Joshua On

By considering the performance, I would not recommend you use 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 sets.

I would suggest you considering scan, if your redis version > 2.8.0. But it rely on which data type you are going to use.

Here is an simple example from redis doc:

redis 127.0.0.1:6379> sadd myset 1 2 3 foo foobar feelsgood
(integer) 6
redis 127.0.0.1:6379> sscan myset 0 match f*
1) "0"
2) 1) "foo"
   2) "feelsgood"
   3) "foobar"