Definition定义
explainTheexplaincommand provides information on the execution of the following commands:aggregate,count,distinct,find,findAndModify,delete,mapReduce, andupdate.explain命令提供有关执行以下命令的信息:aggregate、count、distinct、find、findAndModify、delete、mapReduce和update。Tip
In在mongosh, this command can also be run through thedb.collection.explain()andcursor.explain()helper methods.mongosh中,此命令也可以通过db.collection.explain()和cursor.explain()辅助方法运行。Helper methods are convenient for助手方法对mongoshusers, 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使用explainignores 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:该命令包含以下字段:
explain | aggregate, count, distinct, find, findAndModify, delete, mapReduce, and update.aggregate、count、distinct、find、findAndModify、delete、mapReduce和update。 | |
verbosity |
| |
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>的queryPlanner和executionStats信息。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>的queryPlanner和executionStats信息。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 Stable API V1支持explain command:explain命令的以下详细模式:
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()。相反,您可以:
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.queryPlanner、executionStats、allPlansExecution)确定结果是否包括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命令为更新命令返回所有考虑的计划的queryPlanner和executionStats:
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 } }
}
]
}
}
)