mongodb的explain

explain()是MongoDB的一个重要的查询论断工具,这个函数能够提供大量的与查询相关的信息,该函数会返回查询计划、执行状态、服务器信息,根据这些信息可以有针对性的对性能进行优化。

  1. explain()函数
  2. explain()返回信息
  3. explain()使用示例

1. explain()函数

cursor.explain(verbosity)

查看db.collection.find()的执行查询计划信息时,explain()的使用方法如下:

db.collection.find().explain()

explain()方法的参数如下:

  • verbose:{String},可选参数。指定冗长模式的解释输出,方式指定后会影响explain()的行为及输出信息。

    可选值有:"queryPlanner""executionStats""allPlansExecution",默认为"queryPlanner"

2. explain()返回信息

explain()的返回值有:

queryPlanner(查询计划):查询优化选择的计划细节和被拒绝的计划。其可能包括以下值:
queryPlanner.namespace-一个字符串,运行查询的指定命名空间
queryPlanner.indexFilterSet-一个布尔什,表示MongoDB在查询中是否使用索引过滤
queryPlanner.winningPlan-由查询优化选择的计划文档
winningPlan.stage-表示查询阶段的字符串
winningPlan.inputStage-表示子过程的文档
winningPlan.inputStages-表示子过程的文档数组
queryPlanner.rejectedPlans-被查询优化备选并被拒绝的计划数组
executionStats,(执行状态):被选中执行计划和被拒绝执行计划的详细说明:
queryPlanner.nReturned-匹配查询条件的文档数
queryPlanner.executionTimeMillis-计划选择和查询执行所需的总时间(毫秒数)
queryPlanner.totalKeysExamined-扫描的索引总数
queryPlanner.totalDocsExamined-扫描的文档总数
queryPlanner.totalDocsExamined-扫描的文档总数
queryPlanner.executionStages-显示执行成功细节的查询阶段树
executionStages.works-指定查询执行阶段执行的“工作单元”的数量
executionStages.advanced-返回的中间结果数
executionStages.needTime-未将中间结果推进到其父级的工作周期数
executionStages.needYield-存储层要求查询系统产生的锁的次数
executionStages.isEOF-指定执行阶段是否已到达流结束
queryPlanner.allPlansExecution-包含在计划选择阶段期间捕获的部分执行信息,包括选择计划和拒绝计划
serverInfo,(服务器信息):MongoDB实例的相关信息:
serverInfo.winningPlan-使用的执行计划
winningPlan.shards-包括每个访问片的queryPlanner和serverInfo的文档数组

3. explain()使用示例

有一个users集合,现查询其'status'值为'1'的数据,并查看执行情况:

> db.users.find({status:1}).explain()
{
  "cursor" : "BasicCursor",
  "isMultiKey" : false,
  "n" : 1,
  "nscannedObjects" : 1,
  "nscanned" : 1,
  "nscannedObjectsAllPlans" : 1,
  "nscannedAllPlans" : 1,
  "scanAndOrder" : false,
  "indexOnly" : false,
  "nYields" : 0,
  "nChunkSkips" : 0,
  "millis" : 9,
  "server" : "localhost:27017",
  "filterSet" : false
}
glc-test:PRIMARY> db.galleryimg.find().explain("executionStats")
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "gallery.galleryimg",
                "indexFilterSet" : false,
                "parsedQuery" : {

                },
                "winningPlan" : {
                        "stage" : "COLLSCAN",
                        "direction" : "forward"
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 7598,
                "executionTimeMillis" : 195,
                "totalKeysExamined" : 0,
                "totalDocsExamined" : 7598,
                "executionStages" : {
                        "stage" : "COLLSCAN",
                        "nReturned" : 7598,
                        "executionTimeMillisEstimate" : 58,
                        "works" : 7600,
                        "advanced" : 7598,
                        "needTime" : 1,
                        "needYield" : 0,
                        "saveState" : 59,
                        "restoreState" : 59,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "direction" : "forward",
                        "docsExamined" : 7598
                }
        },
        "serverInfo" : {
                "host" : "xxx",
                "port" : 28042,
                "version" : "4.0.17-10",
                "gitVersion" : "c7a95fa71cb1a23bc9b73401b178dfb7cff8cc7d"
        },
        "ok" : 1,
        "operationTime" : Timestamp(1604636678, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1604636678, 1),
                "signature" : {
                        "hash" : BinData(0,"++H4QYguEzP2QqAKEycUwT7izww="),
                        "keyId" : NumberLong("6856584343653974019")
                }
        }
}

###################################################################

原文地址:https://www.cnblogs.com/igoodful/p/13936269.html