Definition定义
planCacheClearRemoves 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 thePlanCache.clear()andPlanCache.clearPlansByQuery()helper methods.mongosh中,此命令也可以通过PlanCache.clear()和PlanCache.clearPlansByQuery()辅助方法运行。Helper methods are convenient for助手方法对mongoshusers, 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:该命令接受以下可选字段:
query | ||
projection | ||
sort | ||
comment |
|
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 从MongoDB 8.0开始,现有的queryHash field is duplicated in a new field named planCacheShapeHash. queryHash字段被复制到一个名为planCacheShapeHash的新字段中。If you're using an earlier MongoDB version, you'll only see the 如果你使用的是早期的MongoDB版本,你只会看到queryHash field. Future MongoDB versions will remove the deprecated queryHash field, and you'll need to use the planCacheShapeHash field instead.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"
}
)