On this page
度量 Metrics 使用
在本页面
使用$ indexStats 获取索引访问信息
使用$indexStats聚合阶段可获取有关集合每个索引的使用情况的统计信息。例如,以下聚合操作返回有关orders
集合上索引使用情况的统计信息:
db.orders.aggregate( [ { $indexStats: { } } ] )
See also
使用 explain()返回查询计划
在executionStats模式下使用db.collection.explain()或cursor.explain()方法返回有关查询过程的统计信息,包括使用的索引,扫描的文档数以及查询处理的时间(以毫秒为单位)。
在allPlansExecution模式下运行db.collection.explain()或cursor.explain()方法以查看在计划选择期间收集的部分执行统计信息。
db.collection.explain()提供有关其他操作的执行信息,例如db.collection.update()。有关详细信息,请参见db.collection.explain()。
控制索引与 hint()一起使用
要强制 MongoDB 对db.collection.find()操作使用特定的索引,请使用hint()方法指定索引。将hint()方法追加到find()方法。考虑以下示例:
db.people.find(
{ name: "John Doe", zipcode: { $gt: "63000" } }
).hint( { zipcode: 1 } )
要查看特定索引的执行统计信息,请在db.collection.find() hint()方法后附加cursor.explain(),例如:
db.people.find(
{ name: "John Doe", zipcode: { $gt: "63000" } }
).hint( { zipcode: 1 } ).explain("executionStats")
或者,将hint()方法附加到db.collection.explain().find():
db.people.explain("executionStats").find(
{ name: "John Doe", zipcode: { $gt: "63000" } }
).hint( { zipcode: 1 } )
为hint()方法指定$natural
运算符,以防止 MongoDB 使用* any *索引:
db.people.find(
{ name: "John Doe", zipcode: { $gt: "63000" } }
).hint( { $natural: 1 } )
实例索引使用报告
MongoDB 提供了许多在分析数据库的索引使用时可能需要考虑的索引使用和操作 Metrics: