On this page本页内容
db.collection.
explain
()¶mongo
Shell Method
This page documents the 本页记录了mongo
shell method, and does not refer to the MongoDB Node.js driver (or any other driver) method. mongo
shell方法,未提及MongoDB Node.js驱动程序(或任何其他驱动程序)方法。For corresponding MongoDB driver API, refer to your specific MongoDB driver documentation instead.有关相应的MongoDB驱动程序API,请参阅特定的MongoDB驱动程序文档。
Returns information on the query plan for the following methods:返回有关以下方法的查询计划的信息:
To use 要使用db.collection.explain()
, append one of the aforementioned methods to db.collection.explain()
:db.collection.explain()
,请将上述方法之一附加到db.collection.explain()
:
For example,例如
For more examples, see Examples. 有关更多示例,请参阅示例。See also db.collection.explain().help().另请参见db.collection.explain().help()。
The db.collection.explain()
method has the following parameter:db.collection.explain()
方法具有以下参数:
verbosity |
string |
|
The behavior of db.collection.explain()
and the amount of information returned depend on the verbosity
mode.db.collection.explain()
的行为和返回的信息量取决于verbosity
(详细)模式。
queryPlanner
executionStats
allPlansExecution
By default, 默认情况下,db.collection.explain()
runs in queryPlanner
verbosity mode.db.collection.explain()
以queryPlanner
详细模式运行。
MongoDB runs the query optimizer to choose the winning plan for the operation under evaluation. MongoDB运行查询优化器,为正在评估的操作选择获胜计划。db.collection.explain()
returns the 返回已计算方法的queryPlanner
information for the evaluated method.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, 对于写操作,db.collection.explain()
returns information about the update or delete operations that would be performed, but does not apply the modifications to the database.db.collection.explain()
返回有关将要执行的更新或删除操作的信息,但不将修改应用于数据库。
db.collection.explain()
returns the 返回已评估方法的queryPlanner
and executionStats
information for the evaluated method. queryPlanner
和executionStats
信息。However, 但是,executionStats
does not provide query execution information for the rejected plans.executionStats
不提供被拒绝计划的查询执行信息。
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, 对于写操作,db.collection.explain()
returns information about the update or delete operations that would be performed, but does not apply the modifications to the database.db.collection.explain()
返回有关将要执行的更新或删除操作的信息,但不将修改应用于数据库。
db.collection.explain()
returns the 返回已评估方法的queryPlanner
and executionStats
information for the evaluated method. 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, 对于写操作,db.collection.explain()
returns information about the write operation that would be performed but does not actually modify the database.db.collection.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
模式或allPlainsExecution
模式中运行explain
命令/db.collection.explain()
。Instead, you can either:但是,你可以:
explain()
The db.collection.explain()
method wraps the explain
command and is the preferred way to run explain
.db.collection.explain()
方法包装explain
命令,是运行explain
的首选方法。
db.collection.explain().find()
is similar to 与db.collection.find().explain()
with the following key differences:db.collection.find().explain()
类似,但有以下关键区别:
db.collection.explain().find()
construct allows for the additional chaining of query modifiers. db.collection.explain().find()
构造允许查询修饰符的附加链接。db.collection.explain().find().help()
。db.collection.explain().find()
returns a cursor, which requires a call to .next()
, or its alias .finish()
, to return the explain()
results. db.collection.explain().find()
返回一个游标,需要调用.next()
或其别名.finish()
才能返回explain()
结果。mongo
shell, the mongo
shell automatically calls .finish()
to return the results. mongo
shell中以交互方式运行,mongo
shell会自动调用.finish()
以返回结果。.next()
, or .finish()
, to return the results. .next()
或.finish()
,才能返回结果。db.collection.explain().find().help()
。db.collection.explain().aggregate()
is equivalent to passing the explain option to the 相当于将db.collection.aggregate()
method.explain
选项传递给db.collection.aggregate()
方法。
help()
¶To see the list of operations supported by 要查看db.collection.explain()
, run:db.collection.explain()
支持的操作列表,请运行:
db.collection.explain().find()
returns a cursor, which allows for the chaining of query modifiers. 返回一个游标,该游标允许查询修饰符的链接。To see the list of query modifiers supported by 要查看db.collection.explain().find()
as well as cursor-related methods, run:db.collection.explain().find()
支持的查询修饰符列表以及与游标相关的方法,请运行:
You can chain multiple modifiers to 可以将多个修饰符链接到db.collection.explain().find()
. db.collection.explain().find()
。For an example, see Explain find() with Modifiers.有关示例,请参阅用修饰符解释find()
。
queryPlanner
By default, 默认情况下,db.collection.explain()
runs in "queryPlanner"
verbosity mode.db.collection.explain()
以"queryPlanner"
详细模式运行。
The following example runs 以下示例在db.collection.explain()
in "queryPlanner"
verbosity mode to return the query planning information for the specified count()
operation:"queryPlanner"
详细模式下运行db.collection.explain()
,以返回指定count()
操作的查询计划信息:
executionStats
The following example runs 以下示例在db.collection.explain()
in "executionStats"
verbosity mode to return the query planning and execution information for the specified find()
operation:"executionStats"
详细模式下运行db.collection.explain()
,以返回指定find()
操作的查询计划和执行信息:
allPlansExecution
The following example runs 以下示例以db.collection.explain()
in "allPlansExecution"
verbosity mode. "allPlansExecution"
详细模式运行db.collection.explain()
。The db.collection.explain()
returns the queryPlanner
and executionStats
for all considered plans for the specified update()
operation:db.collection.explain()
为指定的update()
操作返回所有考虑的计划的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返回在计划选择阶段捕获的执行信息。
find()
with Modifiersfind()
¶db.collection.explain().find()
construct allows for the chaining of query modifiers. 构造允许查询修饰符的链接。For example, the following operation provides information on the 例如,以下操作使用find()
method with sort()
and hint()
query modifiers.sort()
和hint()
查询修饰符提供有关find()
方法的信息。
For a list of query modifiers available, run in the 有关可用查询修饰符的列表,请在mongo
shell:mongo
shell中运行:
explain().find()
Return Cursorexplain().find()
返回游标¶db.collection.explain().find()
returns a cursor to the explain results. 将游标返回到解释结果。If run interactively in the 如果在mongo
shell, the mongo
shell automatically iterates the cursor using the .next()
method. mongo
shell中以交互方式运行,mongo
shell将使用.next()
方法自动迭代游标。For scripts, however, you must explicitly call 但是,对于脚本,必须显式调用.next()
(or its alias .finish()
) to return the results:.next()
(或其别名.finish()
)以返回结果:
db.collection.explain()
operations can return information regarding:操作可以返回有关以下内容的信息:
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
是否包括在计划选择期间捕获的数据。
For details on the output, see Explain Results.有关输出的详细信息,请参阅解释结果。