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
下载软件包:

epel-release-8-11.el8.noarch.rpm                                                                          394 kB/s |  24 kB     00:00    
----------------------------------------------------------------------------------------------------------------
总计                                                                                                      389 kB/s |  24 kB     00:00     

已安装:
  epel-release-8-11.el8.noarch                                                                                                            

完毕!

3、安装python-pip

[root soft]# wget https://bootstrap.pypa.io/get-pip.py
--2021-08-10 09:23:20--  https://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|:443... 已连接。
已发出 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: http://mirrors.cloud.aliyuncs.com/pypi/simple/
Collecting pip
  Downloading http://mirrors.cloud.aliyuncs.com/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 http://mirrors.cloud.aliyuncs.com/pypi/packages/65/63/39d04c74222770ed1589c0eaba06c05891801219272420b40311cd60c880/wheel-0.36.2-py2.py3-none-any.whl (35 kB)
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: http://mirrors.cloud.aliyuncs.com/pypi/simple/
Collecting rdbtools
  Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/dd/e5/eebb748863d74b7f9d433e359b874d97e830e9d1b3e3d876b91b5cba6b9d/rdbtools-0.1.15.tar.gz (31 kB)
Collecting redis
  Downloading http://mirrors.cloud.aliyuncs.com/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: http://mirrors.cloud.aliyuncs.com/pypi/simple/
Collecting python-lzf
  Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/e3/33/b8f67bbe695ccc39f868ae55378993a7bde1357a0567803a80467c8ce1a4/python-lzf-0.2.4.tar.gz (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

可以看到方法一、二的结果是一致的。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM