How to determine the size of a single key in Redis

2019-08-10 12:31发布

How can I know the size (in KB) of a particular key in ?
I'm aware of info memory command, but it gives combined size of Redis instance, not for a single key.

5条回答
smile是对你的礼貌
2楼-- · 2019-08-10 13:02

You currently (v2.8.23 & v3.0.5) can't.

The serializedlength from DEBUG OBJECT (as suggested by @Kumar) is not indicative of the value's true size in RAM - Redis employs multiple "tricks" to save on RAM on the one hand and on the other hand you also need to account for the data structure's overhead (and perhaps some of Redis' global dictionary as well).

The good news is that there has been talk on the topic in the OSS project and it is likely that in the future memory introspection will be greatly improved.

Note: I started (and stopped for the time being) a series on the topic - here's the 1st part: https://redislabs.com/blog/redis-ram-ramifications-i

查看更多
Deceive 欺骗
3楼-- · 2019-08-10 13:03

I know this is an old question, but, just for the record, Redis implemented a memory usage <key> command since version 4.0.0.

The output is the amount of bytes required to store the key in RAM.

Reference: https://redis.io/commands/memory-usage

查看更多
混吃等死
4楼-- · 2019-08-10 13:12

If you just want to get the length of a key (string): STRLEN

查看更多
别忘想泡老子
5楼-- · 2019-08-10 13:17

Why not try

APPEND {your-key} ""

This will append nothing to the existing value but return the current length.

查看更多
Summer. ? 凉城
6楼-- · 2019-08-10 13:20

DEBUG OBJECT <key> reveals something like the serializedlength of key, which was in fact something I was looking for... For a whole database you need to aggregate all values for KEYS * which shouldn't be too dfficult with a scripting language of your choice... The bad thing is that redis.io doesn't really have a lot of information about DEBUG OBJECT.

查看更多
登录 后发表回答