Redis如何查看指定key的⼤⼩(即所占内存空间⼤⼩)
⽅式⼀:利⽤rdbtools⼯具的redis-memory-for-key命令即可查看的单个key的详细信息
安装该⼯具需要有python的pip包管理,所以先安装环境:
1、安装gcc
[root soft]# yum install gcc
上次元数据过期检查:2:22:05 前,执⾏于 2021年08⽉10⽇星期⼆ 07时00分30秒。
软件包 gcc-8.3.1-5.1.el8.x86_64 已安装。
依赖关系解决。
================================================================================================================
软件包架构版本仓库⼤⼩
================================================================================================================
升级:
cpp              x86_64            8.4.1-1.el8      AppStream        10 M
gcc      x86_64    8.4.1-1.el8 AppStream  23 M
libgcc    x86_64    8.4.1-1.el8 BaseOS  78 k
libgomp    x86_64    8.4.1-1.el8 BaseOS  204 k
事务概要
================================================================================================================
升级  4 软件包
总下载:34 M
确定吗?[y/N]: y
下载软件包:
(1/4): libgcc-8.4.1-1.el8.x86_64.rpm                          9.3 MB/s |  78 kB    00:00
(2/4): libgomp-8.4.1-1.el8.x86_64.rpm                      18 MB/s | 204 kB    00:00
(3/4): cpp-8.4.1-1.el8.x86_64.rpm                            71 MB/s |  10 MB    00:00
(4/4): gcc-8.4.1-1.el8.x86_64.rpm                            86 MB/s |  23 MB    00:00
----------------------------------------------------------------------------------------------------------------
总计                                                                                                      124 MB/s |  34 MB    00:00
已升级:
cpp-8.4.1-1.el8.x86_64          gcc-8.4.1-1.el8.x86_64          libgcc-8.4.1-1.el8.x86_64          libgomp-8.4.1-1.el8.x86_64
完毕!
2、安装epel-release
[root soft]# yum -y install epel-release
上次元数据过期检查:2:22:21 前,执⾏于 2021年08⽉10⽇星期⼆ 07时00分30秒。
依赖关系解决。
================================================================================================================
软件包架构版本仓库⼤⼩
================================================================================================================
安装:
epel-release      noarch          8-11.el8          extras          24 k
事务概要
================================================================================================================
安装  1 软件包
总下载:24 k
安装⼤⼩:35 k
下载软件包:
arch.rpm                                                                          394 kB/s |  24 kB    00:00
----------------------------------------------------------------------------------------------------------------
总计                                                                                                      389 kB/s |  24 kB    00:00
已安装:
arch
完毕!
3、安装python-pip
[root soft]# wget bootstrap.pypa.io/get-pip.py
--2021-08-10 09:23:20--  bootstrap.pypa.io/get-pip.py
正在解析主机 bootstrap.pypa.io (bootstrap.pypa.io)... 151.101.72.175, 2a04:4e42:36::175
正在连接 bootstrap.pypa.io (bootstrap.pypa.io)|151.101.72.175|: 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1957374 (1.9M) [text/x-python]
正在保存⾄: “get-pip.py”
get-pip.py                        100%[==============================================================>]  1.87M  1.53MB/s  ⽤时 1.2s
2021-08-10 09:23:24 (1.53 MB/s) - 已保存 “get-pip.py” [1957374/1957374])
[root soft]# python3 get-pip.py
/usr/local/lib/python3.6/site-packages/setuptools/dist.py:645: UserWarning: Usage of dash-separated 'index-url' will not be supported in future versions. Please use the underscore name 'index_url' instead  % (opt, underscore_opt))
Looking in indexes: mirrors.cloud.aliyuncs/pypi/simple/
Collecting pip
Downloading mirrors.cloud.aliyuncs/pypi/packages/8a/d7/f505e91e2cdea53cfcf51f4ac478a8cd64fb0bc1042629cedde20d9a6a9b/pip-21.2.2-py3-none-any.whl (1.6 MB)
|████████████████████████████████| 1.6 MB 5.4 MB/s
Collecting wheel
Downloading mirrors.cloud.aliyuncs/pypi/packages/65/63/39d04c74222770ed1589c0eaba06c05891801219272420b40311cd60c880/wheel-0.36.2-py2.py3-none-any.whl (35 kB)
bootstrap 软件Installing collected packages: wheel, pip
/usr/local/lib/python3.6/site-packages/setuptools/dist.py:645: UserWarning: Usage of dash-separated 'index-url' will not be supported in future versions. Please use the underscore name 'index_url' instead  % (opt, underscore_opt))
Attempting uninstall: pip
Found existing installation: pip 9.0.3
Uninstalling pip-9.0.3:
Successfully uninstalled pip-9.0.3
/usr/local/lib/python3.6/site-packages/setuptools/dist.py:645: UserWarning: Usage of dash-separated 'index-url' will not be supported in future versions. Please use the underscore name 'index_url' instead  % (opt, underscore_opt))
/usr/local/lib/python3.6/site-packages/setuptools/dist.py:645: UserWarning: Usage of dash-separated 'index-url' will not be supported in future versions. Please use the underscore name 'index_url' instead  % (opt, underscore_opt))
Successfully installed pip-21.2.2 wheel-0.36.2
4、安装rdbtools
[root soft]# pip install rdbtools
Looking in indexes: mirrors.cloud.aliyuncs/pypi/simple/
Collecting rdbtools
Downloading mirrors.cloud.aliyuncs/pypi/packages/dd/e5/eebb748863d74b7f9d433e359b874d97e830e9d1b3e3d876b91b5cba6b9d/rdbtools-0.1. (31 kB)
Collecting redis
Downloading mirrors.cloud.aliyuncs/pypi/packages/a7/7c/24fb0511df653cf1a5d938d8f5d19802a88cef255706fdda242ff97e91b7/redis-3.5.3-py2.py3-none-any.whl (72 kB)
|████████████████████████████████| 72 kB 19.0 MB/s
Building wheels for collected packages: rdbtools
Building wheel for rdbtools (setup.py) ... done
Created wheel for rdbtools: filename=rdbtools-0.1.15-py3-none-any.whl size=30803 sha256=727a896a0b411082eaba11e32639e47878916c0ba02033735b56d23355f18bae
Stored in directory: /root/.cache/pip/wheels/e1/56/98/39e6102d53d12925474430db32176f475e35fcbe99f38610fd
Successfully built rdbtools
Installing collected packages: redis, rdbtools
Successfully installed rdbtools-0.1.15 redis-3.5.3
5、安装python-lzf
[root 6379]# pip install python-lzf
Looking in indexes: mirrors.cloud.aliyuncs/pypi/simple/
Collecting python-lzf
Downloading mirrors.cloud.aliyuncs/pypi/packages/e3/33/b8f67bbe695ccc39f868ae553789
93a7bde1357a0567803a80467c8ce1a4/python-lzf-0.2. (9.3 kB)
Building wheels for collected packages: python-lzf
Building wheel for python-lzf (setup.py) ... done
Created wheel for python-lzf: filename=python_lzf-0.2.4-cp36-cp36m-linux_x86_64.whl size=23789 sha256=330ced9eba75883deb5f91d76762618a718ba9f07ab39a5602e384820104fe26
Stored in directory: /root/.cache/pip/wheels/d4/c0/da/4ee70acb90d7d539ff99029f3c807e41b7b6f8e90b73a2b973
Successfully built python-lzf
Installing collected packages: python-lzf
Successfully installed python-lzf-0.2.4
6、到redis的rdb⽂件,根据rdb⽂件解析出key占⽤内存情况
[root redis]# cd /var/lib/redis/6379
[root 6379]# ls
dump.rdb
[root 6379]# rdb -c memory dump.rdb > /root/result.csv
vim /root/result.csv
数据库  key类型 key 占⽤内存(bytes)
database,type,key,size_in_bytes,encoding,num_elements,len_largest_element,expiry
0,string,11111111111111111111111111111111-1111111111111111111111111111111111111111111111111111111111111111,152,string,8,8,
0,string,22222222222222222222222222222222-2222222222222222222222222222222222222222222222222222222222222222,152,string,8,8,
0,string,33333333333333333333333333333333-3333333333333333333333333333333333333333333333333333333333333333,152,string,8,8,
说明:
database:数据库编号
type:数据类型
key:key
size_in_bytes:以字节为单位使⽤的内存(内存使⽤包括键、值和任何其他开销。)
encoding:RDB编码类型
7、⚠ 注意如果⽣成的结果⽂件中key数量与Redis中不⼀致,这是因为rdb⽂件还未同步导致的,可以在redis-cli客户端执⾏save同步⼀次rdb再⽣成结果⽂件。
⽅式⼆:使⽤Redis4.0.0版本提供的 MEMORY USAGE 命令
Redis MEMORY USAGE 命令给出⼀个 key 和它值在 RAM 中占⽤的字节数。返回的结果是 key 的值以及为管理该 key 分配的内存总字节数。对于嵌套数据类型,可以使⽤选项 SAMPLES,其中 COUNT 表⽰抽样的元素个数,默认值为 5。当需要抽样所有元素时,使⽤ SAMPLES 0。
1、MEMORY USAGE 命令格式
MEMORY USAGE key [SAMPLES count]
可⽤版本:>=4.0.0
时间复杂度:O(N),N 为 SAMPLES 的个数
返回使⽤的内存的字节数。
2、使⽤
127.0.0.1:6379> memory usage 11111111111111111111111111111111-1111111111111111111111111111111111111111111111111111111111111111
(integer) 152
可以看到⽅法⼀、⼆的结果是⼀致的。