Redis rdb文件内存分析
需要安装Python2.7:
yum -y install zlib*
wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
tar -zxvf Python-2.7.9.tgz
cd Python-2.7.9
./configure --prefix=/usr/local/python2.7
make && make install
ln -s /usr/local/python2.7/bin/python2.7 /usr/bin/python2.7
wget https://bootstrap.pypa.io/get-pip.py
python2.7 get-pip.py
ln -s /usr/local/python2.7/bin/pip2.7 /usr/bin/pip2.7
pip2.7 install redis
pip2.7 install rdbtools
pip2.7 install python-lzf
从rds下载rdb备份文件:这里为a.rdb
rdb -c memory a.rdb >memory.csv (rdb命令的完整路径:/usr/local/python2.7/bin/rdb)
sed -i 's/,$//' memory.csv
sed -i 1d memory.csv
wget https://www.sqlite.org/2020/sqlite-autoconf-3310100.tar.gz
$tar xvfz sqlite-autoconf-3310100.tar.gz
$cd sqlite-autoconf-3310100
$./configure --prefix=/usr/local
$make
$make install
sqlite3 memory.db
sqlite> create table memory(database int,type varchar(128),key varchar(128),size_in_bytes int,encoding varchar(128),num_elements int,len_largest_element varchar(128));
sqlite>.mode csv memory
注意:如果执行.mode csv memory报错了,请执行 .mode csv
sqlite>.import memory.csv memory
使用redis-rdb-tools分析内存快照
将数据导入SQLite数据库后,可根据需要使用SQL语句进行分析,部分分析方式的示例如下。
查询内存中的key总数:
sqlite>select count(*) from memory;
查询内存占用总量:
sqlite>select sum(size_in_bytes) from memory;
查询内存占用量最高的10个key:
sqlite>select * from memory order by size_in_bytes desc limit 10;
查询元素数量1000以上的list:
sqlite>select * from memory where type='list' and num_elements > 1000;