Docs HomeMongoDB Manual

Measure Index Use衡量索引使用

Get Index Access Information with $indexStats使用$indexStats获取索引访问信息

Use the $indexStats aggregation stage to get statistics regarding the use of each index for a collection. 使用$indexStats聚合阶段可以获取有关集合中每个索引使用情况的统计信息。For example, the following aggregation operation returns statistics on the index use on the orders collection:例如,以下聚合操作返回orders集合上使用的索引的统计信息:

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

See also: 另请参阅:

Return Query Plan with explain()返回带explain()的查询计划

Use the db.collection.explain() or the cursor.explain() method in executionStats mode to return statistics about the query process, including the index used, the number of documents scanned, and the time the query takes to process in milliseconds.executionStats模式中使用db.collection.explain()cursor.explain()

Run db.collection.explain() or the cursor.explain() method in allPlansExecution mode to view partial execution statistics collected during plan selection.allPlansExecution模式下运行db.collection.explain()cursor.explain()

Tip

See also: 另请参阅:

Control Index Use with hint()控件索引与hint()一起使用

To force MongoDB to use a particular index for a db.collection.find() operation, specify the index with the hint() method. 要强制MongoDB为db.collection.find()操作使用特定索引,请使用hint()方法指定索引。Append the hint() method to the find() method. Consider the following example:hint()方法附加到find()方法。考虑以下示例:

db.people.find(
{ name: "John Doe", zipcode: { $gt: "63000" } }
).hint( { zipcode: 1 } )

To view the execution statistics for a specific index, append to the db.collection.find() the hint() method followed by cursor.explain(), e.g.:要查看特定索引的执行统计信息,请在db.collection.find()后面追加hint()方法,然后再追加cursor.explain(),例如:

db.people.find(
{ name: "John Doe", zipcode: { $gt: "63000" } }
).hint( { zipcode: 1 } ).explain("executionStats")

Or, append hint() method to db.collection.explain().find():或者,将hint()方法附加到db.collection.explain().find()

db.people.explain("executionStats").find(
{ name: "John Doe", zipcode: { $gt: "63000" } }
).hint( { zipcode: 1 } )

Specify the $natural operator to the hint() method to prevent MongoDB from using any index:hint()方法指定$natural运算符,以防止MongoDB使用任何索引:

db.people.find(
{ name: "John Doe", zipcode: { $gt: "63000" } }
).hint( { $natural: 1 } )

Index Metrics索引度量

In addition to the $indexStats aggregation stage, MongoDB provides various index statistics that you may want to consider when analyzing index use for your database:除了$indexStats聚合阶段外,MongoDB还提供了各种索引统计信息,您在分析数据库的索引使用情况时可能需要考虑这些统计信息: