Print number of keys in Redis
Asked Answered
G

10

240

Is there a way to print the number of keys in Redis?

I am aware of

keys *

But that seems slightly heavy weight. - Given that Redis is a key value store maybe this is the only way to do it. But I would still like to see something along the lines of

count keys *
Greengrocer answered 27/3, 2012 at 11:6 Comment(2)
There's a pull request for COUNT, it got denied though. github.com/antirez/redis/pull/32 antirez also commented on KEYSAnnabell
I wondered if they hadn't supported it as it would be O(n) - guess this confirms it.Greengrocer
M
266

You can issue the INFO command, which returns information and statistics about the server. See here for an example output.

As mentioned in the comments by mVChr, you can use info keyspace directly on the redis-cli.

redis> INFO
# Server
redis_version:6.0.6
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:b63575307aaffe0a
redis_mode:standalone
os:Linux 5.4.0-1017-aws x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:9.3.0
process_id:2854672
run_id:90a5246f10e0aeb6b02cc2765b485d841ffc924e
tcp_port:6379
uptime_in_seconds:2593097
uptime_in_days:30
hz:10
configured_hz:10
lru_clock:4030200
executable:/usr/local/bin/redis-server
Morbid answered 27/3, 2012 at 13:18 Comment(1)
redis-cli INFO Keyspace | grep ^dbHorlacher
I
238

DBSIZE returns the number of keys and it's easier to parse.

Downside: if a key has expired it may still count.

http://redis.io/commands/dbsize

Isaacisaacs answered 27/3, 2012 at 16:44 Comment(1)
In that example, KEYS * evicts the expired key. Also Redis may actively evict some expired keys, but not necessarily all of them.Isaacisaacs
O
77

The DBSIZE command returns the number of keys

> DBSIZE
Overpower answered 16/3, 2018 at 4:55 Comment(0)
A
50

WARNING: Do not run this on a production machine.

On a Linux box:

redis-cli KEYS "*" | wc -l

Note: As mentioned in comments below, this is an O(N) operation, so on a large DB with many keys you should not use this. For smaller deployments, it should be fine.

Alfano answered 13/7, 2012 at 15:56 Comment(8)
Extremely handy, and lets you filter on keys as well.Saunderson
That's an O(n) operation, is there any way to do this in O(1)?Platelayer
Do not use on large database in production environment. KEYS CommandAmundson
Somebody is going to read this, do this on a production box some day without thinking it through and then push it over the edge...probably has already happened.Johannessen
This should have a disclaimer to only use on non-production servers. Otherwise you should use redis.io/commands/SCANFriedcake
can confirm: it has happened.Canea
This answer is outright dangerous and there is no need to use this O(n) operation when INFO KEYSPACE and DBSIZE will do!Nomarchy
I don't mean to offend but this answer is same as what is explained in the question without the wc -l command and the author is asking for an alternative.Tymothy
I
28

Since Redis 2.6, lua is supported, you can get number of wildcard keys like this

eval "return #redis.call('keys', 'prefix-*')" 0

see eval command

Izak answered 15/8, 2013 at 7:10 Comment(2)
Using KEYS to count keys (with or without a prefix) is like throwing the baby out with the bathwater.Sheerness
For Lua newbies: the # in this code is the length operator.Deka
S
13

Go to redis-cli and use below command

info keyspace

It may help someone

Sunder answered 1/12, 2020 at 5:47 Comment(0)
L
12
  1. Displays the DB's name along with keys count:
redis-cli info keyspace
# Keyspace
db0:keys=12995,expires=0,avg_ttl=0
db12:keys=5524396,expires=5,avg_ttl=45201
  1. Display the no. of keys in the selected database:
redis-cli dbsize
(integer) 12995
  1. Displays whole Redis's stats:
redis-cli info
Levitate answered 15/9, 2021 at 6:31 Comment(0)
C
5

dbsize() returns the total number of keys.

You can quickly estimate the number of keys matching a given pattern by sampling keys at random, then checking what fraction of them matches the pattern.

Example in python; counting all keys starting with prefix_:

import redis
r = redis.StrictRedis(host = 'localhost', port=6379)
iter=1000
print 'Approximately', r.dbsize() * float(sum([r.randomkey().startswith('prefix_') for i in xrange(iter)])) / iter

Even iter=100 gives a decent estimate in my case, yet is very fast, compared to keys prefix_.

An improvement is to sample 1000 keys on every request, but keep the total count, so that after two requests you'll divide by 2000, after three requests you'll divide by 3000. Thus, if your application is interested in the total number of matching keys fairly often, then every time it will get closer and closer to the true value.

Cortisone answered 25/4, 2015 at 3:37 Comment(0)
S
0

After Redis 2.6, the result of INFO command are splitted by sections. In the "keyspace" section, there are "keys" and "expired keys" fields to tell how many keys are there.

Shakeup answered 6/6, 2013 at 20:37 Comment(1)
This is not correct. This is a sample output of the section : # Keyspace db0:keys=366,expires=366 Here, 'keys' indicates total no of keys and 'expires' indicates no of keys with expiry set. Essentially it means that they have a ttl set and they are set to expire, not that they have expired.Wail
D
-1
eval "local count = redis.call('scan', 0, 'match', 'key:*:key', 'count', 10000) if count ~= 0 then return #count[2] end " 0

eval "local count = redis.call('sscan', 'key.key:all', 0, 'match', '*', 'count', 1000000) if count ~= 0 then return #count[2] end " 0
Darbies answered 26/10, 2017 at 9:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.