*Redis CMS.INFO 命令
CMS.INFO 返回 Count-Min Sketch 的元数据信息(宽度、深度、总计数)。
*语法
CMS.INFO key
*参数说明
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| key | String | 是 | Sketch 的键名 |
*返回值
- Array:返回包含 sketch 元信息的数组,格式为
[width, <宽度值>, depth, <深度值>, count, <总计数值>] - Error:参数错误、键不存在,或键类型错误
*时间复杂度
O(1)
*
*示例
*查看空 sketch 信息
> CMS.INITBYDIM test 2000 5
OK
> CMS.INFO test
1) "width"
2) (integer) 2000
3) "depth"
4) (integer) 5
5) "count"
6) (integer) 0
*查看有数据的 sketch 信息
> CMS.INCRBY test foo 10 bar 42
1) (integer) 10
2) (integer) 42
> CMS.INFO test
1) "width"
2) (integer) 2000
3) "depth"
4) (integer) 5
5) "count"
6) (integer) 52
*检查不存在的 sketch
> CMS.INFO notexist
(error) ERR key does not exist
*常见错误
| 错误 | 原因 | 解决 |
|---|---|---|
| ERR wrong number of arguments | 未提供 key | 提供 sketch 的键名 |
| ERR key does not exist | key 不存在 | 确认 key 已初始化 |
| WRONGTYPE | key 不是 CMS 类型 | 确认 key 正确,或重新初始化 |
*最佳实践
- 合并 sketch 前先用 CMS.INFO 检查各 sketch 的 width 和 depth 是否一致
- count 字段返回的是所有 item 计数的总和,可用于监控 sketch 的数据量
- 结合 width 和 depth 可估算 sketch 的内存占用(约 width × depth × 8 字节)
- 可用于监控 sketch 使用情况和内存占用评估
*FAQ
Q1: count 字段表示什么? A: count 表示 sketch 中所有 item 的计数总和。每次 CMS.INCRBY 增加的增量都会累加到 count 中。
Q2: 如何根据 INFO 结果估算内存占用? A: 内存占用约为 width × depth × 8 字节(每个计数器 64 位)。例如 width=2000, depth=5 时约为 80 KB。
Q3: INFO 中的 width 和 depth 可以修改吗? A: 不能。width 和 depth 在初始化时确定,无法动态修改。如需调整,只能创建新 sketch 并迁移数据。