liveBook · Manning

executionStats

As explained in MongoDB 8.0 in Action, Third Edition

executionStats is an explain verbosity mode that reports the selected query plan together with query execution statistics.

executionStats is an explain() verbosity mode that combines the selected query plan with statistics from query execution. Use it to diagnose inefficient queries and verify whether an index is helping. section 7.1.1

The output helps connect the plan to observed work:

  • queryPlanner describes the winning plan; executionStats adds returned-document counts, execution time, keys examined, and documents examined. section 7.1.1
  • A winningPlan commonly forms a stage tree: IXSCAN reads matching index keys, FETCH retrieves documents, and the top stage produces results. section 7.1.1
  • Compare examined keys and documents with nReturned to assess how selectively the query is being served. section 7.1.1
  • Use executionStats after creating an index to validate actual use, including indexed filtering and sorting. section 7.2.1

Inspect a movie query

Run db.movies.find({ "runtime": 100 }).explain("executionStats") after creating runtime_1. The output can show an IXSCAN followed by FETCH, nReturned: 1, executionTimeMillis: 3, totalKeysExamined: 1, and totalDocsExamined: 1. section 7.2.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 the default queryPlanner mode, executionStats, and allPlansExecution directly on the same query. section 7.1.1

Listing 7.2 Execution-plan output structure
{
  "winningPlan": {
    "stage": "FETCH",
    "inputStage": {
      "stage": "IXSCAN",
      "indexName": "yourField_1",
      "keyPattern": {
        "yourField": 1
      },
      "indexBounds": {
        "yourField": [
          "[\"value\", \"value\"]"
        ]
      }
    }
  },
  "executionStats": {
    "nReturned": 1,
    "executionTimeMillis": 3,
    "totalKeysExamined": 1,
    "totalDocsExamined": 1
  }
}

Inspect the compact relationship between winningPlan stages and executionStats values. section 7.1.1

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Combines plan-stage interpretation with execution metrics, but stops short of presenting complete explain output.
Examples
Uses movie runtime queries, runtime_1, filtered sorting, and compact execution-plan JSON.
What it assumes
Assumes basic MongoDB queries and indexes while defining explain verbosity modes and interpreting their output.
What you can do after
Diagnose/debug with it by comparing returned results, execution time, examined keys, examined documents, and winning stages.
How it connects
Connects explain(), query planning, indexes, IXSCAN, FETCH, and plan caching.

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.