Explain Results
To return information on query plans and execution statistics of thequery plans, MongoDB provides:
- the
db.collection.explain()
method, - the
cursor.explain()
method, and - the
explain
command.
The explain
results present the query plans as a tree of stages.
- "winningPlan" : {
- "stage" : <STAGE1>,
- ...
- "inputStage" : {
- "stage" : <STAGE2>,
- ...
- "inputStage" : {
- "stage" : <STAGE3>,
- ...
- }
- }
- },
Each stage passes its results (i.e. documents or index keys) to theparent node. The leaf nodes access the collection or the indices. Theinternal nodes manipulate the documents or the index keys that resultfrom the child nodes. The root node is the final stage from whichMongoDB derives the result set.
Stages are descriptive of the operation; e.g.
COLLSCAN
for a collection scanIXSCAN
for scanning index keysFETCH
for retrieving documentsSHARD_MERGE
for merging results from shardsSHARDING_FILTER
for filtering out orphan documents from shards
Explain Output
The following sections presents a list of some key fields returned bythe explain
operation.
Note
- The list of fields is not meant to be exhaustive, but is meant tohighlight some key field changes from earlier versions of explain.
- The output format is subject to change between releases.
queryPlanner
queryPlanner
information details the plan selected bythe query optimizer.
- Unsharded Collections
- Sharded Collections
For unsharded collections, explain
returns the followingqueryPlanner
information:
- "queryPlanner" : {
- "plannerVersion" : <int>,
- "namespace" : <string>,
- "indexFilterSet" : <boolean>,
- "parsedQuery" : {
- ...
- },
- "queryHash" : <hexadecimal string>,
- "planCacheKey" : <hexadecimal string>,
- "optimizedPipeline" : <boolean>, // Starting in MongoDB 4.2, only appears if true
- "winningPlan" : {
- "stage" : <STAGE1>,
- ...
- "inputStage" : {
- "stage" : <STAGE2>,
- ...
- "inputStage" : {
- ...
- }
- }
- },
- "rejectedPlans" : [
- <candidate plan 1>,
- ...
- ]
- }
For sharded collections, explain
includes the corequery planner and server information for each accessedshard in the shards
field:
- "queryPlanner" : {
- "mongosPlannerVersion" : <int>,
- "winningPlan" : {
- "stage" : <STAGE1>,
- "shards" : [
- {
- "shardName" : <string>,
- "connectionString" : <string>,
- "serverInfo" : {
- "host" : <string>,
- "port" : <int>,
- "version" : <string>,
- "gitVersion" : <string>
- },
- "plannerVersion" : <int>,
- "namespace" : <string>,
- "parsedQuery" : <document>,
- "queryHash" : <hexadecimal string>,
- "planCacheKey" : <hexadecimal string>,
- "optimizedPipeline" : <boolean>, // Starting in MongoDB 4.2, only appears if true
- "winningPlan" : {
- "stage" : <STAGE2>,
- "inputStage" : {
- "stage" : <STAGE3>
- ...,
- }
- },
- "rejectedPlans" : [
- <candidate plan 1>,
- ...
- ]
- },
- ...
- ]
- }
- }
explain.
queryPlanner
Contains information on the selection of the query plan by thequery optimizer.
explain.queryPlanner.
namespace
A string that specifies the namespace (i.e.,
<database>.<collection>
) against which the query is run.A boolean that specifies whether MongoDB applied an indexfilter for the query shape.
- A hexadecimal string that represents the hash of thequery shape and is dependent only on the query shapes.
queryHash
can help identify slow queries (including thequery filter of write operations) with the same query shape.
Note
As with any hash function, two different query shapes may resultin the same hash value. However, the occurrence of hashcollisions between different query shapes is unlikely.
For more information on queryHash
and planCacheKey
,see queryHash and planCacheKey.
New in version 4.2.
explain.queryPlanner.
planCacheKey
- A hash of the key for the plan cache entry associated with thequery.
Unlike the queryHash
, theplanCacheKey
is a function ofboth the query shape and the currently available indexes forthat shape. That is, if indexes that can support the queryshape are added/dropped, the planCacheKey
value may changewhereas the queryHash
value would not change.
For more information on queryHash
and planCacheKey
,see queryHash and planCacheKey.
New in version 4.2.
explain.queryPlanner.
optimizedPipeline
- A boolean that indicates that the entire aggregation pipelineoperation was optimized away, and instead, fulfilled by a treeof query plan execution stages.
For example, starting in MongodB 4.2, the followingaggregation operation can be fulfilled by the tree of queryplan execution rather than using the aggregation pipeline.
- db.example.aggregate([ { $match: { someFlag: true } } ] )
The field is only present if the value is true
and onlyapplies to explain on aggregation pipeline operations. Whentrue
, because the pipeline was optimized away, noaggregation stage information appears in the output.
New in version 4.2.
explain.queryPlanner.
winningPlan
A document that details the plan selected by the queryoptimizer. MongoDB presents the plan as atree of stages; i.e. a stage can have an
inputStage
or, if thestage has multiple child stages,inputStages
.
Each stage consists of information specific to the stage. Forinstance, an IXSCAN
stage will include the index boundsalong with other data specific to the index scan. If a stagehas a child stage or multiple child stages, the stage will have aninputStage or inputStages.
- <code>explain.queryPlanner.winningPlan.</code><code>inputStage</code>[]($b09beaeefc00b1c7.md#explain.queryPlanner.winningPlan.inputStage)
-
A document that describes the child stage, which provides thedocuments or index keys to its parent. The field is presentif the parent stage has only one child.
- <code>explain.queryPlanner.winningPlan.</code><code>inputStages</code>[]($b09beaeefc00b1c7.md#explain.queryPlanner.winningPlan.inputStages)
-
An array of documents describing the child stages. Childstages provide the documents or index keys to the parentstage. The field is present if the parent stage has multiplechild nodes. For example, stages for $or expressions or index intersection consume input frommultiple sources.
explain.queryPlanner.
rejectedPlans
- Array of candidate plans considered and rejected by thequery optimizer. The array can be empty if there were no othercandidate plans.
executionStats
The returned executionStats
information details theexecution of the winning plan. In order to includeexecutionStats
in the results, you must run the explain in either:
- executionStats or
- allPlansExecutionverbosity mode. Use
allPlansExecution
mode to include partialexecution data captured during plan selection.
- Unsharded Collections
- Sharded Collections
For unsharded collections, explain
returns the followingexecutionStats
information:
- "executionStats" : {
- "executionSuccess" : <boolean>,
- "nReturned" : <int>,
- "executionTimeMillis" : <int>,
- "totalKeysExamined" : <int>,
- "totalDocsExamined" : <int>,
- "executionStages" : {
- "stage" : <STAGE1>
- "nReturned" : <int>,
- "executionTimeMillisEstimate" : <int>,
- "works" : <int>,
- "advanced" : <int>,
- "needTime" : <int>,
- "needYield" : <int>,
- "saveState" : <int>,
- "restoreState" : <int>,
- "isEOF" : <boolean>,
- ...
- "inputStage" : {
- "stage" : <STAGE2>,
- "nReturned" : <int>,
- "executionTimeMillisEstimate" : <int>,
- ...
- "inputStage" : {
- ...
- }
- }
- },
- "allPlansExecution" : [
- {
- "nReturned" : <int>,
- "executionTimeMillisEstimate" : <int>,
- "totalKeysExamined" : <int>,
- "totalDocsExamined" :<int>,
- "executionStages" : {
- "stage" : <STAGEA>,
- "nReturned" : <int>,
- "executionTimeMillisEstimate" : <int>,
- ...
- "inputStage" : {
- "stage" : <STAGEB>,
- ...
- "inputStage" : {
- ...
- }
- }
- }
- },
- ...
- ]
- }
For sharded collections, explain
includes the executionstatistics for each accessed shard.
- "executionStats" : {
- "nReturned" : <int>,
- "executionTimeMillis" : <int>,
- "totalKeysExamined" : <int>,
- "totalDocsExamined" : <int>,
- "executionStages" : {
- "stage" : <STAGE1>
- "nReturned" : <int>,
- "executionTimeMillis" : <int>,
- "totalKeysExamined" : <int>,
- "totalDocsExamined" : <int>,
- "totalChildMillis" : <NumberLong>,
- "shards" : [
- {
- "shardName" : <string>,
- "executionSuccess" : <boolean>,
- "executionStages" : {
- "stage" : <STAGE2>,
- "nReturned" : <int>,
- "executionTimeMillisEstimate" : <int>,
- ...
- "chunkSkips" : <int>,
- "inputStage" : {
- "stage" : <STAGE3>,
- ...
- "inputStage" : {
- ...
- }
- }
- }
- },
- ...
- ]
- }
- "allPlansExecution" : [
- {
- "shardName" : <string>,
- "allPlans" : [
- {
- "nReturned" : <int>,
- "executionTimeMillisEstimate" : <int>,
- "totalKeysExamined" : <int>,
- "totalDocsExamined" :<int>,
- "executionStages" : {
- "stage" : <STAGEA>,
- "nReturned" : <int>,
- "executionTimeMillisEstimate" : <int>,
- ...
- "inputStage" : {
- "stage" : <STAGEB>,
- ...
- "inputStage" : {
- ...
- }
- }
- }
- },
- ...
- ]
- },
- {
- "shardName" : <string>,
- "allPlans" : [
- ...
- ]
- },
- ...
- ]
- }
explain.
executionStats
Contains statistics that describe the completed query execution forthe winning plan. For write operations, completed query executionrefers to the modifications that would be performed, but doesnot apply the modifications to the database.
explain.executionStats.
nReturned
Number of documents that match the query condition.
nReturned
corresponds to then
fieldreturned bycursor.explain()
in earlier versions of MongoDB.Total time in milliseconds required for query plan selection andquery execution.
executionTimeMillis
correspondsto themillis
field returned bycursor.explain()
inearlier versions of MongoDB.Number of index entries scanned.
totalKeysExamined
corresponds to thenscanned
field returned bycursor.explain()
inearlier versions of MongoDB.- Number of documents examined during query execution. Commonquery execution stages that examine documents are
COLLSCAN
andFETCH
.
Note
totalDocsExamined
refers tothe total number of documents examined and not to thenumber of documents returned. For example, a stage canexamine a document in order to apply a filter. If thedocument is filtered out, then it has been examined butwill not be returned as part of the query result set.
If a document is examined multiple times during queryexecution,totalDocsExamined
countseach examination. That is,totalDocsExamined
is not acount of the total number of unique documents examined.
explain.executionStats.
executionStages
- Details the completed execution of the winning plan as a tree ofstages; i.e. a stage can have an
inputStage
or multipleinputStages
.
Each stage consists of execution information specific to thestage.
- <code>explain.executionStats.executionStages.</code><code>works</code>[]($b09beaeefc00b1c7.md#explain.executionStats.executionStages.works)
-
Specifies the number of “work units” performed by the queryexecution stage. Query execution divides its work into smallunits. A “work unit” might consist of examining a single index key,fetching a single document from the collection, applying aprojection to a single document, or doing a piece of internalbookkeeping.
- <code>explain.executionStats.executionStages.</code><code>advanced</code>[]($b09beaeefc00b1c7.md#explain.executionStats.executionStages.advanced)
-
The number of intermediate results returned, or advanced, bythis stage to its parent stage.
- <code>explain.executionStats.executionStages.</code><code>needTime</code>[]($b09beaeefc00b1c7.md#explain.executionStats.executionStages.needTime)
-
The number of work cycles that did not advance an intermediateresult to its parent stage (seeexplain.executionStats.executionStages.advanced
). Forinstance, an index scan stage may spend a work cycle seeking to anew position in the index as opposed to returning an indexkey; this work cycle would count towardsexplain.executionStats.executionStages.needTime
ratherthan explain.executionStats.executionStages.advanced
.
- <code>explain.executionStats.executionStages.</code><code>needYield</code>[]($b09beaeefc00b1c7.md#explain.executionStats.executionStages.needYield)
-
The number of times that the storage layer requested that thequery stage suspend processing and yield its locks.
- <code>explain.executionStats.executionStages.</code><code>saveState</code>[]($b09beaeefc00b1c7.md#explain.executionStats.executionStages.saveState)
-
The number of times that the query stage suspended processingand saved its current execution state, for example inpreparation for yielding its locks.
- <code>explain.executionStats.executionStages.</code><code>restoreState</code>[]($b09beaeefc00b1c7.md#explain.executionStats.executionStages.restoreState)
-
The number of times that the query stage restored a savedexecution state, for example after recovering locks that it hadpreviously yielded.
- <code>explain.executionStats.executionStages.</code><code>isEOF</code>[]($b09beaeefc00b1c7.md#explain.executionStats.executionStages.isEOF)
-
Specifies whether the execution stage has reached end of stream:
- If <code>true</code> or <code>1</code>, the execution stage has reachedend-of-stream.
- If <code>false</code> or <code>0</code>, the stage may still have results toreturn. For example, consider a query with a limit whoseexecution stages consists of a <code>LIMIT</code> stage with aninput stage of <code>IXSCAN</code> for the query. If the queryreturns more than the specified limit, the <code>LIMIT</code> stagewill report <code>isEOF: 1</code>, but its underlying <code>IXSCAN</code> stagewill report <code>isEOF: 0</code>.
- <code>explain.executionStats.executionStages.inputStage.</code><code>keysExamined</code>[]($b09beaeefc00b1c7.md#explain.executionStats.executionStages.inputStage.keysExamined)
-
For query execution stages that scan an index (e.g. IXSCAN),keysExamined
is the total number of in-bounds and out-of-boundskeys that are examined in the process of the index scan. If theindex scan consists of a single contiguous range of keys, onlyin-bounds keys need to be examined. If the index bounds consists ofseveral key ranges, the index scan execution process may examineout-of-bounds keys in order to skip from the end of one range to thebeginning of the next.
Consider the following example, where there is an index of fieldx
and the collection contains 100 documents with x
values1 through 100:
- db.keys.find( { x : { $in : [ 3, 4, 50, 74, 75, 90 ] } } ).explain( "executionStats" )
The query will scan keys 3
and 4
. It will then scan the key5
, detect that it is out-of-bounds, and skip to the next key50
.
Continuing this process, the query scans keys3, 4, 5, 50, 51, 74, 75, 76, 90, and 91. Keys5
, 51
, 76
, and 91
are out-of-bounds keys that arestill examined. The value of keysExamined
is 10.
- <code>explain.executionStats.executionStages.inputStage.</code><code>docsExamined</code>[]($b09beaeefc00b1c7.md#explain.executionStats.executionStages.inputStage.docsExamined)
-
Specifies the number of documents scanned during thequery execution stage.
Present for the COLLSCAN
stage, as well as for stages thatretrieve documents from the collection (e.g. FETCH
)
- <code>explain.executionStats.executionStages.inputStage.</code><code>seeks</code>[]($b09beaeefc00b1c7.md#explain.executionStats.executionStages.inputStage.seeks)
-
New in version 3.4: For index scan (IXSCAN
) stages only.
The number of times that we had to seek the index cursor toa new position in order to complete the index scan.
explain.executionStats.
allPlansExecution
- Contains partial execution information captured during theplan selection phase forboth the winning and rejected plans. The field is present only if
explain
runs inallPlansExecution
verbosity mode.
serverInfo
- Unsharded Collections
- Sharded Collections
For unsharded collections, explain
returns the followingserverInfo
information for the MongoDB instance:
- "serverInfo" : {
- "host" : <string>,
- "port" : <int>,
- "version" : <string>,
- "gitVersion" : <string>
- }
For sharded collections, explain
returns theserverInfo
for each accessed shard.
- "queryPlanner" : {
- ...
- "winningPlan" : {
- "stage" : <STAGE1>,
- "shards" : [
- {
- "shardName" : <string>,
- "connectionString" : <string>,
- "serverInfo" : {
- "host" : <string>,
- "port" : <int>,
- "version" : <string>,
- "gitVersion" : <string>
- },
- ...
- }
- ...
- ]
3.0 Format Change
Starting in MongoDB 3.0, the format and fields of the explain
results have changed from previous versions. The following lists somekey differences.
Collection Scan vs. Index Use
If the query planner selects a collection scan, the explain resultincludes a COLLSCAN
stage.
If the query planner selects an index, the explain result includes aIXSCAN
stage. The stage includes information such as the indexkey pattern, direction of traversal, and index bounds.
In previous versions of MongoDB, cursor.explain()
returned thecursor
field with the value of:
BasicCursor
for collection scans, andBtreeCursor <index name> [<direction>]
for index scans.
For more information on execution statistics of collection scans versusindex scans, see Analyze Query Performance.
Covered Queries
When an index covers a query, MongoDB can both match the queryconditions and return the results using only the index keys; i.e.MongoDB does not need to examine documents from the collection toreturn the results.
When an index covers a query, the explain result has an IXSCAN
stage that is not a descendant of a FETCH
stage, and in theexecutionStats, the totalDocsExamined
is 0
.
In earlier versions of MongoDB, cursor.explain()
returned theindexOnly
field to indicate whether the index covered a query.
Index Intersection
For an index intersection plan, theresult will include either an AND_SORTED
stage or an AND_HASH
stage with an inputStages
array thatdetails the indexes; e.g.:
- {
- "stage" : "AND_SORTED",
- "inputStages" : [
- {
- "stage" : "IXSCAN",
- ...
- },
- {
- "stage" : "IXSCAN",
- ...
- }
- ]
- }
In previous versions of MongoDB, cursor.explain()
returned thecursor
field with the value of Complex Plan
for indexintersections.
$or Expression
If MongoDB uses indexes for an $or
expression, the result willinclude the OR
stage with aninputStages
array thatdetails the indexes; e.g.:
- {
- "stage" : "OR",
- "inputStages" : [
- {
- "stage" : "IXSCAN",
- ...
- },
- {
- "stage" : "IXSCAN",
- ...
- },
- ...
- ]
- }
In previous versions of MongoDB, cursor.explain()
returned theclauses
array that detailed the indexes.
Sort Stage
If MongoDB can use an index scan to obtain the requested sort order,the result will not include a SORT
stage. Otherwise, if MongoDBcannot use the index to sort, the explain
result will include aSORT
stage.
Prior to MongoDB 3.0, cursor.explain()
returned thescanAndOrder
field to specify whether MongoDB could use the indexorder to return sorted results.