1.Choosing the right explain() verbosity
explain("queryPlanner") — the default — shows the winning plan's shape with no execution. explain("executionStats") actually runs the query and adds real numbers for the winner. explain("allPlansExecution") adds those same real numbers for every candidate, useful specifically when the winner's choice itself is in question.
Key features
- queryPlanner (default): plan shape only, zero execution — cheapest, answers "what would run"
- executionStats: actually executes the winning plan — adds nReturned, totalKeysExamined, totalDocsExamined, executionTimeMillis
- allPlansExecution: executes every candidate's trial, not just the winner — the only mode that populates full rejectedPlans stats
- Higher verbosity costs more real execution time — allPlansExecution runs every candidate, not just one
- None of the three modes read from or write to the query plan cache
Verbosity → what it costs → what it answers
- queryPlanner: no execution, plan shape only — cheapest — "what would run"
- executionStats: no execution, real numbers — runs the winner — real numbers
- allPlansExecution: executes every candidate, real numbers — runs every candidate — "why did this win"
Command
db.<collection>.find({...}).explain(<"queryPlanner" | "executionStats" | "allPlansExecution">)queryPlanner: plan shape, no execution. executionStats: runs the winner, real numbers. allPlansExecution: runs every candidate — reserve it for "why did this plan win" questions specifically, since it costs the most.