如何找到 Redis 中的较大的 Key?
2024-11-30 by dongnan
问题描述
Redis 服务报警,使用 MEMORY STATS
查看内存统计信息:
MEMORY STATS
结果如下
*52
$14
peak.allocated
:1762869432
$15
total.allocated
:1750957064
$17
省略...
keys.count
:2445
$18
keys.bytes-per-key
:713143
省略...
内存使用情况:
- 峰值分配内存:1762869432 字节。
- 当前分配内存:1750957064 字节。
- 数据集内存占用:约 1.71 GB,占总内存的 98.05%。
Key 分布:
- 总 Key 数:2445。
- 平均每 Key 占用内存:713143 字节。
内存碎片:
- 总碎片率:1.0176,有轻微的内存碎片。
环境描述
- OS: Ubuntu Server 20.04 LTS
- redis-cli: 5.0.7
redis-cli
redis-cli 是 redis 命令行下的接口工具,用于与 redis 服务端交互,常用命令参考这里 。
找到 Redis 中的较大的 Key
使用 redis-cli --bigkeys
这个工具会扫描所有 Key,统计每种类型的最大 Key:
redis-cli -h r-8vb7xxxro.redis.xxx.rds.aliyuncs.com -a xxxx --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 'hdxxxk:ORDER_CONCENTRATE-C2409180001' with 1 bytes
[00.41%] Biggest zset found so far '20xxx_iplog' with 1 members
[00.41%] Biggest string found so far 'sys_dict:stock_change_type' with 641 bytes
[00.85%] Biggest string found so far 'sys_xxt:affairs_status' with 681 bytes
[00.85%] Biggest list found so far 'sigxxx_to_access:client:kongtiantian' with 1 items
[03.46%] Biggest zset found so far '2021-11-03_iplog' with 2 members
[07.24%] Biggest string found so far 'shiro:cache:com.alsace.framework.common.shiro.JwtRealm.authenticationCache:eyJsAxxx8DqWpYg' with 766 bytes
[11.47%] Biggest string found so far 'sys_dict:xxx' with 1610 bytes
[47.84%] Biggest zset found so far '2020-07-02_iplog' with 4 members
[48.70%] Biggest string found so far 'sys_dict:oxxxision' with 3244 bytes
[94.30%] Biggest list found so far 'QIxxx_Log' with 1222638 items
-------- summary -------
Sampled 2457 keys in the keyspace!
Total key length in bytes is 106710 (avg len 43.43)
Biggest string found 'sys_dict:overdue_decision' has 3244 bytes
Biggest list found 'QIMINGXING_request_Log' has 1222638 items
Biggest zset found '2020-07-02_iplog' has 4 members
2405 strings with 44266 bytes (97.88% of keys, avg size 18.41)
5 lists with 1222642 items (00.20% of keys, avg size 244528.40)
0 sets with 0 members (00.00% of keys, avg size 0.00)
0 hashs with 0 fields (00.00% of keys, avg size 0.00)
47 zsets with 67 members (01.91% of keys, avg size 1.43)
0 streams with 0 entries (00.00% of keys, avg size 0.00)
清理 redis 所有的 key (可选)
注意:以下两个命令会立即删除数据,且无法恢复,请确保操作前备份重要数据。
清空当前数据库(默认 DB 0):
redis-cli FLUSHDB
清空所有数据库中的 Key(适用于多个数据库):
redis-cli FLUSHALL
参考
- ChatGPT