Explain Results

在本页面

要返回有关查询计划的信息和查询计划的执行统计信息,MongoDB 提供:

explain结果将查询计划呈现为阶段树。

"winningPlan" : {
   "stage" : <STAGE1>,
   ...
   "inputStage" : {
      "stage" : <STAGE2>,
      ...
      "inputStage" : {
         "stage" : <STAGE3>,
         ...
      }
   }
},

每个阶段将其结果(即文档或索引键)传递给父节点。叶节点访问集合或索引。内部节点操纵由子节点产生的文档或索引键。根节点是 MongoDB 从中得出结果集的最后阶段。

阶段描述了操作;例如

Explain Output

以下各节列出了explain操作返回的一些关键字段。

Note

  • 字段列表并不意味着要详尽无遗,而是要突出显示早期解释版本中的一些关键字段更改。

  • 输出格式在各个发行版之间可能有所更改。

queryPlanner

queryPlanner信息详细说明了query optimizer选择的计划。

Unsharded Collections
Sharded Collections

For unsharded collections, explain returns the following queryPlanner information:

"queryPlanner" : {
   "plannerVersion" : <int>,
   "namespace" : <string>,
   "indexFilterSet" : <boolean>,
   "parsedQuery" : {
      ...
   },
   "winningPlan" : {
      "stage" : <STAGE1>,
      ...
      "inputStage" : {
         "stage" : <STAGE2>,
         ...
         "inputStage" : {
            ...
         }
      }
   },
   "rejectedPlans" : [
      <candidate plan 1>,
      ...
   ]
}

For sharded collections, explain includes the core query planner and server information for each accessed shard in the shards field:

"queryPlanner" : {
   "mongosPlannerVersion" : <int>,
   "winningPlan" : {
      "stage" : <STAGE1>,
      "shards" : [
         {
            "shardName" : <string>,
            "connectionString" : <string>,
            "serverInfo" : {
               "host" : <string>,
               "port" : <int>,
               "version" : <string>,
               "gitVersion" : <string>
            },
            "plannerVersion" : <int>,
            "namespace" : <string>,
            ...
            "winningPlan" : {
               "stage" : <STAGE2>,
               "inputStage" : {
                  "stage" : <STAGE3>
                  ...,
               }
            },
            "rejectedPlans" : [
               <candidate plan 1>,
               ...
            ]
         },
         ...
      ]
   }
}

每个阶段都包含特定于该阶段的信息。例如,一个IXSCAN阶段将包括索引范围以及特定于索引扫描的其他数据。如果一个阶段具有一个子阶段或多个子阶段,那么该阶段将具有 inputStage 或 inputStages。

executionStats

返回的executionStats信息详细说明了获胜计划的执行情况。为了在结果中包含executionStats,您必须运行以下任一命令中的解释:

Unsharded Collections
Sharded Collections

For unsharded collections, explain returns the following executionStats information:

"executionStats" : {
   "executionSuccess" : <boolean>,
   "nReturned" : <int>,
   "executionTimeMillis" : <int>,
   "totalKeysExamined" : <int>,
   "totalDocsExamined" : <int>,
   "executionStages" : {
      "stage" : <STAGE1>
      "nReturned" : <int>,
      "executionTimeMillisEstimate" : <int>,
      "works" : <int>,
      "advanced" : <int>,
      "needTime" : <int>,
      "needYield" : <int>,
      "saveState" : <int>,
      "restoreState" : <int>,
      "isEOF" : <boolean>,
      ...
      "inputStage" : {
         "stage" : <STAGE2>,
         "nReturned" : <int>,
         "executionTimeMillisEstimate" : <int>,
         ...
         "inputStage" : {
            ...
         }
      }
   },
   "allPlansExecution" : [
      {
         "nReturned" : <int>,
         "executionTimeMillisEstimate" : <int>,
         "totalKeysExamined" : <int>,
         "totalDocsExamined" :<int>,
         "executionStages" : {
            "stage" : <STAGEA>,
            "nReturned" : <int>,
            "executionTimeMillisEstimate" : <int>,
            ...
            "inputStage" : {
               "stage" : <STAGEB>,
               ...
               "inputStage" : {
                 ...
               }
            }
         }
      },
      ...
   ]
}

For sharded collections, explain includes the execution statistics for each accessed shard.

"executionStats" : {
   "nReturned" : <int>,
   "executionTimeMillis" : <int>,
   "totalKeysExamined" : <int>,
   "totalDocsExamined" : <int>,
   "executionStages" : {
      "stage" : <STAGE1>
      "nReturned" : <int>,
      "executionTimeMillis" : <int>,
      "totalKeysExamined" : <int>,
      "totalDocsExamined" : <int>,
      "totalChildMillis" : <NumberLong>,
      "shards" : [
         {
            "shardName" : <string>,
            "executionSuccess" : <boolean>,
            "executionStages" : {
               "stage" : <STAGE2>,
               "nReturned" : <int>,
               "executionTimeMillisEstimate" : <int>,
               ...
               "chunkSkips" : <int>,
               "inputStage" : {
                  "stage" : <STAGE3>,
                  ...
                  "inputStage" : {
                     ...
                  }
               }
            }
         },
         ...
      ]
   }
   "allPlansExecution" : [
      {
         "shardName" : <string>,
         "allPlans" : [
            {
               "nReturned" : <int>,
               "executionTimeMillisEstimate" : <int>,
               "totalKeysExamined" : <int>,
               "totalDocsExamined" :<int>,
               "executionStages" : {
                  "stage" : <STAGEA>,
                  "nReturned" : <int>,
                  "executionTimeMillisEstimate" : <int>,
                  ...
                  "inputStage" : {
                     "stage" : <STAGEB>,
                     ...
                     "inputStage" : {
                       ...
                     }
                  }
               }
            },
            ...
         ]
      },
      {
         "shardName" : <string>,
         "allPlans" : [
          ...
         ]
      },
      ...
   ]
}

