Database Manual / Reference / Database Commands / Diagnostics

explain (database command数据库命令)

Definition定义

explain

The explain command provides information on the execution of the following commands: aggregate, count, distinct, find, findAndModify, delete, mapReduce, and update.explain命令提供有关执行以下命令的信息:aggregatecountdistinctfindfindAndModifydeletemapReduceupdate

Tip

In mongosh, this command can also be run through the db.collection.explain() and cursor.explain() helper methods.mongosh中,此命令也可以通过db.collection.explain()cursor.explain()辅助方法运行。

Helper methods are convenient for mongosh users, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.助手方法对mongosh用户来说很方便,但它们可能不会返回与数据库命令相同级别的信息。如果不需要便利性或需要额外的返回字段,请使用database命令。

Note

Using explain ignores all existing plan cache entries and prevents the MongoDB query planner from creating a new plan cache entry.使用explain会忽略所有现有的计划缓存条目,并阻止MongoDB查询计划器创建新的计划缓存条目的操作。

Compatibility兼容性

This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Note

This command is supported in all MongoDB Atlas clusters. 所有MongoDB Atlas集群都支持此命令。For information on Atlas support for all commands, see Unsupported Commands.有关Atlas支持所有命令的信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Required Access所需访问权限

To use explain, you must have permission to run the underlying command.要使用explain,您必须具有运行底层命令的权限。

Syntax语法

The command has the following syntax:该命令具有以下语法:

db.runCommand(
{
explain: <command>,
verbosity: <string>,
comment: <any>
}
)

Command Fields命令字段

The command takes the following fields:该命令包含以下字段:

Field字段Type类型Description描述
explaindocument文档A document specifying the command for which to return the execution information. 指定要返回执行信息的命令的文档。For details on the specific command document, see aggregate, count, distinct, find, findAndModify, delete, mapReduce, and update.有关特定命令文档的详细信息,请参阅aggregatecountdistinctfindfindAndModifydeletemapReduceupdate
verbositystring字符串

Optional. 可选。A string specifying the mode in which to run explain. 指定运行explain模式的字符串。The mode affects the behavior of explain and determines the amount of information to return.模式影响explain的行为,并决定要返回的信息量。

The possible modes are:可能的模式有:

  • "queryPlanner"
  • "executionStats"
  • "allPlansExecution" (Default)(默认值)

For more information on the modes, see explain behavior.有关模式的更多信息,请参阅解释行为

commentany任意

Optional. 可选。A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:用户提供了要附加到此命令的注释。设置后,此注释将与此命令的记录一起出现在以下位置:

A comment can be any valid BSON type (string, integer, object, array, etc).注释可以是任何有效的BSON类型(字符串、整数、对象、数组等)。

If you specify explain without a comment, it inherits any comment in the command specified to explain.如果指定不带commentexplain,它将继承指定要explain的命令中的任何comment

Behavior行为

Verbosity Modes详细模式

The behavior of explain and the amount of information returned depend on the verbosity mode.explain的行为和返回的信息量取决于verbosity模式。

queryPlanner Mode

MongoDB runs the query optimizer to choose the winning plan for the operation under evaluation. MongoDB运行查询优化器,为评估中的操作选择获胜计划。explain returns the queryPlanner information for the evaluated <command>.explain返回已求值的<command>queryPlanner信息。

executionStats Mode

MongoDB runs the query optimizer to choose the winning plan, executes the winning plan to completion, and returns statistics describing the execution of the winning plan.MongoDB运行查询优化器以选择获胜计划,执行获胜计划直至完成,并返回描述获胜计划执行情况的统计数据。

For write operations, explain returns information about the update or delete operations that would be performed, but does not apply the modifications to the database.对于写操作,explain会返回有关将要执行的更新或删除操作的信息,但不会将修改应用于数据库。

explain returns the queryPlanner and executionStats information for the evaluated <command>. 返回已评估的<command>queryPlannerexecutionStats信息。However, executionStats does not provide query execution information for the rejected plans.但是,executionStats不提供被拒绝计划的查询执行信息。

allPlansExecution Mode(Default)

By default, explain runs in "allPlansExecution" verbosity mode.默认情况下,explain"allPlansExecution"详细模式运行。

MongoDB runs the query optimizer to choose the winning plan and executes the winning plan to completion. MongoDB运行查询优化器来选择获胜计划,并执行获胜计划直至完成。In "allPlansExecution" mode, MongoDB returns statistics describing the execution of the winning plan as well as statistics for the other candidate plans captured during plan selection."allPlansExecution"模式下,MongoDB返回描述获胜计划执行情况的统计数据,以及在计划选择过程中捕获的其他候选计划的统计数据。

For write operations, explain returns information about the update or delete operations that would be performed, but does not apply the modifications to the database.对于写操作,explain会返回有关将要执行的更新或删除操作的信息,但不会将修改应用于数据库。

