#pip安装分析工具:
#pip install rdbtools python-lzf
#或者源码安装
git clone https://github.com/sripathikrishnan/redis-rdb-tools
cd redis-rdb-tools
python setup.py install
apt install python-lzf
#分析rdb工具,21G,5200W个key的rdb文件最终生成4G的csv文件
nohup /usr/rdbtools/redis-rdb-tools/rdbtools/cli/rdb.py --command memory dump.rdb > memory.csv &
#或者用以下命令,其中–bytes 1048576 可以限制只输出大于1M以上的key值,减小生成的csv文件大小。
nohup /usr/rdbtools/redis-rdb-tools/rdbtools/cli/rdb.py --command memory --bytes 1048576 dump.rdb > memory.csv &
#如果项目redis中使用到了包含",“的key,csv文件导入数据库时会将key中的”,“当成分隔符,把对应的统计数据插入到错误的字段中去
#有这种情况的话,需要将”,“换成其他符号,下面语句把”,“换成”."
cat memory.csv|awk -F “,” ‘{if(NF>8){key=$3;for (i=4;i<=NF-4;i++){key=key"."$i;} print $1","$2",“key”,"$NF-4","$NF-3","$NF-2","$NF-1","$NF;} else print $0}’ > memoryfinal.csv
#生成csv文件后,要先建表,如果项目redis没有分多个库的话,索引’idx_size’中的database字段可以去掉,提高数据插入速度,或者根据需要自行设定索引,提高查询速度
CREATE TABLE memory
(id
int(11) NOT NULL AUTO_INCREMENT,database
int(2) DEFAULT 0,type
varchar(32) DEFAULT ‘’,key
varchar(32) DEFAULT ‘’,size_in_bytes
int(11) DEFAULT 0,encoding
varchar(32) DEFAULT ‘’,num_elements
int(32) DEFAULT 0,len_largest_element
int(32) DEFAULT 0,expiry
varchar(32) DEFAULT ‘’,
primary key (id
) using btree,
INDEX idx_size
(database
,size_in_bytes
)
) ENGINE=INNODB CHARACTER SET=utf8 ;
#csv文件导入数据表(mysql中执行)
load data infile ‘/usr/redis-4.0.9/data/memoryfinal.csv’
into table memory
fields terminated by ‘,’
lines terminated by ‘\n’ ignore 1 lines
(database
,type
,key
,size_in_bytes
,encoding
,num_elements
,len_largest_element
,expiry
);
#查询占用内容前100个大key
select m.* from memory m join (select id from memory where memory.database=2 order by size_in_bytes desc limit 100) ids on ids.id=m.id;
#查询无过期时间大key
select m.* from memory m join (select id from memory where memory.database=2 and memory.expiry="" order by size_in_bytes desc limit 100) ids on ids.id=m.id;