cursor.explain()
On this page本页内容
Definition定义
cursor.explain(verbosity)
- Important
mongosh Method
This page documents a
mongosh
method. This is not the documentation for a language-specific driver, such as Node.js.For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.
Provides information on the query plan for the提供有关db.collection.find()
method.db.collection.find()
方法的查询计划的信息。Theexplain()
method has the following form:explain()
方法的形式如下:db.collection.find().explain()
Theexplain()
method has the following parameter:explain()
方法具有以下参数:Parameter参数Type类型Description描述verbose
string Optional.可选的。Specifies the verbosity mode for the explain output.指定解释输出的详细模式。The mode affects the behavior of该模式影响explain()
and determines the amount of information to return.explain()
的行为,并确定要返回的信息量。The possible modes are:可能的模式有:"queryPlanner"
,"executionStats"
, and"allPlansExecution"
."queryPlanner"
、"executionStats"
和"allPlansExecution"
。
Default mode is默认模式为"queryPlanner"
."queryPlanner"
。
For backwards compatibility with earlier versions of为了与早期版本的cursor.explain()
, MongoDB interpretstrue
as"allPlansExecution"
andfalse
as"queryPlanner"
.cursor.explain()
向后兼容,MongoDB将true
解释为"allPlansExecution"
,将false
解释为"queryPlanner"
。
For more information on the modes, see Verbosity Modes.有关模式的详细信息,请参阅详细模式。Theexplain()
method returns a document with the query plan and, optionally, the execution statistics.explain()
方法返回一个文档,其中包含查询计划和执行统计信息(可选)。
Behavior行为
Verbosity Modes详细模式
The behavior of cursor.explain()
and the amount of information returned depend on the verbosity
mode.cursor.explain()
的行为和返回的信息量取决于verbosity
(详细程度)模式。
queryPlanner
Mode模式
By default, 默认情况下,cursor.explain()
runs in queryPlanner
verbosity mode.cursor.explain()
在verbosity
(详细程度)模式queryPlanner
下运行。
MongoDB runs the query optimizer to choose the winning plan for the operation under evaluation. MongoDB运行查询优化器来为正在评估的操作选择获胜计划。cursor.explain()
returns the queryPlanner
information for the evaluated method.cursor.explain()
返回被评估方法的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运行查询优化器来选择获胜计划,执行获胜计划直到完成,并返回描述获胜计划执行情况的统计信息。
cursor.explain()
returns the queryPlanner
and executionStats
information for the evaluated method. cursor.explain()
returns the queryPlanner
返回已评估方法的queryPlanner
和executionStats
信息。However, 但是,executionStats
does not provide query execution information for the rejected plans.executionStats
不提供被拒绝计划的查询执行信息。
allPlansExecution
Mode模式
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返回描述获胜计划执行情况的统计信息,以及在计划选择过程中捕获的其他候选计划的统计信息。
cursor.explain()
returns the queryPlanner
and executionStats
information for the evaluated method. cursor.explain()
返回已评估方法的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
信息还包括在计划选择阶段为获胜和被拒绝的候选计划捕获的部分执行信息。
db.collection.explain().find()
db.collection.explain().find()
is similar to 类似于db.collection.find().explain()
with the following key differences:db.collection.find().explain()
,但有以下关键区别:
Thedb.collection.explain().find()
construct allows for the additional chaining of query modifiers.db.collection.explain().find()
构造允许额外的查询修饰符链接。For list of query modifiers, see db.collection.explain().find().help().有关查询修饰符的列表,请参阅db.collection.explain().find().help()
。Thedb.collection.find().explain()
returns a cursor, which requires a call to.next()
, or its alias.finish()
, to return theexplain()
results.db.collection.find().explain()
返回一个游标,该游标需要调用.next()
或其别名.finish()
来返回explain()
结果。If run interactively in如果在mongosh
,mongosh
automatically calls.finish()
to return the results.mongosh
中以交互方式运行,mongosh
会自动调用.finish()
返回结果。For scripts, however, you must explicitly call但是,对于脚本,必须显式调用.next()
, or.finish()
, to return the results..next()
或.finish()
才能返回结果。For list of cursor-related methods, see db.collection.explain().find().help().有关游标相关方法的列表,请参阅db.collection.explain().find().help()
。
See 有关详细信息,请参阅db.collection.explain()
for more information.db.collection.explain()
。
Output输出
cursor.explain()
operations can return information regarding:操作可以返回关于以下内容的信息:
explainVersion
, the output format version (for example,,输出格式版本(例如,"1"
);"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; and,提供了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. 解释输出受到BSON文档的最大嵌套深度的限制,即100个嵌套级别。Explain output that exceeds the limit is truncated.解释超过限制的输出被截断。
For details on the output, see Explain Results.有关输出的详细信息,请参阅解释结果。
Example实例
The following example runs 以下示例在"executionStats"详细模式下运行cursor.explain()
in "executionStats" verbosity mode to return the query planning and execution information for the specified db.collection.find()
operation:cursor.explain()
,以返回指定db.collection.find()
操作的查询计划和执行信息:
db.products.find(
{ quantity: { $gt: 50 }, category: "apparel" }
).explain("executionStats")