How to get Redis Hash Length?

I store objects using hashes in Redis, and I would like to calculate the length of these hashes in terms of Redis.

You can do this easily for strings using STRLEN .

But I just can't find a suitable command for hash data in the documentation . This seems to be the same thing for lists or sets.

Basically, the only solution I found was to get the entire hash with HGETALL and calculate the length to fit the client.

Is something completely out of the box?

If I am mistaken, please do not explain why or give me the appropriate SO links / messages / questions.

Edit :

HLEN is not a solution because it "returns the number of fields contained in the hash." I want to calculate this size for capacity planning and active monitoring in a Redis database.

+6
source share
4 answers

For length, you can use DEBUG OBJECT as disk space, since it returns several information items for each key.

 redis 127.0.0.1:50001> hset myhash field1 'hello' (integer) 1 redis 127.0.0.1:50001> hset myhash field2 'world' (integer) 1 redis 127.0.0.1:50001> DEBUG OBJECT myhash Value at:0x7fb8de4ad590 refcount:1 encoding:zipmap serializedlength:31 lru:696871 lru_seconds_idle:0 

Hope this helps

+7
source

Just use HLEN .

 redis> HSET myhash field1 "Hello" (integer) 1 redis> HSET myhash field2 "World" (integer) 1 redis> HLEN myhash (integer) 2 

EDIT:. It was found that the OP wants a hash disk size for active monitoring. In this case, I would definitely go with a Lua script that calculates the hash size on your server and returns the value back to you. Do not use HGETALL if you expect large hashes because you will need to transfer the entire hash from your server to your client computer, and this will become your bottleneck very quickly. Just doing this calculation on a Redis server using Lua means that you just pass an int from the number of bytes of your network, and not, possibly, mb of data for your entire hash.

+23
source

Depends on what you want to do with the hash length.

If you want the length to perform some diagnostics or monitoring, such as memory searches, I suggest you use it offline with a tool like redis-rdb-tools . The csv dump file will give you statistics about each key, including total size, total memory used, etc.

But if you want the size to implement any function of the application, then there is no ready-made solution. HGETALL plus calculating customer size by length is the way to go. You can optimize it by writing a lua script so that the length calculation happens on the redis server itself.

+4
source

It seems best to use HSCAN when you come across a hash to get the fields and summarize the HSTRLEN for each of them.

0
source

Source: https://habr.com/ru/post/945585/


All Articles