MongoDB study notes (five)

MongoDB view an execution plan

explain MongoDB in the () function can help us see the relevant information, which helps us to quickly find the search bottleneck and then solve it, we'll take a look at this article explain () usage and meaning of some of the query results.

This article is the eighth in the series of articles MongoDB, understand the previous article contribute to a better understanding of the article:

Overall, explain () usage and sort (), limit () uses about the same, except that explain () must be placed last.

Basic Usage

First look at a basic usage:

1
db.sang_collect.find({x:1}).explain()

Directly with the find () after the function, that the implementation plan view the find () function, the results are as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{
"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
}

Return result contains two blocks of information, one is queryPlanner, namely query plan, there is a serverInfo, some of the information that is MongoDB service. This parameter involved more, we look one by one:

parameter meaning
plannerVersion Query Plan Version
namespace Collection to be queried
indexFilterSet Whether to use index
parsedQuery Query, here x = 1
winningPlan Best execution plan
stage Query, a common ** COLLSCAN / full table scan, IXSCAN / index scan, FETCH / according to the index to retrieve documents, SHARD_MERGE / merge fragmented results, IDHACK / for
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
| filter | filter criteria | 
| direction | Search direction |
| rejectedPlans | refuse execution plan |
| ServerInfo | MongoDB server information |

## add different parameters

explain () also receive different parameters, by setting different parameters we can see more detail the query plan.

QueryPlanner ###

queryPlanner default parameters, add queryPlanner parameter query result is what we see above the query results, so, not repeat them here.

ExecutionStats ###

executionStats return some statistical information on best execution plan, as follows: ``

`json
{
" queryPlanner ": {
" plannerVersion ": 1,
" namespace ":" sang.sang_collect ",
" indexFilterSet ": false,
"parsedQuery": {},
"winningPlan": {
"Stage": "COLLSCAN",
"direction": "Forward"
},
"rejectedPlans":



"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
}

In addition to the above, here we are introduced to some of the parameters, but also more executionStats parameters, the following meanings:

parameter meaning
executionSuccess Whether success
nReturned The number of returned results
executionTimeMillis Time-consuming
totalKeysExamined An index number of scans
totalDocsExamined Document scanning frequency
executionStages Describe the state of the implementation of this classified
stage Scanning the same manner, and specific optional value above
nReturned The number of query results
executionTimeMillisEstimate Estimated time consuming
works The number of units of work, a query into smaller units of work
advanced Priority number of results returned
docsExamined The number of documents checked, totalDocsExamined consistent with

allPlansExecution

allPlansExecution used to get all execution plans, the results of the basic parameters the same as above, will not elaborate here.

Well, explain MongoDB in () Here we are, small partners have discussed questions please leave a message.

References:

  1. "MongoDB Definitive Guide 2nd Edition"
  2. MongoDB implementation plan acquisition (db.collection.explain ())
  3. mongodb .explain ( 'executionStats') query performance analysis (could not find the original source)

Guess you like

Origin www.cnblogs.com/eer123/p/11734058.html