Database Manual / Reference / Database Commands / Query Plan Cache

planCacheClear (database command)(数据库命令)

Definition定义

planCacheClear

Removes cached query plans for a collection. Specify a plan cache query shape to remove cached query plans for that shape. Omit the plan cache query shape to clear all cached query plans.删除集合的缓存查询计划。指定计划缓存查询形状以删除该形状的缓存查询计划。省略计划缓存查询形状以清除所有缓存的查询计划。

Tip

In mongosh, this command can also be run through the PlanCache.clear() and PlanCache.clearPlansByQuery() helper methods.mongosh中,此命令也可以通过PlanCache.clear()PlanCache.clearPlansByQuery()辅助方法运行。

Helper methods are convenient for mongosh users, but they may not return the same level of information as database commands. 助手方法对mongosh用户来说很方便,但它们可能不会返回与数据库命令相同级别的信息。In cases where the convenience is not needed or the additional return fields are required, use the database command.如果不需要便利性或需要额外的返回字段,请使用database命令。

Query Settings查询设置

Starting in MongoDB 8.0, use query settings instead of adding index filters. Index filters are deprecated starting in MongoDB 8.0.从MongoDB 8.0开始,使用查询设置,而不是添加索引筛选器。索引筛选器从MongoDB 8.0开始就被弃用。

Query settings have more functionality than index filters. Also, index filters aren't persistent and you cannot easily create index filters for all cluster nodes. 查询设置比索引筛选器具有更多功能。此外,索引筛选器不是持久的,您无法轻松为所有集群节点创建索引筛选器。To add query settings and explore examples, see setQuerySettings.要添加查询设置并探索示例,请参阅setQuerySettings

Compatibility兼容性

This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Important

This command is not supported in M0 and Flex clusters. For more information, see Unsupported Commands.M0和Flex集群不支持此命令。有关详细信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

The command has the following syntax:该命令具有以下语法:

db.runCommand(
{
planCacheClear: <collection>,
query: <query>,
sort: <sort>,
projection: <projection>,
comment: <any>
}
)

Command Fields命令字段

The command takes the following optional fields:该命令接受以下可选字段:

Field字段Type类型Description描述
querydocument文档Optional. 可选。The query predicate of the plan cache query shape. Only the structure of the predicate, including the field names, are significant to the shape; the values in the query predicate are insignificant.计划缓存查询形状的查询谓词。只有谓词的结构,包括字段名,对形状有意义;查询谓词中的值是不重要的。
projectiondocument文档Optional. 可选。The projection associated with the plan cache query shape.与计划缓存查询形状关联的投影。
sortdocument文档Optional. 可选。The sort associated with the plan cache query shape.与计划缓存查询形状关联的排序。
commentany任意

Optional. 可选。A user-provided comment to attach to this command. Once set, this comment appears alongside records of this command in the following locations:用户提供了要附加到此命令的注释。设置后,此注释将与此命令的记录一起出现在以下位置:

A comment can be any valid BSON type (string, integer, object, array, etc).注释可以是任何有效的BSON类型(字符串、整数、对象、数组等)。

To see the query shapes for which cached query plans exist, see Examples.要查看存在缓存查询计划的查询形状,请参阅示例

Required Access所需访问权限

On systems running with authorization, a user must have access that includes the planCacheWrite action.authorization运行的系统上,用户必须具有包含planCacheWrite操作的访问权限。

Examples示例

Clear Cached Plans for a Plan Cache Query Shape清除计划缓存查询形状的缓存计划

If a collection orders has the following plan cache query shape:如果集合orders具有以下计划缓存查询形状:

{
"query" : { "qty" : { "$gt" : 10 } },
"sort" : { "ord_date" : 1 },
"projection" : { },
"planCacheShapeHash" : "9AAD95BE"
}

Warning

Starting in MongoDB 8.0, the existing queryHash field is duplicated in a new field named planCacheShapeHash. 从MongoDB 8.0开始,现有的queryHash字段被复制到一个名为planCacheShapeHash的新字段中。If you're using an earlier MongoDB version, you'll only see the queryHash field. Future MongoDB versions will remove the deprecated queryHash field, and you'll need to use the planCacheShapeHash field instead.如果你使用的是早期的MongoDB版本,你只会看到queryHash字段。未来的MongoDB版本将删除弃用的queryHash字段,您将需要使用planCacheShapeHash字段。

The following operation clears the query plan cached for the shape:以下操作将清除为形状缓存的查询计划:

db.runCommand(
{
planCacheClear: "orders",
query: { "qty" : { "$gt" : 10 } },
sort: { "ord_date" : 1 }
}
)

Clear All Cached Plans for a Collection清除集合的所有缓存计划

The following example clears all the cached query plans for the orders collection:以下示例清除orders集合的所有缓存查询计划:

db.runCommand(
{
planCacheClear: "orders"
}
)