衡量索引使用

在本页面

db.orders.aggregate( [ { $indexStats: { } } ] )

也可参考:

$indexStatsarrow-up-right

使用 explain()返回查询计划

executionStatsarrow-up-right 模式中使用db.collection.explain()arrow-up-rightcursor.explain()arrow-up-right方法返回关于查询过程的统计信息,包括使用的索引、扫描的文档数量以及查询处理所用的时间(以毫秒为单位)。

allPlansExecutionarrow-up-right 模式下使用 db.collection.explain()arrow-up-rightcursor.explain()arrow-up-right方法查看计划选择期间收集的部分执行统计信息。

也可参考:

planCacheKeyarrow-up-right

使用hint()控制索引使用

要强制MongoDB为db.collection.find()arrow-up-right操作使用特定的索引,请使用hint()方法指定该索引。将hint()arrow-up-right方法附加到find()arrow-up-right方法。考虑下面的例子:

代码示例如下:

查看使用特定索引的执行统计信息,在db.collection.find()arrow-up-right语句追加的hint()arrow-up-right方法后跟随cursor.explain()arrow-up-right方法,代码示例如下:

或者在db.collection.explain().find()arrow-up-right方法后追加hint()arrow-up-right方法。

hint()arrow-up-right方法中声明$natural参数,避免MongoDB在查询过程中使用任何索引。

索引指标

除了$indexStatsarrow-up-right聚合阶段,MongoDB提供了各种索引统计数据,您可能想要考虑分析索引使用您的数据库:

译者:程哲欣

最后更新于