explain
On this page本页内容
Definition定义
explain
-
The
explain
command provides information on the execution of the following commands:aggregate
,count
,distinct
,find
,findAndModify
,delete
,mapReduce
, andupdate
.TipIn
mongosh
, this command can also be run through thedb.collection.explain()
andcursor.explain()
helper methods.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.
Syntax语法
The command has the following syntax:该命令具有以下语法:
db.runCommand(
{
explain: <command>,
verbosity: <string>,
comment: <any>
}
)
Command Fields命令字段
The command takes the following fields:该命令包含以下字段:
explain | document | aggregate , count , distinct , find , findAndModify , delete , mapReduce , and update . |
verbosity | string | explain . The mode affects the behavior of explain and determines the amount of information to return.explain 的模式的字符串。该模式影响explain 的行为,并决定返回的信息量。
|
comment | any |
Note If you specify explain without a comment , it inherits any comment in the command specified to explain . |
Behavior行为
Verbosity Modes详细模式
The behavior of explain
and the amount of information returned depend on the verbosity
mode.explain
的行为和返回的信息量取决于详细程度模式。
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>
.
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>
. However, executionStats
does not provide query execution information for the rejected plans.
By default, explain
runs in "allPlansExecution"
verbosity mode.
MongoDB runs the query optimizer to choose the winning plan and executes the winning plan to completion. In MongoDB运行查询优化器来选择获胜计划,并执行获胜计划直至完成。在"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>
. The executionStats
includes the completed query execution information for the winning plan.
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.
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.
Restrictions限制
Starting in MongoDB 4.2, 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:
- run the explain in
queryPlanner
mode or - run the explain in
executionStats
mode orallPlansExecution
mode but without the$out
stage to return information for the stages that precede the$out
stage.
Output输出
explain
operations can return information regarding:操作可以返回关于以下内容的信息:
explainVersion
, the output format version (for example,"1"
);command
, which details the command being explained;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; andserverParameters
, 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.
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.
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:
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:
db.runCommand(
{
explain: { count: "products", query: { quantity: { $gt: 50 } } },
verbosity: "executionStats"
}
)
allPlansExecution
Mode
By default, explain
runs in "allPlansExecution"
verbosity mode. The following explain
command returns the queryPlanner
and executionStats
for all considered plans for an update
command:
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 } }
}
]
}
}
)