mongodb基础篇--explain执行计划


我们可以通过执行计划来判断查询语句的效率,根据实际情况进行调整,然后提高查询效率。
可以使用如下方法:

db.collection.find().explain(<verbose>)

verbose 参数表示执行计划的输出模式。有三种:queryPlanner,executionStats,allPlanExecution

模式名字描述
queryPlanner执行计划的详细信息,包括查询计划、集合信息、查询条件、最佳执行计划、查询方式和 MongoDB 服务信息等
exectionStats最佳执行计划的执行情况和被拒绝的计划等信息
allPlansExecution选择并执行最佳执行计划,并返回最佳执行计划和其他执行计划的执行情况

每种模式返回均不相同

queryPlanner

db.getCollection('test').find({"user_id":3224484}).explain("queryPlanner")

返回值如下:

{
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "w.test",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "user_id" : {
                "$eq" : 3224484.0
            }
        },
        "winningPlan" : {
            "stage" : "FETCH",
            "inputStage" : {
                "stage" : "IXSCAN",
                "keyPattern" : {
                    "user_id" : -1
                },
                "indexName" : "idx_user_id",
                "isMultiKey" : false,
                "multiKeyPaths" : {
                    "user_id" : []
                },
                "isUnique" : false,
                "isSparse" : false,
                "isPartial" : false,
                "indexVersion" : 2,
                "direction" : "forward",
                "indexBounds" : {
                    "user_id" : [ 
                        "[3224484.0, 3224484.0]"
                    ]
                }
            }
        },
        "rejectedPlans" : []
    },
    "serverInfo" : {
        "host" : "localhost",
        "port" : 37100,
        "version" : "4.0.10",
        "gitVersion" : "c389e7f69f637f7a1ac3cc9fae843b635f20b766"
    },
    "ok" : 1.0,
    "operationTime" : Timestamp(1575535171, 1)
}
字段名称描述
plannerVersion执行计划的版本
namespace查询的集合
indexFilterSet是否使用索引
parsedQuery查询条件
winningPlan最佳执行计划
stage查询方式
filter过滤条件
direction查询顺序
rejectedPlans拒绝的执行计划
serverInfomongodb服务器信息

executionStats

executionStats 模式的返回信息中包含了 queryPlanner 模式的所有字段,并且还包含了最佳执行计划的执行情况

db.getCollection('test').find({"user_id":3224484}).explain("executionStats")

返回值如下:

{
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "w.test",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "user_id" : {
                "$eq" : 3224484.0
            }
        },
        "winningPlan" : {
            "stage" : "FETCH",
            "inputStage" : {
                "stage" : "IXSCAN",
                "keyPattern" : {
                    "user_id" : -1
                },
                "indexName" : "idx_user_id",
                "isMultiKey" : false,
                "multiKeyPaths" : {
                    "user_id" : []
                },
                "isUnique" : false,
                "isSparse" : false,
                "isPartial" : false,
                "indexVersion" : 2,
                "direction" : "forward",
                "indexBounds" : {
                    "user_id" : [ 
                        "[3224484.0, 3224484.0]"
                    ]
                }
            }
        },
        "rejectedPlans" : []
    },
    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : 4,
        "executionTimeMillis" : 0,
        "totalKeysExamined" : 4,
        "totalDocsExamined" : 4,
        "executionStages" : {
            "stage" : "FETCH",
            "nReturned" : 4,
            "executionTimeMillisEstimate" : 0,
            "works" : 5,
            "advanced" : 4,
            "needTime" : 0,
            "needYield" : 0,
            "saveState" : 0,
            "restoreState" : 0,
            "isEOF" : 1,
            "invalidates" : 0,
            "docsExamined" : 4,
            "alreadyHasObj" : 0,
            "inputStage" : {
                "stage" : "IXSCAN",
                "nReturned" : 4,
                "executionTimeMillisEstimate" : 0,
                "works" : 5,
                "advanced" : 4,
                "needTime" : 0,
                "needYield" : 0,
                "saveState" : 0,
                "restoreState" : 0,
                "isEOF" : 1,
                "invalidates" : 0,
                "keyPattern" : {
                    "user_id" : -1
                },
                "indexName" : "idx_user_id",
                "isMultiKey" : false,
                "multiKeyPaths" : {
                    "user_id" : []
                },
                "isUnique" : false,
                "isSparse" : false,
                "isPartial" : false,
                "indexVersion" : 2,
                "direction" : "forward",
                "indexBounds" : {
                    "user_id" : [ 
                        "[3224484.0, 3224484.0]"
                    ]
                },
                "keysExamined" : 4,
                "seeks" : 1,
                "dupsTested" : 0,
                "dupsDropped" : 0,
                "seenInvalidated" : 0
            }
        }
    },
    "serverInfo" : {
        "host" : "localhost",
        "port" : 37100,
        "version" : "4.0.10",
        "gitVersion" : "c389e7f69f637f7a1ac3cc9fae843b635f20b766"
    },
    "ok" : 1.0,
    "operationTime" : Timestamp(1575536281, 1)
}
字段名称描述
winningPlan.inputStage用来描述子stage,并且为其父stage提供文档和索引关键字
winningPlan.inputStage.stage子查询方式
winningPlan.inputStage.keyPattern所扫描的index内容
winningPlan.inputStage.indexName索引名
winningPlan.inputStage.isMultiKey是否是Multikey。如果索引建立在array上,将是true
executionStats.executionSuccess是否执行成功
executionStats.nReturned返回的个数
executionStats.executionTimeMillis这条语句执行时间
executionStats.executionStages.executionTimeMillisEstimate检索文档获取数据的时间
executionStats.executionStages.inputStage.executionTimeMillisEstimate扫描获取数据的时间
executionStats.totalKeysExamined索引扫描次数
executionStats.totalDocsExamined文档扫描次数
executionStats.executionStages.isEOF是否到达 steam 结尾,1 或者 true 代表已到达结尾
executionStats.executionStages.works工作单元数,一个查询会分解成小的工作单元
executionStats.executionStages.advanced优先返回的结果数
executionStats.executionStages.docsExamined文档检查数

allPlansExecution

allPlansExecution返回的信息包含 executionStats 模式的内容,且包含 “allPlansExecution” : [ ] 块

“allPlansExecution” : [ ] 块信息格式如下:

"allPlansExecution" : [
      {
         "nReturned" : <int>,
         "executionTimeMillisEstimate" : <int>,
         "totalKeysExamined" : <int>,
         "totalDocsExamined" :<int>,
         "executionStages" : {
            "stage" : <STAGEA>,
            "nReturned" : <int>,
            "executionTimeMillisEstimate" : <int>,
            ...
            }
         }
      },
      ...
   ]

stage状态

状态描述
COLLSCAN全表扫描
IXSCAN索引扫描
FETCH根据索引检索指定文档
SHARD_MERGE将各个分片返回数据进行合并
SORT在内存中进行了排序
LIMIT使用limit限制返回数
SKIP使用skip进行跳过
IDHACK对_id进行查询
SHARDING_FILTER通过mongos对分片数据进行查询
COUNTSCANcount不使用Index进行count时的stage返回
COUNT_SCANcount使用了Index进行count时的stage返回
SUBPLA未使用到索引的$or查询的stage返回
TEXT使用全文索引进行查询时候的stage返回
PROJECTION限定返回字段时候stage的返回

执行计划的返回结果中尽量不要出现以下stage:

  • COLLSCAN(全表扫描)
  • SORT(使用sort但是无index)
  • 不合理的SKIP
  • SUBPLA(未用到index的$or)
  • COUNTSCAN(不使用index进行count)
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值