#!/usr/bin/env bash | |
# This script prints out all of your Redis keys and their size in a human readable format | |
# Copyright 2013 Brent O'Connor | |
# License: http://www.apache.org/licenses/LICENSE-2.0 | |
human_size() { | |
awk -v sum="$1" ' BEGIN {hum[1024^3]="Gb"; hum[1024^2]="Mb"; hum[1024]="Kb"; for (x=1024^3; x>=1024; x/=1024) { if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x]; break; } } if (sum<1024) print "1kb"; } ' | |
} | |
redis_cmd='redis-cli' | |
# get keys and sizes | |
for k in `$redis_cmd keys "*"`; do key_size_bytes=`$redis_cmd debug object $k | perl -wpe 's/^.+serializedlength:([\d]+).+$/$1/g'`; size_key_list="$size_key_list$key_size_bytes $k\n"; done | |
# sort the list | |
sorted_key_list=`echo -e "$size_key_list" | sort -n` | |
# print out the list with human readable sizes | |
echo -e "$sorted_key_list" | while read l; do | |
if [[ -n "$l" ]]; then | |
size=`echo $l | perl -wpe 's/^(\d+).+/$1/g'`; hsize=`human_size "$size"`; key=`echo $l | perl -wpe 's/^\d+(.+)/$1/g'`; printf "%-10s%s\n" "$hsize" "$key"; | |
fi | |
done |
This comment has been minimized.
This comment has been minimized.
To select the db, change the redis_cmd to include the -n # option. |
This comment has been minimized.
This comment has been minimized.
Thanks, this was a nice shortcut when I was troubleshooting some odd performance problems! |
This comment has been minimized.
This comment has been minimized.
Exactly what I was looking for. To make this perfect, sum up the total size of the keys. Thanks again. |
This comment has been minimized.
This comment has been minimized.
Awesome work! |
This comment has been minimized.
This comment has been minimized.
Thanks. It's very helpful! |
This comment has been minimized.
This comment has been minimized.
This saved me some real time today. Thanks for your good work! |
This comment has been minimized.
This comment has been minimized.
But will the 'serializedlength' include the index size(and all other relavant stuffs) redis maintains for that key?? I need the entire memory size consumed by redis for a single key.. |
This comment has been minimized.
This comment has been minimized.
Nice! Thank you. |
This comment has been minimized.
This comment has been minimized.
0.00 Gb ERR unknown command 'debug'
any idea on this error ? |
This comment has been minimized.
This comment has been minimized.
If you use an external Redis hosting you might have the |
This comment has been minimized.
This comment has been minimized.
It's too slow for large keys.... |
This comment has been minimized.
This comment has been minimized.
Nice work! So there's no way to profile on AWS ElasticCache since |
This comment has been minimized.
This comment has been minimized.
@sakovias - Seems correct. http://docs.aws.amazon.com/AmazonElastiCache/latest/UserGuide/ClientConfig.RestrictedCommands.html Only option for AWS ElastiCache is to use a tool that does not rely on debug, perhaps: https://github.com/antirez/redis-sampler |
This comment has been minimized.
This comment has been minimized.
This work for me. Awesome and niubible!!! |
This comment has been minimized.
This comment has been minimized.
How to use with authentication? |
This comment has been minimized.
This comment has been minimized.
guys, you have |
This comment has been minimized.
This comment has been minimized.
FYI: the script doesn't work if you're using Redis on AWS Elasticache, 'redis-cli debug' command not supported. |
This comment has been minimized.
This comment has been minimized.
that's really a bad idea by using this script on product environment. Maybe using |
This comment has been minimized.
This comment has been minimized.
Agree |
This comment has been minimized.
This comment has been minimized.
what does that -e does in echo? |
This comment has been minimized.
This comment has been minimized.
@navi2589, Try running the following and see what it does.
|
This comment has been minimized.
This comment has been minimized.
it doesn't work on ubuntu 18. Just calculate the size by your brain, the serializedsize unit is 'byte', so 1024 = 1k, 1024000 = 1MB |
This comment has been minimized.
Nice work! But, how select DB? Can you help?