On this page本页内容
The cursor.explain("executionStats")
and the db.collection.explain("executionStats")
methods provide statistics about the performance of a query. These statistics can be useful in measuring if and how a query uses an index.
db.collection.explain()
provides information on the execution of other operations, such as db.collection.update()
. See db.collection.explain()
for details.
MongoDB Compass provides an Explain Plan tab, which displays statistics about the performance of a query. These statistics can be useful in measuring if and how a query uses an index.
Consider a collection inventory
with the following documents:
The documents appear in MongoDB Compass as the following:
The following query retrieves documents where the quantity
field has a value between 100
and 200
, inclusive:
The query returns the following documents:
To view the query plan selected, chain the cursor.explain("executionStats")
cursor method to the end of the find command:
explain()
returns the following results:
queryPlanner.winningPlan.stage
displays COLLSCAN
to indicate a collection scan.
Collection scans indicate that the mongod
had to scan the entire collection document by document to identify the results. This is a generally expensive operation and can result in slow queries.
executionStats.nReturned
displays 3
to indicate that the query matches and returns three documents.executionStats.totalKeysExamined
displays 0
to indicate that this is query is not using an index.executionStats.totalDocsExamined
displays 10
to indicate that MongoDB had to scan ten documents (i.e. all documents in the collection) to find the three matching documents.The following query retrieves documents where the quantity
field has a value between 100
and 200
, inclusive:
Copy the following filter into the Compass query bar and click Find:将以下筛选器复制到Compass查询栏中,然后单击“查找”:
The query returns the following documents:
To view the query plan selected:
test.inventory
collection.MongoDB Compass displays the query plan as follows:
Note
Because we are working with such a small dataset for the purposes of this tutorial, the Actual Query Execution Time displays 0
seconds, even though we are not using an index.
In a larger dataset, the difference in query execution time between an indexed query versus a non-indexed query would be much more substantial.
3
to indicate that the query matches and returns three documents.0
to indicate that this query is not using an index.10
to indicate that MongoDB had to scan ten documents (i.e. all documents in the collection) to find the three matching documents.COLLSCAN
query stage to indicate that a collection scan was used for this query.
Collection scans indicate that the mongod
had to scan the entire collection document by document to identify the results. This is a generally expensive operation and can result in slow queries.
The explain details can also be viewed in raw JSON format by clicking Raw JSON below the query bar:
The difference between the number of matching documents and the number of examined documents may suggest that, to improve efficiency, the query might benefit from the use of an index.
To support the query on the quantity
field, add an index on the quantity
field:
To view the query plan statistics, use the explain("executionStats")
method:
The explain()
method returns the following results:
queryPlanner.winningPlan.inputStage.stage
displays IXSCAN
to indicate index use.executionStats.nReturned
displays 3
to indicate that the query matches and returns three documents.executionStats.totalKeysExamined
displays 3
to indicate that MongoDB scanned three index entries. The number of keys examined match the number of documents returned, meaning that the mongod
only had to examine index keys to return the results. The mongod
did not have to scan all of the documents, and only the three matching documents had to be pulled into memory. This results in a very efficient query.executionStats.totalDocsExamined
display 3
to indicate that MongoDB scanned three documents.test.inventory
collection.quantity
from the Select a field name dropdown.1 (asc)
from the type dropdown.Note
Leaving the index name field blank causes MongoDB Compass to create a default name for the index.
You can now see your newly created index in the Indexes tab:
Return to the Explain Plan tab for the inventory
collection and re-run the query from the previous step:
MongoDB Compass displays the query plan as follows:
3
to indicate that the query matches and returns three documents.3
to indicate that MongoDB scanned three index entries. The number of keys examined match the number of documents returned, meaning that the mongod
only had to examine index keys to return the results. The mongod
did not have to scan all of the documents, and only the three matching documents had to be pulled into memory. This results in a very efficient query.3
to indicate that MongoDB scanned three documents.quantity
index.FETCH
and IXSCAN
. IXSCAN
indicates that the mongod
used an index to satisfy the query before exeuting the FETCH
stage and retrieving the documents.The explain details can also be viewed in raw JSON format by clicking Raw JSON below the query bar:
Without the index, the query would scan the whole collection of 10
documents to return 3
matching documents. The query also had to scan the entirety of each document, potentially pulling them into memory. This results in an expensive and potentially slow query operation.
When run with an index, the query scanned 3
index entries and 3
documents to return 3
matching documents, resulting in a very efficient query.
To manually compare the performance of a query using more than one index, you can use the hint()
method in conjunction with the explain()
method.
Consider the following query:
The query returns the following documents:
To support the query, add a compound index. With compound indexes, the order of the fields matter.
For example, add the following two compound indexes. The first index orders by quantity
field first, and then the type
field. The second index orders by type
first, and then the quantity
field.
Evaluate the effect of the first index on the query:
The explain()
method returns the following output:
MongoDB scanned 5
index keys (executionStats.totalKeysExamined
) to return 2
matching documents (executionStats.nReturned
).
Evaluate the effect of the second index on the query:
The explain()
method returns the following output:
MongoDB scanned 2
index keys (executionStats.totalKeysExamined
) to return 2
matching documents (executionStats.nReturned
).
The second compound index, { type: 1, quantity: 1 }
, is therefore the more efficient index for supporting the example query, as the MongoDB server only needs to scan 2
index keys
to find all matching documents using this index, compared to 5
when when using the compound index { quantity: 1, type: 1 }
.