如何查看数据库大 Key
更新时间:2025-08-15 03:41:38
功能概述
Redis 提供了 List、Hash、Zset 等复杂类型的数据结构,用户在使用 Redis 时可能由于 Key 设计不合理导致某个 Key 过大,致使读取、删除这些数据的操作花费过多的时间,从而对业务产生一定影响。另外,若单个热 Key 过大,容易造成对于单个 Key 的操作占用内存、CPU、网络带宽等资源过高,影响其他 Key 操作。
同时在集群模式下,大 Key 的产生容易导致某个子节点的内存溢出。因此,我们需要找出大 Key,并对内存结构进行优化。
本文主要介绍如何使用 redis-cli 的 --bigkeys 选项查找大 Key。
操作步骤
步骤一:安装 Redis 客户端
安装步骤可参见如何访问 Redis 数据库中的安装步骤。
| 说明 |
|---|
|
步骤二:查找大 Key
-
登录已安装 redis-cli 的服务器。
-
执行如下命令查找大 Key。
redis-cli -h <redis_instance_address> -a <password> -p <port> --bigkeys参数说明
参数 说明 <redis_instance_address>
表示 Redis Standalone 实例的 IP 地址,请根据实际地址替换。
<port>
表示 Redis Standalone 实例的端口号,默认为
6379。<password>
表示 Redis Standalone 实例的认证密码。
-
命令行执行成功后,系统显示类似如下信息。
$ redis-cli -h 192.168.*.* -a ****** -p 6379 --bigkeys # Scanning the entire keyspace to find biggest keys as well as # average sizes per key type. You can use -i 0.1 to sleep 0.1 sec # per 100 SCAN commands (not usually needed). [00.00%] Biggest string found so far 'key-419' with 3 bytes [05.14%] Biggest list found so far 'mylist' with 100004 items [35.77%] Biggest string found so far 'counter:__rand_int__' with 6 bytes [73.91%] Biggest hash found so far 'myobject' with 3 fields -------- summary ------- Sampled 506 keys in the keyspace! Total key length in bytes is 3452 (avg len 6.82) Biggest string found 'counter:__rand_int__' has 6 bytes Biggest list found 'mylist' has 100004 items Biggest hash found 'myobject' has 3 fields 504 strings with 1403 bytes (99.60% of keys, avg size 2.78) 1 lists with 100004 items (00.20% of keys, avg size 100004.00) 0 sets with 0 members (00.00% of keys, avg size 0.00) 1 hashs with 3 fields (00.20% of keys, avg size 3.00) 0 zsets with 0 members (00.00% of keys, avg size 0.00)