对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

PlanCache.list()(mongosh方法)

PlanCache.list(<pipeline>)

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

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

db.collection.getPlanCache().list(<pipeline>)
Parameter
类型
说明

阵列

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

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

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

相当于

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

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

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

此方法可用于以下环境中托管的部署:

  • MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务

从 MongoDB 8.0开始,使用查询设置而不是添加索引筛选器。 从 MongoDB 8.0开始,索引筛选器已弃用。

查询设置的功能比索引筛选器更多。 此外,索引筛选器不是持久性的,您无法轻松地为所有集群节点创建索引筛选器。 要添加查询设置并探索示例,请参阅setQuerySettings 。

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

为了帮助识别具有相同计划缓存查询结构的慢速查询,每个计划缓存查询结构都与一个查询哈希相关联。 计划缓存查询结构哈希是一个十六进制string ,表示查询结构的哈希值,并且仅依赖于查询结构。

注意

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

从MongoDB 8.0 开始,现有的 queryHash字段将复制到名为 planCacheShapeHash 的新字段中。 如果您使用的是早期MongoDB版本,则只能看到 queryHash字段。 未来的MongoDB版本将删除已弃用的 queryHash字段,您需要改用 planCacheShapeHash字段。

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

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

警告

从MongoDB 8.0 开始,现有的 queryHash字段将复制到名为 planCacheShapeHash 的新字段中。 如果您使用的是早期MongoDB版本,则只能看到 queryHash字段。 未来的MongoDB版本将删除已弃用的 queryHash字段,您需要改用 planCacheShapeHash字段。

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

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

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

注意

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

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

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

注意

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

以下返回已缓存orders集合计划的计划缓存查询结构:

db.orders.getPlanCache().list()

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

[ { "createdFromQuery" : { "query" : { "quantity" : { "$gte" : 5 }, "type" : "apparel" }, "sort" : { }, "projection" : { } }, "planCacheShapeHash" : "4D151C4C", "planCacheKey" : "DD67E353", "isActive" : false, "works" : Long(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, "peakTrackedMemBytes" : 795, // Available starting in MongoDB 8.3 "totalKeysExamined" : 3, "totalDocsExamined" : 2, "executionStages" : { "stage" : "FETCH", "nReturned" : 2, "executionTimeMillisEstimate" : 0, "works" : 4, ... } } ], "candidatePlanScores" : [ 1.5002, 1.5002 ], "indexFilterSet" : false, "estimatedSizeBytes" : Long(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" : { } }, "planCacheShapeHash" : "23B19B75", "planCacheKey" : "6F23F858", "isActive" : false, "works" : Long(1), ... }, { "createdFromQuery" : { "query" : { "item" : "abc", "price" : { "$gte" : Decimal128("5") } }, "sort" : { }, "projection" : { } }, "planCacheShapeHash" : "117A6B10", "planCacheKey" : "A1824628", "isActive" : false, "works" : Long(4), ... }, { "createdFromQuery" : { "query" : { "item" : "abc", "price" : { "$gte" : Decimal128("10") } }, "sort" : { }, "projection" : { } }, "planCacheShapeHash" : "117A6B10", "planCacheKey" : "2E6E536B", "isActive" : false, "works" : Long(3), ... }]

警告

从MongoDB 8.0 开始,现有的 queryHash字段将复制到名为 planCacheShapeHash 的新字段中。 如果您使用的是早期MongoDB版本,则只能看到 queryHash字段。 未来的MongoDB版本将删除已弃用的 queryHash字段,您需要改用 planCacheShapeHash字段。

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

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

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

警告

从MongoDB 8.0 开始,现有的 queryHash字段将复制到名为 planCacheShapeHash 的新字段中。 如果您使用的是早期MongoDB版本,则只能看到 queryHash字段。 未来的MongoDB版本将删除已弃用的 queryHash字段,您需要改用 planCacheShapeHash字段。

该操作返回以下计划缓存查询结构:

[ { "createdFromQuery" : { "query" : { "quantity" : { "$gte" : 5 }, "type" : "apparel" }, "sort" : { }, "projection" : { } }, "planCacheShapeHash" : "4D151C4C" }, { "createdFromQuery" : { "query" : { "quantity" : { "$gte" : 20 } }, "sort" : { }, "projection" : { } }, "planCacheShapeHash" : "23B19B75" }, { "createdFromQuery" : { "query" : { "item" : "abc", "price" : { "$gte" : Decimal128("5") } }, "sort" : { }, "projection" : { } }, "planCacheShapeHash" : "117A6B10" }, { "createdFromQuery" : { "query" : { "item" : "abc", "price" : { "$gte" : Decimal128("10") } }, "sort" : { }, "projection" : { } }, "planCacheShapeHash" : "117A6B10" }]

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

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

db.orders.getPlanCache().list([ { $match: { planCacheKey: "DD67E353"} } ] )

该操作返回以下内容:

[ { "createdFromQuery" : { "query" : { "quantity" : { "$gte" : 5 }, "type" : "apparel" }, "sort" : { }, "projection" : { } }, "planCacheShapeHash" : "4D151C4C", "planCacheKey" : "DD67E353", "isActive" : false, "works" : Long(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, "peakTrackedMemBytes" : 795, // Available starting in MongoDB 8.3 "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, "peakTrackedMemBytes" : 795, // Available starting in MongoDB 8.3 "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" : Long(3160), // Available starting in MongoDB 5.0 "host" : "mongodb1.example.net:27018", "shard" : "shardA" // Available if run on sharded cluster }]

警告

从MongoDB 8.0 开始,现有的 queryHash字段将复制到名为 planCacheShapeHash 的新字段中。 如果您使用的是早期MongoDB版本,则只能看到 queryHash字段。 未来的MongoDB版本将删除已弃用的 queryHash字段,您需要改用 planCacheShapeHash字段。

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