DBAtlas · Database operations atlas

Inspect a query plan with real execution statistics (explain executionStats)

MongoDBPerformance

Command

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 } } }])

What it does

Runs the query and shows the winning plan (IXSCAN/COLLSCAN, SORT stage), the number of returned documents, examined index keys and documents, and the time taken.

When to use it

When analyzing a query that appears slow in the profiler or log, and when verifying that a new index is actually used.

Caution

executionStats really runs the query; explaining a heavy query at peak time adds load too. For write commands (update/delete/findAndModify) explain does not modify data but does run the plan. A high totalDocsExamined/nReturned ratio or an in-memory SORT stage in the plan means the index is not suitable.

Open in DBAtlas →

Related