On this page本页内容
explain
The explain
command provides information on the execution of the following commands: aggregate
, count
, distinct
, find
, findAndModify
, delete
, mapReduce
, and update
.explain
命令提供了以下命令的执行信息:aggregate
、count
、distinct
、find
、findAndModify
、delete
、mapReduce
和update
。
Although MongoDB provides the 尽管MongoDB提供了explain
command, the preferred method for running explain
is to use the db.collection.explain()
and cursor.explain()
helpers.explain
命令,但运行explain
的首选方法是使用db.collection.explain()
和cursor.explain()
。
The explain
command has the following syntax:explain
命令具有以下语法:
{
explain: <command>,
verbosity: <string>,
comment: <any>
}
The command takes the following fields:该命令接受以下字段:
explain | document | aggregate , count , distinct , find , findAndModify , delete , mapReduce , and update . aggregate 、count 、distinct 、find 、findAndModify 、delete 、mapReduce 和update 。 |
verbosity | string |
|
comment | any |
|
The behavior of explain
and the amount of information returned depend on the verbosity
mode.explain
的行为和返回的信息量取决于verbosity
模式。
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>
.<command>
的queryPlanner
信息。
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>
. explain
返回已求值<command>
的queryPlanner
和executionStats
信息。However, 但是,executionStats
does not provide query execution information for the rejected plans.executionStats
不提供被拒绝计划的查询执行信息。
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>
. explain
返回已求值<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
信息还包括在计划选择阶段为获胜和被拒绝的候选计划捕获的部分执行信息。
For write operations, the 对于写操作,explain
command returns information about the write operation that would be performed but does not actually modify the database.explain
命令返回有关将要执行但实际上不修改数据库的写操作的信息。
Starting in MongoDB 4.2, you cannot run the 从MongoDB 4.2开始,对于包含explain
command/db.collection.explain()
in executionStats
mode or allPlansExecution
mode for an aggregation pipeline
that contains the $out
stage. $out
阶段的聚合管道,不能在executionStats
模式或allPlansExecution
模式下运行explain
命令/db.collection.explain()
。Instead, you can either:相反,您可以:
explain
operations can return information regarding:操作可以返回以下信息:
explainVersion
"1"
);command
queryPlanner
executionStats
serverInfo
serverParameters
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. 解释输出受限于BSON文档的最大嵌套深度,即100层嵌套。Explain output that exceeds the limit is truncated.说明超出限制的输出被截断。
For details on the output, see Explain Results.有关输出的详细信息,请参阅解释结果。
queryPlanner
ModeThe 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
ModeThe 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
ModeBy 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
:
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 } } } ] } } )