Skip to content

Query Performance

explain()

Ask the planner what it did. The first tool when a query is slow.

IntermediateAbout 6 minutes

A slow query is a measurement problem first. `explain()` shows the plan and, with executionStats, how many keys and documents the server actually touched. Do not add an index until you have seen this output. 'I would add an index' without numbers is the junior answer.

db.orders.find({ status: "delivered" }).explain("executionStats")
executionStats runs the query and counts. queryPlanner only picks a plan.
winningPlan.stage: COLLSCAN
nReturned: 40
totalKeysExamined: 0
totalDocsExamined: 800
executionTimeMillis: 12
Read these four numbers and the winning stage. Ignore the rest until the next lesson.
FieldMeaning
nReturnedDocuments you got back
totalDocsExaminedDocuments the server opened
totalKeysExaminedIndex keys walked (0 on a collection scan)
executionTimeMillisWall time for this run — noisy, still useful as before/after

`queryPlanner` — cheapest: winning plan, no execution counts. `executionStats` — the default for a slow query. `allPlansExecution` — how the trial of competing plans went; later lesson. In Node: collection.find(filter).explain('executionStats'). The ratio that matters is examined / returned. Close to 1 is healthy. 800 examined for 40 returned is a scan or a useless index.

Interview question

A query is slow. How do you diagnose it?

Think about it first.

Practice

These are the filters you would explain first.