文档
Visual Explain — 一眼读懂计划
颜色编码的 stage cards、cost 比率 chips 和 per-stage 字形,让一个计划中最差的 stage 一眼可见,无需读取 JSON。
它显示什么
每个 aggregation/find 计划呈现为 stage cards 的树。每个 card 携带 4 个视觉信号:左侧 4px 彩色 accent 条(good/warn/bad 等级)、识别 stage 类型的字形(IXSCAN 用 Key 图标、COLLSCAN 用 Warning、SORT 用 SortAscending 等)、当 docsExamined 可用时带 examined/returned 比率的 chip,以及 IXSCAN 系列 stages 的 index 名称 chip。
如何打开
- 在 collection 上打开任何 workspace tab(Documents、Aggregation 等)。
- 在 Query Bar 中输入您的查询或在 Aggregation 构建器中构建 pipeline。
- 点击 "Explain"(右上角)。计划以树形打开。
- 在任意 cost 比率 chip 上 hover 以查看该 stage 获得其等级的原因(全 collection 扫描、低选择性、占主导地位的时间份额等)。
Cost 等级
NoSqlStudio 将每个 stage 评级为 good(高效)、warn(可能浪费)或 bad(肯定错误)— 按优先级顺序应用,首次匹配胜出:
| 颜色 | 含义 | 触发器 |
|---|---|---|
| 🟢 绿色 | 高效 stage | IXSCAN / EXPRESS_IXSCAN / IDHACK / FETCH |
| 🟡 黄色 | 可能浪费 — 审查 | 小 COLLSCAN、内存中 SORT、低索引选择性(<10%)、stage 占总时间 >30% |
| 🔴 红色 | 肯定错误 — 发布前修复 | >1000 文档的 COLLSCAN、内存中 SORT 超过 32MB 阈值 |
读取 cost 比率 chip
chip 显示 "examined → returned · X%" — 实际返回的检查文档百分比。经验法则:
- > 50%:索引为此 query 形状良好。
- 10–50%:边界情况 — 对临时查询可能可接受;为热路径重建索引。
- < 10% 且 examined > 100:warn — 索引形状错误(缺少 composite 索引、排序顺序错误等)。
实战示例
针对没有 composite 索引的 1M 文档 orders collection 运行此查询:
db.orders.find({ status: 'paid', userId: ObjectId('...') })您会在计划底部看到一个红色 COLLSCAN card("Full collection scan over >1000 documents — likely missing an index")。在 { status: 1, userId: 1 } 上创建一个 composite 索引并重新运行 — 计划现在显示一个 99% cost 比率的绿色 IXSCAN。