bigywater.tech
Redis · 117

redis-cli --bigkeys

redis-cli 自带两个找大 key 的开关:--bigkeys 与 --memkeys。名字很像,判据不同。这篇在同一份数据上各跑一次,把两者的输出并排,看它们各自给出什么、忽略什么。

元素数 vs 字节数
--bigkeys 按元素个数;--memkeys 按 MEMORY USAGE 的字节数
6784700 字节
--memkeys 找到的最大 key(big:set,15 万成员)
约 7 s
扫描 30 万个 key 的耗时(-i 0.01,含休眠)

--bigkeys

实测 · 本机执行输出
  [00.00%] Biggest string found so far "small:36268" with 20 bytes
  [29.44%] Biggest set    found so far "big:set" with 150000 members
  [30.71%] Biggest hash   found so far "small:hash" with 1 fields
  [38.21%] Biggest string found so far "big:string" with 5242880 bytes
  [48.81%] Biggest hash   found so far "big:hash" with 100000 fields
  [75.83%] Biggest zset   found so far "big:zset" with 50000 members
  [87.31%] Biggest list   found so far "big:list" with 200000 items
  -------- summary -------
  Sampled 300006 keys in the keyspace!
  Biggest   list found "big:list" has 200000 items
  Biggest   hash found "big:hash" has 100000 fields
  Biggest string found "big:string" has 5242880 bytes
  Biggest    set found "big:set" has 150000 members
  Biggest   zset found "big:zset" has 50000 members
  (耗时 7.1 s)

每种类型最大的 key,「大」指元素个数(字符串是字节数)。进度行 [xx.xx%] Biggest … found so far 随扫描更新,最后是 summary。-i 0.01 让它每扫一批就睡一会儿。注意:它只给「每种类型最大的一个」,不列出全部超标的 key。

--memkeys

实测 · 本机执行输出
  [00.00%] Biggest string found so far "small:1648" with 64 bytes
  [29.44%] Biggest set    found so far "big:set" with 6784700 bytes
  [30.71%] Biggest hash   found so far "small:hash" with 41 bytes
  [38.21%] Biggest string found so far "big:string" with 5259293 bytes
  [48.81%] Biggest hash   found so far "big:hash" with 5450727 bytes
  [75.83%] Biggest zset   found so far "big:zset" with 4256515 bytes
  [87.31%] Biggest list   found so far "big:list" with 1700350 bytes
  -------- summary -------
  Sampled 300006 keys in the keyspace!
  Biggest   list found "big:list" has 1700350 bytes
  Biggest   hash found "big:hash" has 5450727 bytes
  Biggest string found "big:string" has 5259293 bytes
  Biggest    set found "big:set" has 6784700 bytes
  Biggest   zset found "big:zset" has 4256515 bytes
  (耗时 7.2 s)

同样的框架,但每个 key 用 MEMORY USAGE 的字节数比较。这个版本上得到的最大的 set 是 big:set(6784700 字节),最大的 hash 是 big:hash(5450727 字节),最大的 list 只有 1700350 字节。

两种口径并排

实测 · 本机执行输出
  big:list     元素数   200000  MEMORY USAGE   1700350
  big:hash     元素数   100000  MEMORY USAGE   5450727
  big:set      元素数   150000  MEMORY USAGE   6784700
  big:zset     元素数    50000  MEMORY USAGE   4256515
  big:string   元素数  5242880  MEMORY USAGE   5259293

元素最多的是 5 MB 的字符串(以字节计)与 20 万元素的 list,但字节数最大的是 15 万成员的 set。找「占内存大」的 key 应该用 --memkeys;--bigkeys 适合找「元素多、可能造成遍历/删除慢」的集合。

分步演示:两个开关

redis-cli --bigkeys
本步的对象

    本文用到的工具与边界

    • 本机 macOS 27 / Redis 8.10.2 独立实例(本机回环端口,save 关闭),redis-cli 风格输出由自写的 RESP 客户端打印;计时用 time.perf_counter,数字每次运行会有小幅抖动。同第 116 篇的数据集;redis-cli 8.10.2 -i 0.01;输出为去掉终端控制符后的文本
    • 没有验证:不带 -i 时对服务器的冲击、--memkeys 的 --memkeys-samples 选项、--bigkeys 的每类型统计(本版本的输出里没有出现)、集群模式下的行为
    京ICP备2026067502号-1京公网安备11010502063487号