redis-cli 自带两个找大 key 的开关:--bigkeys 与 --memkeys。名字很像,判据不同。这篇在同一份数据上各跑一次,把两者的输出并排,看它们各自给出什么、忽略什么。
[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。
[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 适合找「元素多、可能造成遍历/删除慢」的集合。