问题
How can I know the size (in KB) of a particular key in redis?
I'm aware of info memory
command, but it gives combined size of Redis instance, not for a single key.
回答1:
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
回答2:
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
回答3:
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.
回答4:
If you just want to get the length of a key (string): STRLEN
回答5:
Why not try
APPEND {your-key} ""
This will append nothing to the existing value but return the current length.
来源:https://stackoverflow.com/questions/33735096/how-to-determine-the-size-of-a-single-key-in-redis