$planCacheStats
在此页面上
定义
$planCacheStats
4.2 版本中的新增功能。
返回集合的计划缓存信息。 该阶段为每个计划缓存条目返回一个文档。
$planCacheStats
阶段必须是管道中的第一个阶段。 该阶段采用空文档作为参数,语法如下:{ $planCacheStats: { } } 注意
4.4 变更
从版本 4.4 开始,
$planCacheStats
阶段可以在mongos
实例和mongod
实例上运行。 在4中。 2 、$planCacheStats
阶段只能在mongod
实例上运行。$planCacheStats
包括新字段:主机字段,以及针对mongos
运行时的分片字段。mongo
shell提供PlanCache.list()
方法作为$planCacheStats
聚合阶段的包装器。MongoDB 删除以下内容:
planCacheListPlans
和planCacheListQueryShapes
命令,以及PlanCache.getPlansByQuery()
和PlanCache.listQueryShapes()
方法。
Considerations
管道
$planCacheStats
必须是聚合管道的第一阶段。
限制
$planCacheStats
不允许:$planCacheStats
需要读关注级别"local"
。
访问控制
在使用 authorization
运行的系统上,用户必须具有该集合的 planCacheRead
权限。
读取偏好
$planCacheStats
在选择要返回计划缓存信息的主机时观察读取偏好。
应用程序可能会针对副本集的不同成员。 因此,每个副本集节点可能会收到不同的读取命令,并具有与其他节点不同的计划缓存信息。 不过,在副本集或分片集群上运行$planCacheStats
会遵循正常的读取偏好规则。 也就是说,在副本集上,该操作仅从副本集的一个成员收集计划缓存信息,而在分片集群上,该操作仅从每个分片副本集的一个成员收集计划缓存信息。
输出
对于每个计划缓存条目, $planCacheStats
阶段返回类似于以下内容的文档:
{ "createdFromQuery" : <document>, "queryHash" : <hexadecimal string>, "planCacheKey" : <hexadecimal string>, "isActive" : <boolean>, "works" : <NumberLong>, "cachedPlan" : { "stage" : <STAGE1>, "filter" : <document>, "inputStage" : { "stage" : <STAGE2>, ... } }, "timeOfCreation" : <date>, "creationExecStats" : [ // Exec Stats Document for each candidate plan { "nReturned" : <num>, "executionTimeMillisEstimate" : <num> "totalKeysExamined" : <num> "totalDocsExamined" :<num> "executionStages" : { "stage" : <STAGE A>, ... "inputStage" : { "stage" : <STAGE B>, ... } } }, ... ], "candidatePlanScores" : [ <number>, ... ], "indexFilterSet" : <boolean>, "estimatedSizeBytes" : <num>, // Available starting in MongoDB 5.0, 4.4.3, 4.2.12 "host" : <string>, // Available starting in MongoDB 4.4 "shard" : <string> // Available starting in MongoDB 4.4 if run on sharded cluster }
每个文档都包含各种查询计划和执行统计信息,包括:
字段 | 说明 | |||||
---|---|---|---|---|---|---|
包含生成此缓存条目的特定查询的文档;即
| ||||||
| ||||||
表示查询结构的哈希值的十六进制字符串。 有关更多信息,请参阅 | ||||||
一个十六进制字符串,表示用于查找与此查询关联的计划缓存条目的键的哈希值。 计划缓存键是查询结构和该结构当前可用索引的函数。 有关详细信息,请参阅
| ||||||
已缓存计划的详细信息。 请参阅 | ||||||
| 在查询规划器评估候选计划的试用期内,查询执行计划执行的“工作单元”数量。 有关详细信息,请参阅
| |||||
| 创建条目的时间。 | |||||
执行统计文档的数组。该数组包含每个候选计划的文档。 有关执行统计的详细信息,请参阅 | ||||||
| ||||||
| 一个布尔值,表示查询结构是否存在索引筛选器。 | |||||
| 一个数字,描述计划缓存条目的估计大小(以字节为单位)。 版本 5.0 中的新增功能。 从 MongoDB 5.0、4.4.3 和 4.2.12 开始,此字段可用。 | |||||
示例
本部分中的示例使用以下 orders
集合:
db.orders.insertMany( [ { "_id" : 1, "item" : "abc", "price" : NumberDecimal("12"), "quantity" : 2, "type": "apparel" }, { "_id" : 2, "item" : "jkl", "price" : NumberDecimal("20"), "quantity" : 1, "type": "electronics" }, { "_id" : 3, "item" : "abc", "price" : NumberDecimal("10"), "quantity" : 5, "type": "apparel" }, { "_id" : 4, "item" : "abc", "price" : NumberDecimal("8"), "quantity" : 10, "type": "apparel" }, { "_id" : 5, "item" : "jkl", "price" : NumberDecimal("15"), "quantity" : 15, "type": "electronics" } ] )
在集合上创建以下索引:
db.orders.createIndex( { item: 1 } ); db.orders.createIndex( { item: 1, quantity: 1 } ); db.orders.createIndex( { quantity: 1 } ); db.orders.createIndex( { quantity: 1, type: 1 } ); db.orders.createIndex( { item: 1, price: 1 }, { partialFilterExpression: { price: { $gte: NumberDecimal("10")} } } );
注意
索引 { item: 1, price: 1 }
是部分索引,仅索引 price
字段大于或等于 NumberDecimal("10")
的文档。
对集合运行一些查询:
db.orders.find( { item: "abc", price: { $gte: NumberDecimal("10") } } ) db.orders.find( { item: "abc", price: { $gte: NumberDecimal("5") } } ) db.orders.find( { quantity: { $gte: 20 } } ) db.orders.find( { quantity: { $gte: 5 }, type: "apparel" } )
返回查询缓存中所有条目的信息
以下聚合管道使用$planCacheStats
返回有关集合的计划缓存条目的信息:
db.orders.aggregate( [ { $planCacheStats: { } } ] )
该操作返回缓存中的所有条目:
{ // Plan Cache Entry 1 "createdFromQuery" : { "query" : { "quantity" : { "$gte" : 5 }, "type" : "apparel" }, "sort" : { }, "projection" : { } }, "queryHash" : "4D151C4C", "planCacheKey" : "DD67E353", "isActive" : false, "works" : NumberLong(4), "cachedPlan" : { ... }, "timeOfCreation" : ISODate("2020-02-06T18:15:44.849Z"), "creationExecStats" : [ { ... // Exec Stats for Candidate 1 }, { ... // Exec Stats for Candidate 2 } ], "candidatePlanScores" : [ 1.5002, 1.5002 ], "indexFilterSet" : false, "estimatedSizeBytes" : NumberLong(3160), // Available starting in MongoDB 5.0, 4.4.3, 4.2.12 "host" : "mongodb1.example.net:27018", // Available starting in MongoDB 4.4 "shard" : "shardA" // Available starting in MongoDB 4.4 if run on sharded cluster } { // Plan Cache Entry 2 "createdFromQuery" : { "query" : { "quantity" : { "$gte" : 20 } }, "sort" : { }, "projection" : { } }, "queryHash" : "23B19B75", "planCacheKey" : "6F23F858", "isActive" : false, "works" : NumberLong(1), "cachedPlan" : { ... }, "timeOfCreation" : ISODate("2020-02-06T18:15:44.454Z"), "creationExecStats" : [ { ... // Exec Stats for Candidate 1 }, { ... // Exec Stats for Candidate 2 } ], "candidatePlanScores" : [ 1.0002, 1.0002 ], "indexFilterSet" : false, "estimatedSizeBytes" : NumberLong(2539), // Available starting in MongoDB 5.0, 4.4.3, 4.2.12 "host" : "mongodb1.example.net:27018", // Available starting in MongoDB 4.4 "shard" : "shardA" // Available starting in MongoDB 4.4 if run on sharded cluster } { // Plan Cache Entry 3 "createdFromQuery" : { "query" : { "item" : "abc", "price" : { "$gte" : NumberDecimal("5") } }, "sort" : { }, "projection" : { } }, "queryHash" : "117A6B10", "planCacheKey" : "A1824628", "isActive" : true, "works" : NumberLong(4), "cachedPlan" : { ... }, "timeOfCreation" : ISODate("2020-02-06T18:15:44.452Z"), "creationExecStats" : [ { ... // Exec Stats for Candidate 1 }, { ... // Exec Stats for Candidate 2 } ], "candidatePlanScores" : [ 1.7502, 1.7502 ], "indexFilterSet" : false, "estimatedSizeBytes" : NumberLong(3183), // Available starting in MongoDB 5.0, 4.4.3, 4.2.12 "host" : "mongodb1.example.net:27018", // Available starting in MongoDB 4.4 "shard" : "shardA" // Available starting in MongoDB 4.4 if run on sharded cluster } { // Plan Cache Entry 4 "createdFromQuery" : { "query" : { "item" : "abc", "price" : { "$gte" : NumberDecimal("10") } }, "sort" : { }, "projection" : { } }, "queryHash" : "117A6B10", "planCacheKey" : "2E6E536B", "isActive" : true, "works" : NumberLong(3), "cachedPlan" : { ... }, "timeOfCreation" : ISODate("2020-02-06T18:15:44.449Z"), "creationExecStats" : [ { ... // Exec Stats for Candidate 1 }, { ... // Exec Stats for Candidate 2 }, { ... // Exec Stats for Candidate 3 } ], "candidatePlanScores" : [ 1.6668666666666665, 1.6668666666666665, 1.6668666666666665 ], "indexFilterSet" : false, "estimatedSizeBytes" : NumberLong(4653), // Available starting in MongoDB 5.0, 4.4.3, 4.2.12 "host" : "mongodb1.example.net:27018", // Available starting in MongoDB 4.4 "shard" : "shardA" // Available starting in MongoDB 4.4 if run on sharded cluster }
另请参阅 planCacheKey
。
列出查询结构
MongoDB 4.4 删除了已弃用的planCacheListQueryShapes
命令及其辅助方法PlanCache.listQueryShapes()
。
作为替代方案,您可以使用$planCacheStats
阶段获取具有缓存计划的所有查询结构的列表。
例如,以下使用$project
阶段仅输出createdFromQuery
字段和queryHash
字段。
db.orders.aggregate( [ { $planCacheStats: { } } , { $project: {createdFromQuery: 1, queryHash: 1 } } ] )
该操作返回以下查询结构:
{ "createdFromQuery" : { "query" : { "item" : "abc", "price" : { "$gte" : NumberDecimal("5") } }, "sort" : { }, "projection" : { } }, "queryHash" : "117A6B10" } { "createdFromQuery" : { "query" : { "quantity" : { "$gte" : 5 }, "type" : "apparel" }, "sort" : { }, "projection" : { } }, "queryHash" : "4D151C4C" } { "createdFromQuery" : { "query" : { "quantity" : { "$gte" : 20 } }, "sort" : { }, "projection" : { } }, "queryHash" : "23B19B75" } { "createdFromQuery" : { "query" : { "item" : "abc", "price" : { "$gte" : NumberDecimal("10") } }, "sort" : { }, "projection" : { } }, "queryHash" : "117A6B10" }
查找查询结构的缓存条目详细信息
要返回特定查询结构的计划缓存信息,可以在$planCacheStats
阶段之后跟上针对planCacheKey
字段的$match
。
以下聚合管道使用$planCacheStats
,后跟$match
和$project
来返回特定查询结构的特定信息:
db.orders.aggregate( [ { $planCacheStats: { } }, { $match: { planCacheKey: "DD67E353"} } ] )
该操作返回以下内容:
{ "createdFromQuery" : { "query" : { "quantity" : { "$gte" : 5 }, "type" : "apparel" }, "sort" : { }, "projection" : { } }, "queryHash" : "4D151C4C", "planCacheKey" : "DD67E353", "isActive" : false, "works" : NumberLong(4), "cachedPlan" : { "stage" : "FETCH", "filter" : { "type" : { "$eq" : "apparel" } }, "inputStage" : { "stage" : "IXSCAN", "keyPattern" : { "quantity" : 1 }, "indexName" : "quantity_1", "isMultiKey" : false, "multiKeyPaths" : { "quantity" : [ ] }, "isUnique" : false, "isSparse" : false, "isPartial" : false, "indexVersion" : 2, "direction" : "forward", "indexBounds" : { "quantity" : [ "[5.0, inf.0]" ] } } }, "timeOfCreation" : ISODate("2020-02-06T18:15:44.849Z"), "creationExecStats" : [ { "nReturned" : 2, "executionTimeMillisEstimate" : 0, "totalKeysExamined" : 3, "totalDocsExamined" : 3, "executionStages" : { "stage" : "FETCH", "filter" : { "type" : { "$eq" : "apparel" } }, "nReturned" : 2, "executionTimeMillisEstimate" : 0, "works" : 4, "advanced" : 2, "needTime" : 1, "needYield" : 0, "saveState" : 0, "restoreState" : 0, "isEOF" : 1, "docsExamined" : 3, "alreadyHasObj" : 0, "inputStage" : { "stage" : "IXSCAN", "nReturned" : 3, "executionTimeMillisEstimate" : 0, "works" : 4, "advanced" : 3, "needTime" : 0, "needYield" : 0, "saveState" : 0, "restoreState" : 0, "isEOF" : 1, "keyPattern" : { "quantity" : 1 }, "indexName" : "quantity_1", "isMultiKey" : false, "multiKeyPaths" : { "quantity" : [ ] }, "isUnique" : false, "isSparse" : false, "isPartial" : false, "indexVersion" : 2, "direction" : "forward", "indexBounds" : { "quantity" : [ "[5.0, inf.0]" ] }, "keysExamined" : 3, "seeks" : 1, "dupsTested" : 0, "dupsDropped" : 0 } } }, { "nReturned" : 2, "executionTimeMillisEstimate" : 0, "totalKeysExamined" : 3, "totalDocsExamined" : 2, "executionStages" : { "stage" : "FETCH", "nReturned" : 2, "executionTimeMillisEstimate" : 0, "works" : 4, "advanced" : 2, "needTime" : 1, "needYield" : 0, "saveState" : 0, "restoreState" : 0, "isEOF" : 1, "docsExamined" : 2, "alreadyHasObj" : 0, "inputStage" : { "stage" : "IXSCAN", "nReturned" : 2, "executionTimeMillisEstimate" : 0, "works" : 4, "advanced" : 2, "needTime" : 1, "needYield" : 0, "saveState" : 0, "restoreState" : 0, "isEOF" : 1, "keyPattern" : { "quantity" : 1, "type" : 1 }, "indexName" : "quantity_1_type_1", "isMultiKey" : false, "multiKeyPaths" : { "quantity" : [ ], "type" : [ ] }, "isUnique" : false, "isSparse" : false, "isPartial" : false, "indexVersion" : 2, "direction" : "forward", "indexBounds" : { "quantity" : [ "[5.0, inf.0]" ], "type" : [ "[\"apparel\", \"apparel\"]" ] }, "keysExamined" : 3, "seeks" : 2, "dupsTested" : 0, "dupsDropped" : 0 } } } ], "candidatePlanScores" : [ 1.5002, 1.5002 ], "indexFilterSet" : false, "estimatedSizeBytes" : NumberLong(3160), // Available starting in MongoDB 5.0, 4.4.3, 4.2.12 "host" : "mongodb1.example.net:27018", // Available starting in MongoDB 4.4 "shard" : "shardA" // Available starting in MongoDB 4.4 if run on sharded cluster }
另请参见 planCacheKey
和 queryHash
。