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