explain returns the queryPlanner and executionStats information for the evaluated <command>. 返回已评估的<command>queryPlannerexecutionStats信息。The executionStats includes the completed query execution information for the winning plan.executionStats包括获胜计划的已完成查询执行信息。

If the query optimizer considered more than one plan, executionStats information also includes the partial execution information captured during the plan selection phase for both the winning and rejected candidate plans.如果查询优化器考虑了多个计划,executionStats信息还包括在计划选择阶段为获胜和被拒绝的候选计划捕获的部分执行信息。

Explain and Write Operations解释和编写操作

For write operations, the explain command returns information about the write operation that would be performed but does not actually modify the database.对于写操作,explain命令返回有关将要执行但实际上不会修改数据库的写操作的信息。

Stable API

The Stable API V1 supports the following verbosity modes for the explain command:Stable API V1支持explain命令的以下详细模式:

Warning

MongoDB does not guarantee any specific output format from the explain command, even when using the Stable API.MongoDB不保证explain命令的任何特定输出格式,即使在使用Stable API时也是如此。

Restrictions限制

You cannot run the explain command/db.collection.explain() in executionStats mode or allPlansExecution mode for an aggregation pipeline that contains the $out stage. Instead, you can either:对于包含$out阶段的聚合管道,您无法在executionStats模式或allPlansExecution模式下运行explain命令/db.collection.explain()。相反,您可以:

  • run the explain in queryPlanner mode orqueryPlanner模式下运行解释,或
  • run the explain in executionStats mode or allPlansExecution mode but without the $out stage to return information for the stages that precede the $out stage.executionStats模式或allPlansExecution模式下运行解释,但不运行$out阶段,以返回$out阶段之前阶段的信息。

Output输出

explain operations can return information regarding:操作可以返回以下信息:

  • explainVersion, the output format version (for example, "1").,输出格式版本(例如"1")。
  • command, which details the command being explained.,其中详细说明了正在解释的命令。
  • queryShapeHash, starting in MongoDB 8.0, which is a hexadecimal string with the hash of a query shape. ,从MongoDB 8.0开始,它是一个十六进制字符串,具有查询形状的哈希值。For details, see Query Shapes, Query Shape Hash, and explain.queryShapeHash.有关详细信息,请参阅查询形状查询形状哈希explain.queryShapeHash
  • queryPlanner, which details the plan selected by the query optimizer and lists the rejected plans.,其中详细说明了查询优化器选择的计划,并列出了被拒绝的计划。
  • executionStats, which details the execution of the winning plan and the rejected plans.,其中详细说明了获胜计划和被拒绝计划的执行情况。
  • serverInfo, which provides information on the MongoDB instance.,它提供有关MongoDB实例的信息。
  • serverParameters, which details internal parameters.,其中详细说明了内部参数。

The verbosity mode (i.e. queryPlanner, executionStats, allPlansExecution) determines whether the results include executionStats and whether executionStats includes data captured during plan selection.详细程度模式(即queryPlannerexecutionStatsallPlansExecution)确定结果是否包括executionStats,以及executionStats是否包括在计划选择过程中捕获的数据。

Explain output is limited by the maximum Nested Depth for BSON Documents, which is 100 levels of nesting. Explain output that exceeds the limit is truncated.解释输出受BSON文档的最大嵌套深度限制,即100级嵌套。解释超出限制的输出被截断。

For details on the output, see Explain Results.有关输出的详细信息,请参阅解释结果

Examples示例

queryPlanner Mode模式

The following explain command runs in "queryPlanner" verbosity mode to return the query planning information for a count command:以下explain命令在"queryPlanner"详细程度模式下运行,以返回count命令的查询计划信息:

db.runCommand(
{
explain: { count: "products", query: { quantity: { $gt: 50 } } },
verbosity: "queryPlanner"
}
)

executionStats Mode模式

The following explain operation runs in "executionStats" verbosity mode to return the query planning and execution information for a count command:以下explain操作在"executionStats"详细程度模式下运行,以返回count命令的查询计划和执行信息:

db.runCommand(
{
explain: { count: "products", query: { quantity: { $gt: 50 } } },
verbosity: "executionStats"
}
)

allPlansExecution Mode模式

By default, explain runs in "allPlansExecution" verbosity mode. 默认情况下,explain"allPlansExecution"详细模式运行。The following explain command returns the queryPlanner and executionStats for all considered plans for an update command:以下explain命令为更新命令返回所有考虑的计划的queryPlannerexecutionStats

Note

The execution of this explain will not modify data but runs the query predicate of the update operation. 执行此解释不会修改数据,而是运行更新操作的查询谓词。For candidate plans, MongoDB returns the execution information captured during the plan selection phase.对于候选计划,MongoDB返回在计划选择阶段捕获的执行信息。

db.runCommand(
{
explain: {
update: "products",
updates: [
{
q: { quantity: 1057, category: "apparel" },
u: { $set: { reorder: true } }
}
]
}
}
)