Docs HomeMongoDB Manual

planCacheClear

Definition定义

planCacheClear

Removes cached query plans for a collection. Specify a query shape to remove cached query plans for that shape. Omit the 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.如果不需要方便,或者需要额外的返回字段,请使用数据库命令。

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描述
querydocumentOptional.可选的。The query predicate of the 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.只有谓词的结构,包括字段名称,对形状有意义;查询谓词中的值无关紧要。
projectiondocumentOptional.可选的。The projection associated with the query shape.
sortdocumentOptional.可选的。The sort associated with the query shape.
commentanyOptional.可选的。
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).
New in version 4.4. 4.4版新增。

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.

Examples实例

Clear Cached Plans for a Query Shape

If a collection orders has the following query shape:

{
"query" : { "qty" : { "$gt" : 10 } },
"sort" : { "ord_date" : 1 },
"projection" : { },
"queryHash" : "9AAD95BE" // Available starting in MongoDB 4.2
}

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:

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