In this article, we will share with you the knowledge of MongoDB viewing execution plan, hoping to help everyone. The explain() function in MongoDB can help us view query-related information, which helps us quickly find search bottlenecks and solve them. In this article, we will take a look at some uses of explain() and the meaning of query results.
1.Installing MongoDB on Linux
2.Basic operations of MongoDB
3.MongoDB data types
4.MongoDB document update operations
5.MongoDB document query operations (1)
6.MongoDB document query operation (2)
7.MongoDB document query operation (3)
Overall, the usage of explain() and the usage of sort() and limit() Almost the same, the difference is that explain() must be placed at the end.
Let’s first look at a basic usage:
db.sang_collect.find({x:1}).explain()
Follows directly after the find() function, indicating to view the execution plan of the find() function. The results are as follows:
{ "queryPlanner" : { "plannerVersion" : 1, "namespace" : "sang.sang_collect", "indexFilterSet" : false, "parsedQuery" : { "x" : { "$eq" : 1.0 } }, "winningPlan" : { "stage" : "COLLSCAN", "filter" : { "x" : { "$eq" : 1.0 } }, "direction" : "forward" }, "rejectedPlans" : [] }, "serverInfo" : { "host" : "localhost.localdomain", "port" : 27017, "version" : "3.4.9", "gitVersion" : "876ebee8c7dd0e2d992f36a848ff4dc50ee6603e" }, "ok" : 1.0 }
The returned result contains two large pieces of information, one is queryPlanner, which is the query plan, and the other is serverInfo, which is some information about the MongoDB service. So there are many parameters involved here, let’s take a look at them one by one:
Parameters | Meaning |
---|---|
plannerVersion | Query plan version |
namespace | Collection to be queried |
indexFilterSet | Whether to use index |
parsedQuery | query condition, here is x=1 |
winningPlan | Best execution plan |
stage | Query methods, common ones include COLLSCAN/full table scan, IXSCAN/index scan, FETCH /Retrieve documents based on index, SHARD_MERGE/Merge shard results, IDHACK/Query for _id |
Filter conditions | |
Search direction | |
Rejected execution plan | |
MongoDB server information |
{ "queryPlanner" : { "plannerVersion" : 1, "namespace" : "sang.sang_collect", "indexFilterSet" : false, "parsedQuery" : {}, "winningPlan" : { "stage" : "COLLSCAN", "direction" : "forward" }, "rejectedPlans" : [] }, "executionStats" : { "executionSuccess" : true, "nReturned" : 10000, "executionTimeMillis" : 4, "totalKeysExamined" : 0, "totalDocsExamined" : 10000, "executionStages" : { "stage" : "COLLSCAN", "nReturned" : 10000, "executionTimeMillisEstimate" : 0, "works" : 10002, "advanced" : 10000, "needTime" : 1, "needYield" : 0, "saveState" : 78, "restoreState" : 78, "isEOF" : 1, "invalidates" : 0, "direction" : "forward", "docsExamined" : 10000 } }, "serverInfo" : { "host" : "localhost.localdomain", "port" : 27017, "version" : "3.4.9", "gitVersion" : "876ebee8c7dd0e2d992f36a848ff4dc50ee6603e" }, "ok" : 1.0 }
Meaning | |
---|---|
Whether the execution was successful | |
Number of results returned | |
Execution Time consuming | |
Number of index scans | |
Number of document scans | |
This category describes the execution status | |
Scanning method, the specific optional values are the same as above The same as the text | |
Query result number | |
Estimated time consumption | |
The number of work units, a query will be broken down into small work units | |
Priority The number of results returned | |
The number of document checks, consistent with totalDocsExamined |
Okay, let’s stop talking about explain() in MongoDB. If you have any questions, please leave a message for discussion.
Related recommendations:
MongoDB tips and precautions summaryMongoDB fixed collection detailed explanationLaravel tutorial on how to use mongodb database