liveBook · Manning

Query plan cache

As explained in MongoDB 8.0 in Action, Third Edition

A query plan cache is a database-maintained cache of execution plans associated with query shapes or operations.

A query plan cache stores a selected execution plan for a query shape so MongoDB can reuse a strategy instead of repeatedly evaluating every available option. During a trial phase, the query planner compares plans using actual query conditions and favors the plan that retrieves results efficiently with minimal work. section 7.1

Each query shape can occupy one of three cache states:

  • Missing: no entry exists; MongoDB evaluates candidate plans, selects a winner, and creates an Inactive entry recording its work value. section 7.1
  • Inactive: the query shape is recognized, but MongoDB reevaluates plans when queried again; an equally or more efficient plan can replace it as Active. section 7.1
  • Active: the winning plan is currently used; performance degradation or increased work can cause reassessment and movement back to Inactive. section 7.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
)

Inspect a query’s chosen plan, execution statistics, or all candidate plans with MongoDB’s explain() verbosity modes. section 7.1.1

Use $planCacheStats to inspect a collection’s plan-cache information. The cache is purged after mongod restarts or shutdowns and after catalog operations such as dropping indexes or collections. Entries can also be cleared for a collection with PlanCache.clear() or selectively with PlanCache.clearPlansByQuery(). section 7.1.2 section 7.1.1

Inspecting a cached-plan candidate

Run db.collection.find({ yourField: "value" }).explain("executionStats") to see the selected plan and runtime statistics. An IXSCAN can locate matching index keys, while FETCH retrieves the corresponding documents; the output reports returned documents, execution time, examined keys, and examined documents. section 7.1.1

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains trial selection, cache states, purges, and inspection commands, but does not detail the planner’s internal scoring algorithm.
Examples
Uses mongosh explain() commands, IXSCAN and FETCH stages, and PlanCache clearing operations as operational examples.
What it assumes
Assumes familiarity with MongoDB queries and collections while defining indexes, query planning, execution stages, and cache states.
What you can do after
Diagnose query efficiency with explain() and inspect, clear, or interpret plan-cache entries operationally.
How it connects
Connects query planning with indexes, collection scans, explain(), execution statistics, and catalog operations.

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.