99

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?

Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
shanks
  • 1,847
  • 2
  • 15
  • 17

5 Answers5

87

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.

warvariuc
  • 57,116
  • 41
  • 173
  • 227
81

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.

x_maras
  • 2,177
  • 1
  • 25
  • 34
  • That's exactly what I needed ! Thanks – E. Bavoux Jun 06 '18 at 08:37
  • 39
    And what if one has millions of keys? – kivagant Sep 26 '18 at 11:36
  • 4
    That's an old answer. I should actually add a disclaimer here. So do not use this in a production setup with millions of keys. Better use the SCAN cursor with MATCH – x_maras Sep 26 '18 at 13:55
  • 3
    You should never run this command on production, this is the blocking command and if you run this command all request will starve and may get timeout if this command runs longer. – Arpit Agrawal Jul 22 '19 at 08:40
  • Thanks! I noticed as well and added a disclaimer on top, a year ago. When I answered 6+ years ago I didn't consider that someone might run this on production. – x_maras Jul 22 '19 at 08:52
  • @x_maras why only put a text disclaimer, I guess you could add the command related (`./redis-cli --scan --pattern "abc:*" | wc -l`), couldn't you? Or point to an answer giving this. – Ulysse BN Dec 11 '19 at 00:25
  • Will this be advisable to do when the keys expire? for example, im trying to get the number of active users by storing to redis. that key has expiration. will it still affect the production? Thanks – JSTR Oct 14 '21 at 11:50
  • Hey, what if we need select database? – Pham Hung May 25 '22 at 02:30
33

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

mushuweasel
  • 674
  • 7
  • 7
  • 1
    Your answer is perfect but need to explain in details eg. Your redis keys 127.0.0.1:6379> keys abc* 1) "abc123" 2) "abc456" 3) "abc234" Now try to access this: From the command line, redis-cli --scan --pattern 'abc*' | wc -l Response below: localhost@username~$ redis-cli --scan --pattern 'abc*' | wc -l 3 – Pankaj Chauhan Aug 29 '18 at 05:41
  • 2
    This should be the accepted answer. Requires redis with version >= 2.8.0 – Clintm Feb 11 '19 at 17:58
  • 1
    Do I understand correctly that all matching records are fetched to the client? – warvariuc Dec 08 '21 at 06:59
22

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"
Joshua
  • 689
  • 7
  • 16
  • 4
    How would you use scan to count keys? – hasen May 25 '17 at 03:49
  • Either your can read official doc: https://redis.io/commands/scan. Or google it: https://stackoverflow.com/questions/33166812/redis-scan-count-how-to-force-scan-to-return-all-keys-matching-a-pattern – Joshua May 25 '17 at 13:59
  • 15
    Well, Googling took me here :) It would be helpful if you could edit your answer to include a simple command using `scan` for counting the number of keys matching a pattern. – hasen May 26 '17 at 00:44
  • okay, I take your suggestion. :-) It might be easier for the others too. – Joshua May 26 '17 at 08:45
  • 2
    @Joshua Looking at scan api does not look like you can find the total number of count in one attempt. – Viren Oct 25 '17 at 12:51
  • 1
    If you want to count keys using SCAN, you'll need to store the keys on each iteration, and use your preferred method of filtering out duplicates (SCAN does not guarantee values will only appear once), probably in a data structure that allows fast lookup of existing values. For giant sets of keys, this will be impractical since you'll need to store all keys in memory until the SCAN is complete, so you'll need to consider either keeping a secondary key as a counter which you update on SET/DEL, or storing your data in a type that allows easy counting (e.g. in a Redis hash). – philraj Aug 20 '18 at 18:02
16

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"
ben
  • 488
  • 3
  • 11