mongodb基础篇--explain执行计划

文章目录

      • queryPlanner
      • executionStats
      • allPlansExecution
      • stage状态

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

db.collection.find().explain()

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 拒绝的执行计划
serverInfo mongodb服务器信息

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" : ,
         "executionTimeMillisEstimate" : ,
         "totalKeysExamined" : ,
         "totalDocsExamined" :,
         "executionStages" : {
            "stage" : ,
            "nReturned" : ,
            "executionTimeMillisEstimate" : ,
            ...
            }
         }
      },
      ...
   ]

stage状态

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

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

  • COLLSCAN(全表扫描)
  • SORT(使用sort但是无index)
  • 不合理的SKIP
  • SUBPLA(未用到index的$or)
  • COUNTSCAN(不使用index进行count)

你可能感兴趣的:(mongodb)