MongoDB查看执行计划

一、概述

MongoDB中的explain()函数可以帮助我们查看查询相关的信息,查询分析可以确保我们创建的索引是否有效,是查询语句性能分析的重要工具。

二、explain()基本用法

explain()的用法是必须放在最后面,语法如下:

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

explain()常用是直接跟在find()函数后面,表示查看find()函数的执行计划,举例:

MongoDB Enterprise mongos> db.emp.find({"id":{$lt:1000}}).explain()
{
    "queryPlanner" : {
                    serverInfo" : {
                        "host" : "mongotest1",
                        "port" : 27021,
                        "version" : "3.2.8",
                        "gitVersion" : "ed70e33130c977bda0024c125b56d159573dbaf0"
                    },
                    "plannerVersion" : 1,
                    "namespace" : "testdb.emp",
                    "indexFilterSet" : false,
                    "parsedQuery" : {
                        "id" : {
                            "$lt" : 1000
                        }
                    },
                       winningPlan" : {
                        "stage" : "FETCH",
                        "inputStage" : {
                            "stage" : "SHARDING_FILTER",
                            "inputStage" : {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                    "id" : 1
                                },
                                "indexName" : "id_1",
                                "direction" : "forward",
                            },
                    "rejectedPlans" : [ ]
   },
   "ok" : 1
}

参数说明:

《MongoDB查看执行计划》

以上我们看到的是explain()默认参数的情况,其实MongoDB 3.0之后,explain的返回与使用方法与之前版本有了很大的变化,

3.0+版本的explain有三种模式,分别是:queryPlanner、executionStats、allPlansExecution。常用的是queryPlanner和executionStats模式

那我们再来看看executionStats这种模式

MongoDB Enterprise mongos> db.emp.find({"id":{$lt:1000}}).explain("executionStats")
{
    "queryPlanner" : {
        "mongosPlannerVersion" : 1,
        "winningPlan" : {
            "stage" : "SHARD_MERGE",
                  },
                  "rejectedPlans" : [ ]
    },
   "executionStats" : {
        "nReturned" : 999,
        "executionTimeMillis" : 35,
        "totalKeysExamined" : 999,
        "totalDocsExamined" : 999,
        "executionStages" : {
            "stage" : "SHARD_MERGE",
            "nReturned" : 999,
            "executionTimeMillis" : 35,
            "totalKeysExamined" : 999,
            "totalDocsExamined" : 999,
            "totalChildMillis" : NumberLong(33),
            "shards" : [
                {
                    "shardName" : "shard1",
                    "executionSuccess" : true,
                    "executionStages" : {
                        "stage" : "FETCH",
                        "nReturned" : 980,
                        "executionTimeMillisEstimate" : 30,
                        "works" : 981,
                        "advanced" : 980,
                        "needTime" : 0,
                        "needYield" : 0,
                        "saveState" : 7,
                        "restoreState" : 7,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "docsExamined" : 980,
                        "alreadyHasObj" : 0
    },
    "ok" : 1
}

executionStats的参数说明:

《MongoDB查看执行计划》
在此看来,executionStats这种模式比默认的queryPlanner给出来个更多的可参考的信息,

另外一种模式allPlansExecution是用来获取所有执行计划,参数基本与以上的相同,这里就不再详细说明。

三、总结

原来explain()也是可以接收不同的参数,通过设置不同参数我们可以查看更详细的查询计划。

queryPlanner:查询计划的选择器,首先进行查询分析,最终选择一个winningPlan,是explain返回的默认模式

executionStats:为执行统计模式,返回winningPlan的统计结果

allPlansExecution:为返回所有执行计划的统计,包括rejectedPlan

所以:我们在查询优化的时候,只需要关注queryPlanner, executionStats即可,因为queryPlanner为我们选择出了winningPlan, 而executionStats为我们统计了winningPlan的所有关键数据。

整体来说,通过explain()查看执行计划,分析查询性能情况,可以帮助我们更好的分析和优化,必要的时候可以创建索引,提升查询性能。

    原文作者:fyg0524
    原文地址: https://segmentfault.com/a/1190000016809279
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