liveBook · Manning

Execution plan

As explained in MongoDB 8.0 in Action, Third Edition

A query execution plan is the database engine's strategy and sequence of operations for processing a query and returning its results.

An execution plan is MongoDB’s strategy and sequence of operations for processing a query and returning results. The query planner compares available strategies, uses indexes when suitable, and selects a winning plan that balances speed and resource use. Without a suitable index, it may perform a full collection scan, examining every document. section 7.1

Read an execution plan by checking its planning behavior and operation stages:

  • Inspect the plan tree: lower stages access collections or indexes, middle stages process retrieved data, and the top stage returns results. section 7.1.1
  • Look for index-backed stages such as IXSCAN followed by FETCH, rather than an inefficient full collection scan. section 7.1 section 7.1.1
  • Use execution statistics to compare returned documents, examined keys, examined documents, and execution time. section 7.1.1
  • Remember that MongoDB tests available strategies during a trial phase and caches the selected plan for a query shape; cached entries can be missing, inactive, or active. section 7.1

Inspecting a query

Run db.collection.find({ yourField: "value" }).explain("executionStats") to see the chosen plan and its runtime measurements. A typical result uses IXSCAN on yourField_1, passes matching keys to FETCH, returns one document, examines one key and document, and reports executionTimeMillis. section 7.1.1

Listing 7.1 Appending the MongoDB explain () method
db.collection.find(
    { yourField: "value" }
).explain() // Explain the default query planner information

db.collection.find(
    { yourField: "value" }
).explain(
    "executionStats" // Explain the query execution statistics
)

db.collection.find(
    { yourField: "value" }
).explain(
    "allPlansExecution" // Explain the query execution
➥plan with all possible query plans
)

Compare queryPlanner, executionStats, and allPlansExecution modes through copyable mongosh commands. section 7.1.1

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains plan selection, caching, execution stages, and runtime statistics, but does not detail planner internals beyond available output.
Examples
Uses mongosh explain commands and an IXSCAN-to-FETCH plan with execution statistics.
What you can do after
Diagnose query efficiency with db.collection.explain(), cursor.explain(), and selected verbosity modes.
How it connects
Connects query planner, indexes, full collection scans, plan cache, and query execution engines.

Also explained in

Compare how 4 books explain execution plan →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.