Analyze Query Performancethe Explain () cursor method allows you to observe the operations performed by the query system. This method is useful for analyzing efficient queries and determining how indexes are used for querying. This method detects the operation of the query, not the query execution time. Because this method attempts multiple query plans, it does not accurately reflect the query execution time.
evaluate the performance of a query Use the Explain () method to invoke the method of the pointer returned by find ().
Cases:
Create an index in the Type field
db.testData.ensureIndex ({' type ': 1});
Evaluates a query on the Type field.
db.testData.find ({type: ' food '}). Explain ()
The results are as follows:
{
"Cursor": "Btreecursor type_1",
"Ismultikey": false,
"N": 3,
"Nscannedobjects": 3,
"Nscanned": 3,
"Nscannedobjectsallplans": 3,
"Nscannedallplans": 3,
"Scanandorder": false,
"IndexOnly": false,
"Nyields": 1,
"Nchunkskips": 0,
"Millis": 64,
"Indexbounds": {
' Type ': [
[
"Food",
"Food"
]
]
},
"Server": "tt:27017",
"Filterset": false
}
The cursor value for the Btreecursor table name query uses an index.
The query returns N=3 records.
In order to return to these five records, the query scanned the nscanned=3 record and then read the full record of the nscannedobjects=3 bar, and if there is no index, all records will be scanned.
Compare index query Performance Manually compare a query that uses multiple fields, and you can use the hint () and explain () methods together.
Example: Evaluating an index using a different field
db.testData.find ({type: ' food '}). Hint ({type:1}). Explain ();
Results:
{
"Cursor": "Btreecursor type_1",
"Ismultikey": false,
"N": 3,
"Nscannedobjects": 3,
"Nscanned": 3,
"Nscannedobjectsallplans": 3,
"Nscannedallplans": 3,
"Scanandorder": false,
"IndexOnly": false,
"Nyields": 0,
"Nchunkskips": 0,
"Millis": 40,
"Indexbounds": {
' Type ': [
[
"Food",
"Food"
]
]
},
"Server": "tt:27017",
"Filterset": false
}
db.testData.find ({type: ' food '}). Hint ({type:1, name:1}). Explain ();
The execution of this sentence is unsuccessful, pending resolution
The results of these returned statistics omit queries that are executed using their respective indexes.
NOTE: If the hint () explain () method is not applicable, the query optimizer will reevaluate the query and run the multi-index query before returning the query statistics.
For more detailed explain output, see explain-results.
MongoDB Operations Manual crud Query performance analysis