/apps/svr/redis-2.8.19/bin/redis-cli -p 6288 --bigkeys
从库直接执行,后台是使用scan命令的
# /apps/svr/redis/bin/redis-cli -p 6382 --bigkeys -i 0.1 # Scanning the entire keyspace to find biggest keys as well as # average sizes per key type. You can use -i 0.1 to sleep 0.1 sec # per 100 SCAN commands (not usually needed). [00.00%] Biggest hash found so far 'user:001' with 2 fields [00.00%] Biggest string found so far 'k4' with 1 bytes [00.00%] Biggest string found so far 'k1' with 3 bytes -------- summary ------- Sampled 6 keys in the keyspace! Total key length in bytes is 18 (avg len 3.00) Biggest string found 'k1' has 3 bytes Biggest hash found 'user:001' has 2 fields 5 strings with 7 bytes (83.33% of keys, avg size 1.40) 0 lists with 0 items (00.00% of keys, avg size 0.00) 0 sets with 0 members (00.00% of keys, avg size 0.00) 1 hashs with 2 fields (16.67% of keys, avg size 2.00) 0 zsets with 0 members (00.00% of keys, avg size 0.00)
案例:取样查看,较快(在从库查看!)
/apps/svr/redis-2.8.19/bin/redis-cli -h 10.20.54.17 -p 6379 --bigkeys

从中可以看到BaseItem201601和BaseItem201512所占用的内存是最多的,所有有充分的理由怀疑这两个key不正常,但是这两个key具体有多大?有几种方式
1、投机取巧法:将该库的rdb文件在一个测试机器上恢复过来,然后删掉BaseItem201601看内存减少多少,减少的内存即为该KEY大小
2、初略估算法:可以先取出Hash表中的一个元素

至文本文件中,可以得到一个文件的元素大小为,而该列表总共有3056237个元素,所以可以初略估算出总大小为11G左右
3、工具法:使用redis-memory-for-key工具,可以计算单独一个Key 的大小,但是线上没有安装此工具。需要自行安装
全扫操作,较慢(在key数量特别多或者特别大的时候运行会比较慢,建议在从库跑,如果存在Hash列表等情况,不一定能正确反映内存的实际大小情况)
#!/usr/bin/env bash export PATH=/apps/svr/redis-2.8.17/bin/:$PATH # 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
                    
                
                
            
        
浙公网安备 33010602011771号