DBAtlas · 数据库运维图谱

用真实执行统计检查查询计划(explain executionStats)

MongoDB性能

命令

db.musteri.find({ sehir: "Ankara", durum: "aktif" }).sort({ kayitTarihi: -1 }).explain("executionStats")
// Summary: winning plan, returned documents, examined keys/documents and time
const e = db.musteri.find({ sehir: "Ankara", durum: "aktif" }).sort({ kayitTarihi: -1 }).explain("executionStats");
({ plan: e.queryPlanner.winningPlan, n: e.executionStats.nReturned, keys: e.executionStats.totalKeysExamined, docs: e.executionStats.totalDocsExamined, ms: e.executionStats.executionTimeMillis })
// For an aggregation:
db.musteri.explain("executionStats").aggregate([{ $match: { sehir: "Ankara" } }, { $group: { _id: "$durum", n: { $sum: 1 } } }])

作用

执行查询并显示胜出的计划(IXSCAN/COLLSCAN、SORT 阶段)、返回的文档数、检查的索引键和文档数以及耗时。

适用场景

分析在 profiler 或日志中显示为慢的查询,以及验证新索引是否真正被使用时。

注意

executionStats 会真正执行查询;在高峰时段 explain 重型查询也会增加负载。对于写命令(update/delete/findAndModify),explain 不修改数据,但会执行计划。totalDocsExamined/nReturned 比值高或计划中出现内存 SORT 阶段,说明索引不合适。

在 DBAtlas 中打开 →

相关内容