$explain

On this page本页内容

$explain

Deprecated since v3.0

Starting in v3.2, the $explain operator is deprecated in the mongo shell. 从v3.2开始,$explain运算符在mongo shell中不受欢迎。In the mongo shell, use db.collection.explain() or cursor.explain() instead.mongo shell中,请改用db.collection.explain()cursor.explain()

The $explain operator provides information on the query plan. $explain运算符提供有关查询计划的信息。It returns a document that describes the process and indexes used to return the query. 它返回一个文档,描述用于返回查询的过程和索引。This may provide useful insight when attempting to optimize a query. 这可能会在尝试优化查询时提供有用的见解。For details on the output, see cursor.explain().有关输出的详细信息,请参阅cursor.explain()

You can specify the $explain operator in either of the following forms:可以使用以下任一形式指定$explain运算符:

db.collection.find()._addSpecial( "$explain", 1 )
db.collection.find( { $query: {}, $explain: 1 } )

In the mongo shell, you also can retrieve query plan information through the explain() method:mongo shell中,还可以通过explain()方法检索查询计划信息:

db.collection.find().explain()

Behavior行为

$explain runs the actual query to determine the result. $explain运行实际查询以确定结果。Although there are some differences between running the query with $explain and running without, generally, the performance will be similar between the two. 虽然使用$explain运行查询和不使用$explain运行查询之间存在一些差异,但通常情况下,两者的性能相似。So, if the query is slow, the $explain operation is also slow.因此,如果查询速度慢,$explain操作也慢。

Additionally, the $explain operation reevaluates a set of candidate query plans, which may cause the $explain operation to perform differently than a normal query. 此外,$explain操作会重新评估一组候选查询计划,这可能会导致$explain操作的执行方式与普通查询不同。As a result, these operations generally provide an accurate account of how MongoDB would perform the query, but do not reflect the length of these queries.因此,这些操作通常可以准确地描述MongoDB将如何执行查询,但不能反映这些查询的长度。

See also参阅