最后更新于3年前
在本页面
使用$indexStats度量索引使用
使用 聚合阶段获取关于集合中每个索引的使用情况的统计信息。例如,以下聚合操作返回关于orders集合中索引使用情况的统计信息:
orders
db.orders.aggregate( [ { $indexStats: { } } ] )
也可参考:
explain()
在 模式中使用 或方法返回关于查询过程的统计信息,包括使用的索引、扫描的文档数量以及查询处理所用的时间(以毫秒为单位)。
在 模式下使用 或方法查看计划选择期间收集的部分执行统计信息。
hint()
代码示例如下:
db.people.find( { name: "John Doe", zipcode: { $gt: "63000" } } ).hint( { zipcode: 1 } )
db.people.find( { name: "John Doe", zipcode: { $gt: "63000" } } ).hint( { zipcode: 1 } ).explain("executionStats")
db.people.explain("executionStats").find( { name: "John Doe", zipcode: { $gt: "63000" } } ).hint( { zipcode: 1 } )
db.people.find( { name: "John Doe", zipcode: { $gt: "63000" } } ).hint( { $natural: 1 } )
译者:程哲欣
要强制MongoDB为操作使用特定的索引,请使用hint()方法指定该索引。将方法附加到方法。考虑下面的例子:
查看使用特定索引的执行统计信息,在语句追加的方法后跟随方法,代码示例如下:
或者在方法后追加方法。
在方法中声明$natural参数,避免MongoDB在查询过程中使用任何索引。
$natural
除了聚合阶段,MongoDB提供了各种索引统计数据,您可能想要考虑分析索引使用您的数据库:
在方法的输出结果中:
和
在输出结果中
db.collection.find()
find()
cursor.explain()
db.collection.explain().find()
$indexStats
db.collection.explain()
serverStatus
metrics.queryExecutor.scanned
metrics.operation.scanAndOrder
collStats
totalIndexSize
indexSizes
dbStats
dbStats.indexes
dbStats.indexSize