测量索引的使用情况

获取索引访问信息$indexStats

使用$indexStats聚合阶段获取有关集合的每个索引的使用情况的统计信息。例如,以下聚合操作返回orders集合上索引使用情况的统计信息:

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

提示:

也可以看看:

$indexStats

返回查询计划explain()

executionStats模式下使用db.collection.explain()或 方法返回有关查询过程的统计信息,包括使用的索引、扫描的文档数以及查询处理所花费的时间(以毫秒为单位)。cursor.explain()

运行db.collection.explain()allPlansExecutioncursor.explain()模式 下的方法 ,查看计划选择过程中收集的部分执行统计信息。

提示

也可以看看:

planCacheKey

控制索引 与以下内容一起使用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 } )

$natural为该方法指定运算符hint() 以防止 MongoDB 使用任何索引:

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

索引指标

除了$indexStats聚合阶段之外,MongoDB 还提供各种索引统计信息,您在分析数据库索引使用情况时可能需要考虑这些统计信息:

Copyright © 上海锦木信息技术有限公司 all right reserved,powered by Gitbook文件修订时间: 2023-09-01 17:10:26

results matching ""

    No results matching ""