db.collection.explain()

在本页面

Description

3.0 版中的新功能。

返回有关以下操作的查询计划的信息:aggregate(); count(); distinct(); find(); group(); remove();和update()方法。

要使用db.collection.explain(),请在db.collection.explain()后面附加可用于解释的方法:

db.collection.explain().<method(...)>

For example,

db.products.explain().remove( { category: "apparel" }, { justOne: true } )

有关更多示例,请参见Examples。有关可用于db.collection.explain()的方法的列表,请参见db.collection.explain().help()

db.collection.explain()方法具有以下参数:

ParameterTypeDescription
verbositystring可选的。指定说明输出的详细模式。该模式影响explain()的行为并确定要返回的信息量。可能的模式是:"queryPlanner""executionStats""allPlansExecution"


默认模式为"queryPlanner"
为了与cursor.explain()的早期版本向后兼容,MongoDB 会将true解释为"allPlansExecution",而将false解释为"queryPlanner"
有关这些模式的更多信息,请参见Verbosity Modes

Behavior

Verbosity Modes

db.collection.explain()的行为和返回的信息量取决于verbosity模式。

queryPlanner Mode

默认情况下,db.collection.explain()queryPlanner详细模式运行。

MongoDB 运行query optimizer来为评估中的操作选择胜出计划。 db.collection.explain()返回评估方法的queryPlanner信息。

executionStats Mode

MongoDB 运行query optimizer选择中奖计划,执行中奖计划直至完成,并返回描述中奖计划执行情况的统计信息。

对于写操作,db.collection.explain()返回有关将要执行的更新或删除操作的信息,但不会*将修改应用于数据库。

db.collection.explain()返回评估方法的queryPlannerexecutionStats信息。但是,executionStats不提供被拒绝计划的查询执行信息。

allPlansExecution Mode

MongoDB 运行query optimizer选择中奖计划,并执行中奖计划直至完成。在"allPlansExecution"模式下,MongoDB 返回描述获胜计划执行情况的统计信息以及在plan selection期间捕获的其他候选计划的统计信息。

对于写操作,db.collection.explain()返回有关将要执行的更新或删除操作的信息,但不会*将修改应用于数据库。

db.collection.explain()返回评估方法的queryPlannerexecutionStats信息。 executionStats包括中奖计划已完成查询执行信息。

如果查询优化器考虑了多个计划,则executionStats信息还包括在计划选择阶段期间针对获胜和被拒绝的候选计划捕获的* partial *执行信息。

explain() Mechanics

db.collection.explain()方法包装explain命令,是运行explain的首选方法。

db.collection.explain().find()db.collection.find().explain()类似,但有以下主要区别:

  • db.collection.explain().find()构造允许附加其他查询修饰符链接。有关查询修饰符的列表,请参见db.collection.explain().find().help()

  • db.collection.explain().find()返回一个游标,该游标需要调用.next()或其别名.finish()来返回explain()结果。如果在mongo shell 中以交互方式运行,则mongo shell 将自动调用.finish()以返回结果。但是,对于脚本,必须显式调用.next().finish()以返回结果。有关与光标相关的方法的列表,请参见db.collection.explain().find().help()

db.collection.explain().aggregate()等效于将explain选项传递给db.collection.aggregate()方法。

help()

要查看db.collection.explain()支持的操作列表,请运行:

db.collection.explain().help()

db.collection.explain().find()返回一个游标,该游标允许链接查询修饰符。要查看db.collection.explain().find()支持的查询修饰符以及与光标相关的方法的列表,请运行:

db.collection.explain().find().help()

您可以将多个修饰符链接到db.collection.explain().find()。有关示例,请参见用修饰符解释 find()

Examples

queryPlanner Mode

默认情况下,db.collection.explain()"queryPlanner"详细模式运行。

下面的示例以"queryPlanner"详细模式运行db.collection.explain(),以返回指定count()操作的查询计划信息:

db.products.explain().count( { quantity: { $gt: 50 } } )

executionStats Mode

以下示例以"executionStats"详细模式运行db.collection.explain(),以返回指定find()操作的查询计划和执行信息:

db.products.explain("executionStats").find(
   { quantity: { $gt: 50 }, category: "apparel" }
)

allPlansExecution Mode

以下示例以"allPlansExecution"详细模式运行db.collection.explain()db.collection.explain()为指定的update()操作返回所有考虑的计划的queryPlannerexecutionStats

Note

该 explain 的执行将不会修改数据,但会运行更新操作的查询谓词。对于候选计划,MongoDB 返回在计划选择阶段期间捕获的执行信息。

db.products.explain("allPlansExecution").update(
   { quantity: { $lt: 1000}, category: "apparel" },
   { $set: { reorder: true } }
)

用修饰符解释 find()

db.collection.explain().find()构造允许链接查询修饰符。例如,以下操作提供有关带有sort()hint()查询修饰符的find()方法的信息。

db.products.explain("executionStats").find(
   { quantity: { $gt: 50 }, category: "apparel" }
).sort( { quantity: -1 } ).hint( { category: 1, quantity: -1 } )

有关可用的查询修饰符的列表,请在mongo shell 中运行:

db.collection.explain().find().help()

迭代 explain()。find()返回游标

db.collection.explain().find()将光标返回到解释结果。如果在mongo shell 中交互运行,则mongo shell 将使用.next()方法自动迭代光标。但是,对于脚本,必须显式调用.next()(或其别名.finish())以返回结果:

var explainResult = db.products.explain().find( { category: "apparel" } ).next();

Output

db.collection.explain()操作可以返回有关以下内容的信息:

详细模式(即queryPlannerexecutionStatsallPlansExecution)确定结果是否包括executionStats以及executionStats是否包括在plan selection期间捕获的数据。

有关输出的详细信息,请参见Explain Results

对于具有 3.0 mongos版本和至少一个 2.6 mongod shard 的混合版本分片群集,当您在 3.0 mongo版本的 shell 中运行db.collection.explain()时,db.collection.explain()将使用$explain运算符重试以 2.6 格式返回结果。