Home > headlines > MongoDB view execution plan

MongoDB view execution plan

小云云
Release: 2017-12-09 16:12:38
Original
2072 people have browsed it

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.

Basic usage

Let’s first look at a basic usage:

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

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
}
Copy after login

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:

##filterFilter conditionsdirectionSearch directionrejectedPlansRejected execution planserverInfoMongoDB server information
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
Add different parameters

explain() also receives different parameters. By setting different parameters we can view more details query plan.

queryPlanner

queryPlanner is the default parameter. The query result after adding the queryPlanner parameter is the query result we saw above, so we will not go into details here.

executionStats

executionStats will return some statistical information about the best execution plan, as follows:

{
    "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
}
Copy after login
In addition to some of the parameters we introduced above, there are more executionStats parameter, the meaning is as follows:

ParameterMeaningexecutionSuccessWhether the execution was successfulnReturnedNumber of results returnedexecutionTimeMillisExecution Time consumingtotalKeysExaminedNumber of index scanstotalDocsExaminedNumber of document scansexecutionStagesThis category describes the execution statusstageScanning method, the specific optional values ​​are the same as above The same as the textnReturnedQuery result number##executionTimeMillisEstimateworksadvanceddocsExaminedallPlansExecution
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

allPlansExecution is used to obtain all execution plans. The result parameters are basically the same as above, so I won’t go into details here.

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 summary

MongoDB fixed collection detailed explanation

Laravel tutorial on how to use mongodb database

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template