Docs 菜单

Docs 主页开发应用程序MongoDB Manual

PlanCache.list()

在此页面上

  • 定义
  • 考虑因素
  • 必需的访问权限
  • 举例
PlanCache.list(<pipeline>)

返回collection的计划缓存条目数组。

该方法只能从特定集合的plan cache object获得;即

db.collection.getPlanCache().list(<pipeline>)
范围
类型
说明
管道
阵列

可选。 用于筛选/处理查询结构的管道

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

PlanCache.list()方法封装了$planCacheStats聚合管道。也就是说,

db.collection.getPlanCache().list([<stage1>, <stage2>, ...] )

相当于

db.collection.aggregate([ <$planCacheStats stage>, <stage1>, <stage2>, ... ]).toArray();

有关输出的详细信息,请参阅$planCacheStats 输出。

并非所有查询都会自动将查询计划放入缓存中。如果当前不存在带有缓存查询计划的查询结构, PlanCache.list()会返回一个空数组。

提示

另请参阅:

PlanCache.list()事务 中不允许使用 。

为了帮助识别具有相同查询结构的慢查询,从 MongoDB 4开始。 2 ,每个查询结构都与一个 queryHash 关联。 queryHash是一个十六进制字符串,表示查询结构的哈希值,并且仅依赖于查询结构。

注意

与任何哈希函数一样,两个不同的查询结构可能会产生相同的哈希值。但是,不同查询结构之间不太可能发生哈希冲突。

查询优化器仅缓存可能具有多个可行计划的查询结构的计划。

计划缓存中的每个条目都与一个queryHash关联。

PlanCache.list()在选择要返回计划缓存信息的主机时观察读取偏好

应用程序可能会针对副本集的不同成员。因此,每个副本集节点可能会收到不同的读取命令,并具有与其他节点不同的计划缓存信息。不过,在副本集或分片集群上运行PlanCache.list()会遵循正常的读取偏好规则。也就是说,在副本集上,该操作仅从副本集的一个成员收集计划缓存信息,而在分片集群上,该操作仅从每个分片副本集的一个成员收集计划缓存信息。

在使用authorization运行的系统上,用户必须具有该集合的planCacheRead特权。

注意

  • 并非所有查询都会自动将查询计划放入缓存中。如果当前不存在带有缓存查询计划的查询结构, PlanCache.list()会返回一个空数组。

  • 有关输出的详细信息,请参阅$planCacheStats 输出。

本部分中的示例使用以下 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( { item: 1, price: 1 }, { partialFilterExpression: { price: { $gte: NumberDecimal("10")} } } );
db.orders.createIndex( { quantity: 1 } );
db.orders.createIndex( { quantity: 1, type: 1 } );

注意

索引{ 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" } )

以下返回已缓存collection计划的 查询结构 orders

db.orders.getPlanCache().list()

该方法返回当前在缓存中的查询结构的数组。在此示例中, orders collection 缓存 了与以下形状关联的查询计划:

[
{
"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-06T15:57:18.219Z"),
"creationExecStats" : [
{
"nReturned" : 2,
"executionTimeMillisEstimate" : 0,
"totalKeysExamined" : 3,
"totalDocsExamined" : 3,
"executionStages" : {
"stage" : "FETCH",
"filter" : { "type" : { "$eq" : "apparel" } },
"nReturned" : 2,
"executionTimeMillisEstimate" : 0,
"works" : 4,
...
}
},
{
"nReturned" : 2,
"executionTimeMillisEstimate" : 0,
"totalKeysExamined" : 3,
"totalDocsExamined" : 2,
"executionStages" : {
"stage" : "FETCH",
"nReturned" : 2,
"executionTimeMillisEstimate" : 0,
"works" : 4,
...
}
}
],
"candidatePlanScores" : [
1.5002,
1.5002
],
"indexFilterSet" : false,
"estimatedSizeBytes" : NumberLong(3160), // Available starting in MongoDB 5.0
"host" : "mongodb1.example.net:27018",
"shard" : "shardA" // Available if run on sharded cluster
},
{
"createdFromQuery" : {
"query" : { "quantity" : { "$gte" : 20 } },
"sort" : { },
"projection" : { }
},
"queryHash" : "23B19B75",
"planCacheKey" : "6F23F858",
"isActive" : false,
"works" : NumberLong(1),
...
},
{
"createdFromQuery" : {
"query" : { "item" : "abc", "price" : { "$gte" : NumberDecimal("5") } },
"sort" : { },
"projection" : { }
},
"queryHash" : "117A6B10",
"planCacheKey" : "A1824628",
"isActive" : false,
"works" : NumberLong(4),
...
},
{
"createdFromQuery" : {
"query" : { "item" : "abc", "price" : { "$gte" : NumberDecimal("10") } },
"sort" : { },
"projection" : { }
},
"queryHash" : "117A6B10",
"planCacheKey" : "2E6E536B",
"isActive" : false,
"works" : NumberLong(3),
...
}
]

有关输出的详细信息,请参阅$planCacheStats 输出。

要获取有缓存计划的所有查询结构的列表,可以使用PlanCache.list() 。例如,以下操作传入具有$project阶段的管道,以仅输出createdFromQuery字段和queryHash字段。

db.orders.getPlanCache().list( [ { $project: {createdFromQuery: 1, queryHash: 1 } } ] )

该操作返回以下查询结构:

[
{ "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("5") } }, "sort" : { }, "projection" : { } }, "queryHash" : "117A6B10" },
{ "createdFromQuery" : { "query" : { "item" : "abc", "price" : { "$gte" : NumberDecimal("10") } }, "sort" : { }, "projection" : { } }, "queryHash" : "117A6B10" }
]

有关输出的详细信息,请参阅$planCacheStats 输出。

要返回特定查询结构的计划缓存信息,请在管道中传递$matchplanCacheKey字段。

db.orders.getPlanCache().list([ { $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-11T17:14:33.873Z"),
"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
"host" : "mongodb1.example.net:27018",
"shard" : "shardA" // Available if run on sharded cluster
}
]

有关输出的详细信息,请参阅$planCacheStats 输出。

← PlanCache.help()