Docs 菜单

Docs 主页开发应用程序MongoDB Manual

$planCacheStats

在此页面上

  • 定义
  • 考虑因素
  • 输出
  • 举例
$planCacheStats

4.2 版本中的新增功能

返回集合的计划缓存信息。该阶段为每个计划缓存条目返回一个文档。

$planCacheStats阶段必须是管道中的第一个阶段。该阶段采用空文档作为参数,语法如下:

{ $planCacheStats: { } }

注意

4.4 变更

从版本4开始。 4 、

提示

另请参阅:

$planCacheStats 必须是聚合管道的第一阶段。

在使用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
}

每个文档都包含各种查询计划和执行统计信息,包括:

字段
说明
createdFromQuery

包含生成此缓存条目的特定查询的文档;即

{
"query" : <document>,
"sort" : <document>,
"projection" : <document>
}
isActive

指示条目处于活动状态还是非活动状态的布尔值。

  • 如果处于活动状态,则查询规划器当前正在使用该条目生成查询计划。

  • 如果处于非活动状态,则查询规划器当前未使用该条目生成查询计划。

提示

另请参阅:

表示查询结构的哈希值的十六进制字符串。 有关更多信息,请参阅explain.queryPlanner.queryHash

一个十六进制字符串,表示用于查找与此查询关联的计划缓存条目的键的哈希值。 计划缓存键是查询结构和该结构当前可用索引的函数。 有关详细信息,请参阅 explain.queryPlanner.planCacheKey

已缓存计划的详细信息。 请参阅explain.queryPlanner

works
在查询规划器评估候选计划的试用期内,查询执行计划执行的“工作单元”数量。 有关详细信息,请参阅 explain.executionStats.executionStages.works
timeOfCreation
条目的创建时间。

执行统计文档的数组。该数组包含每个候选计划的文档。

有关执行统计信息的详细信息,请参阅explain.executionStats

creationExecStats数组中列出的候选计划的分数数组。

indexFilterSet
一个布尔值,表示查询结构是否存在索引筛选器
estimatedSizeBytes

一个数字,描述计划缓存条目的估计大小(以字节为单位)。

版本 5.0 中的新增功能

从 MongoDB 5.0、4.4.3 和 4.2.12 开始,此字段可用。

返回计划缓存信息的mongod实例的主机名和端口。

在分片集群上运行时,该操作会从每个分片副本集中的单个成员返回计划缓存条目信息。该成员通过分主机字段进行标识。另请参阅读取偏好。

4.4 版本新增

$planCacheStats从中检索缓存条目的分片的名称。

仅当在分片集群上运行时才可用。

4.4 版本新增

本部分中的示例使用以下 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
}

另请参阅planCacheKeyqueryHash

← $out(聚合)