Note

totalDocsExamined是指检查的文档总数,不是是指返回的文档数。例如,阶段可以检查文档以应用过滤器。如果文档被过滤掉,则说明该文档已经过检查,但不会作为查询结果集的一部分返回。

如果在查询执行期间对文档进行了多次检查,则totalDocsExamined对每次检查进行计数。也就是说,totalDocsExamined不是所检查的唯一*文档总数的计数。

每个阶段都包含特定于该阶段的执行信息。

考虑下面的示例,其中有一个字段x的索引,并且该集合包含 100 个文档,它们的x值从 1 到 100:

db.keys.find( { x : { $in : [ 3, 4, 50, 74, 75, 90 ] } } ).explain( "executionStats" )

该查询将扫描键34。然后它将扫描键5,检测到它越界,然后跳到下一个键50

continue 此过程,查询将扫描键 3、4、5、50、51、74、75、76、90 和 91.键5517691是仍在检查的边界键。 keysExamined的值为 10.

出现在COLLSCAN阶段以及从集合中检索文档的阶段(例如FETCH)

为了完成索引扫描,我们必须将索引光标搜索到新位置的次数。

serverInfo

Unsharded Collections
Sharded Collections

For unsharded collections, explain returns the following serverInfo information for the MongoDB instance:

"serverInfo" : {
   "host" : <string>,
   "port" : <int>,
   "version" : <string>,
   "gitVersion" : <string>
}

For sharded collections, explain returns the serverInfo for each accessed shard, and a top-level serverInfo object for the mongos.

"queryPlanner" : {
   ...
   "winningPlan" : {
      "stage" : <STAGE1>,
      "shards" : [
         {
            "shardName" : <string>,
            "connectionString" : <string>,
            "serverInfo" : {
               "host" : <string>,
               "port" : <int>,
               "version" : <string>,
               "gitVersion" : <string>
            },
            ...
         }
         ...
      ]
   }
},
"serverInfo" : {      // serverInfo for mongos (new in 3.6.16)
  "host" : <string>,
  "port" : <int>,
  "version" : <string>,
  "gitVersion" : <string>
}

Format Change

从 MongoDB 3.0 开始,explain结果的格式和字段与以前的版本已更改。以下列出了一些主要区别。

集合扫描与索引使用

如果查询计划者选择了集合扫描,则说明结果将包括COLLSCAN阶段。

如果查询计划者选择了索引,则说明结果包括IXSCAN阶段。该阶段包括诸如索引键样式,遍历方向和索引边界之类的信息。

在以前的 MongoDB 版本中,cursor.explain()返回了cursor字段,其值为:

有关收集扫描和索引扫描的执行统计信息的更多信息,请参见分析查询性能

Covered Queries

当索引涵盖查询时,MongoDB 既可以匹配查询条件,也可以仅使用索引键返回结果;即 MongoDB 无需检查集合中的文档即可返回结果。

当索引覆盖查询时,解释结果将具有IXSCAN阶段,该阶段不是FETCH阶段的后代,而在executionStats中,totalDocsExamined0

在早期版本的 MongoDB 中,cursor.explain()返回indexOnly字段以指示索引是否覆盖查询。

Index Intersection

对于索引交叉口计划,结果将包括AND_SORTED阶段或AND_HASH阶段以及带有详细说明索引的inputStages数组;例如。:

{
   "stage" : "AND_SORTED",
   "inputStages" : [
      {
         "stage" : "IXSCAN",
         ...
      },
      {
         "stage" : "IXSCAN",
         ...
      }
   ]
}

在以前的 MongoDB 版本中,cursor.explain()返回了cursor字段,其中Complex Plan的值用于索引交集。

$or Expression

如果 MongoDB 对$or表达式使用索引,则结果将包括OR阶段和inputStages数组,该数组详细描述了索引;例如。:

{
   "stage" : "OR",
   "inputStages" : [
      {
         "stage" : "IXSCAN",
         ...
      },
      {
         "stage" : "IXSCAN",
         ...
      },
      ...
   ]
}

在以前的 MongoDB 版本中,cursor.explain()返回了详细说明索引的clauses数组。

Sort Stage

如果 MongoDB 可以使用索引扫描来获取请求的排序 Sequences,则结果将不包括SORT阶段。否则,如果 MongoDB 无法使用索引进行排序,则explain结果将包含SORT阶段。

在 MongoDB 3.0 之前,cursor.explain()返回scanAndOrder字段以指定 MongoDB 是否可以使用索引 Sequences 返回排序的结果。

首页