MongoDB operation manual CRUD query performance analysis

Source: Internet
Author: User
The explain () cursor Method for Analyzing query performance allows you to observe the operations performed by the query system. This method is very useful for analyzing efficient queries and deciding how to use indexes for queries. This method detects the query operation, rather than the query execution time. Because this method attempts multiple query plans, it cannot accurately reflect the query execution time.

The explain () cursor Method for Analyzing query performance allows you to observe the operations performed by the query system. This method is very useful for analyzing efficient queries and deciding how to use indexes for queries. This method detects the query operation, rather than the query execution time. Because this method attempts multiple query plans, it cannot accurately reflect the query execution time.

The explain () cursor Method for Analyzing query performance allows you to observe the operations performed by the query system. This method is very useful for analyzing efficient queries and deciding how to use indexes for queries. This method detects the query operation, rather than the query execution time. Because this method attempts multiple query plans, it cannot accurately reflect the query execution time.
To evaluate the performance of a query, use the explain () method to call the pointer returned by find.
Example:
Create an index in the type field
Db. testData. ensureIndex ({'type': 1 });
Evaluate a query in the type field.
Db. testData. find ({type: 'food'}). explain ()
The result is 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 is BtreeCursor. The table name query uses an index.
N = 3 records are returned for query.
In order to return these five records, the query scans the nscanned = 3 Records, and then reads the nscannedObjects = 3 complete records. If no index is found, all records are scanned.
Compare index query performance: manually compare a query that uses multiple fields. You can use the hint () and explain () methods in combination.
For example, the index of different fields is evaluated.
Db. testData. find ({type: 'food'}). hint ({type: 1}). explain ();
Result:
{
"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 ();
// This statement is not executed successfully and will be resolved
The returned statistical results ignore the queries executed using their respective indexes.
Note: If the explain () method is not applicable to hint (), the query optimizer re-evaluates the query and runs the multi-index query before returning the query statistics.
For more detailed explain output, see explain-results.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.