定义
$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" : Decimal128("12"), "quantity" : 2, "type": "apparel" }, { "_id" : 2, "item" : "jkl", "price" : Decimal128("20"), "quantity" : 1, "type": "electronics" }, { "_id" : 3, "item" : "abc", "price" : Decimal128("10"), "quantity" : 5, "type": "apparel" }, { "_id" : 4, "item" : "abc", "price" : Decimal128("8"), "quantity" : 10, "type": "apparel" }, { "_id" : 5, "item" : "jkl", "price" : Decimal128("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: Decimal128("10")} } } );
注意
索引 { item: 1, price: 1 }
是部分索引,仅索引 price
字段大于或等于 Decimal128("10")
的文档。
对集合运行一些查询:
db.orders.find( { item: "abc", price: { $gte: Decimal128("10") } } ) db.orders.find( { item: "abc", price: { $gte: Decimal128("5") } } ) db.orders.find( { quantity: { $gte: 20 } } ) db.orders.find( { quantity: { $gte: 5 }, type: "apparel" } )
前面的查询是使用基于插槽的查询执行引擎完成的。
返回查询缓存中所有条目的信息
以下聚合管道使用 $planCacheStats
返回有关集合的计划缓存条目的信息:
db.orders.aggregate( [ { $planCacheStats: { } } ] )
输出:
[ { // Plan Cache Entry 1 version: '2', planCacheShapeHash: '478AD696', planCacheKey: '21AE23AD', isActive: true, works: Long("7"), timeOfCreation: ISODate("2023-05-22T20:33:49.031Z"), cachedPlan: { ... }, indexFilterSet: false, isPinned: false, estimatedSizeBytes: Long("8194"), host: 'mongodb1.example.net:27018' }, { // Plan Cache Entry 2 version: '2', planCacheShapeHash: '3D8AFDC6', planCacheKey: '1C2C4360', isActive: true, works: Long("6"), timeOfCreation: ISODate("2023-05-22T20:33:50.584Z"), cachedPlan: { ... }, indexFilterSet: false, isPinned: false, estimatedSizeBytes: Long("11547"), host: 'mongodb1.example.net:27018' }, { // Plan Cache Entry 3 version: '2', planCacheShapeHash: '27285F9B', planCacheKey: '20BB9404', isActive: true, works: Long("1"), timeOfCreation: ISODate("2023-05-22T20:33:49.051Z"), cachedPlan: { ... }, indexFilterSet: false, isPinned: false, estimatedSizeBytes: Long("7406"), host: 'mongodb1.example.net:27018' }, { // Plan Cache Entry 4 version: '2', planCacheShapeHash: '478AD696', planCacheKey: 'B1435201', isActive: true, works: Long("5"), timeOfCreation: ISODate("2023-05-22T20:33:49.009Z"), cachedPlan: { ... }, indexFilterSet: false, isPinned: false, estimatedSizeBytes: Long("7415"), host: 'mongodb1.example.net:27018' } ]
另请参阅 planCacheKey
。
查找查询哈希的缓存条目详细信息
要返回特定查询哈希的计划缓存信息,可以在$planCacheStats
阶段之后跟上针对planCacheKey
字段的$match
。
下面的聚合管道使用 $planCacheStats
和 $match
阶段来返回特定查询哈希值的特定信息:
db.orders.aggregate( [ { $planCacheStats: { } }, { $match: { planCacheKey: "B1435201"} } ] )
输出:
[ { version: '2', planCacheShapeHash: '478AD696', planCacheKey: 'B1435201', isActive: true, works: Long("5"), timeOfCreation: ISODate("2023-05-22T20:33:49.009Z"), cachedPlan: { slots: '$$RESULT=s11 env: { s3 = 1684787629009 (NOW), s6 = Nothing, s5 = Nothing, s1 = TimeZoneDatabase(Asia/Kuwait...Etc/UCT) (timeZoneDB), s10 = {"item" : 1, "price" : 1}, s2 = Nothing (SEARCH_META) }', stages: '[2] nlj inner [] [s4, s7, s8, s9, s10] \n' + ' left \n' + ' [1] cfilter {(exists(s5) && exists(s6))} \n' + ' [1] ixseek s5 s6 s9 s4 s7 s8 [] @"358822b7-c129-47b7-ad7f-40017a51b03c" @"item_1_price_1" true \n' + ' right \n' + ' [2] limit 1 \n' + ' [2] seek s4 s11 s12 s7 s8 s9 s10 none none [] @"358822b7-c129-47b7-ad7f-40017a51b03c" true false \n' }, indexFilterSet: false, isPinned: false, estimatedSizeBytes: Long("7415"), host: 'mongodb1.example.net:27018' } ]
另请参见 planCacheKey
和 queryHash
。
要使用MongoDB Node.js驾驶员将 $planCacheStats
阶段添加到聚合管道,请在管道对象中使用 $planCacheStats
操作符。
返回查询缓存中所有条目的信息
以下示例创建了一个管道阶段,该阶段返回有关集合的计划缓存条目的信息。然后,该示例运行聚合管道:
const pipeline = [{ $planCacheStats: {} }]; const cursor = collection.aggregate(pipeline); return cursor;
查找查询哈希的缓存条目详细信息
要返回特定查询哈希的计划缓存信息,请包含一个 $match
阶段,用于检查 planCacheKey
字段中的特定查询哈希。
以下示例创建一个管道,返回查询哈希值 "B1435201"
的信息。然后,该示例运行聚合管道:
const pipeline = [ $planCacheStats: {} }, { $match: { planCacheKey: "B1435201"} } ]; const cursor = collection.aggregate(pipeline); return cursor;