On this page本页内容
$planCacheStats¶New in version 4.2.版本4.2中的新功能。
Returns plan cache information for a collection. 返回集合的计划缓存信息。The stage returns a document for each plan cache entry.阶段为每个计划缓存条目返回一个文档。
The $planCacheStats stage must be the first stage in the pipeline. $planCacheStats阶段必须是管道中的第一个阶段。The stage takes an empty document as a parameter and has the following syntax:该阶段将一个空文档作为参数并且语法如下所示:
4.4 Changes
Starting in version 4.4,
$planCacheStats stage can be run on mongos instances as well as on mongod instances. In 4.2, $planCacheStats stage can only run on mongod instances.$planCacheStats includes new fields: the host field and, when run against a mongos, the shard field.mongo shell provides the method PlanCache.list() as a wrapper for $planCacheStats aggregation stage.planCacheListPlans and planCacheListQueryShapes commands, andPlanCache.getPlansByQuery() and PlanCache.listQueryShapes() methods.Use $planCacheStats or PlanCache.list() instead.
See also参阅
$planCacheStats must be the first stage in an aggregation pipeline.$planCacheStats必须是聚合管道中的第一个阶段。
$planCacheStats$facet$planCacheStats requires read concern level local.$planCacheStats需要读关注级别local。On systems running with 在使用authorization, the user must have the planCacheRead privilege for the collection.authorization的系统上,用户必须具有集合的planCacheRead权限。
$planCacheStats observes the read preference in selecting the host(s) from which to return the plan cache information.$planCacheStats在选择要从中返回计划缓存信息的主机时会观察读取首选项。
Applications may target different members of a replica set. 应用程序可能以副本集的不同成员为目标。As such, each replica set member might receive different read commands and have plan cache information that differs from other members. 因此,每个副本集成员可能会收到不同的读取命令,并且具有不同于其他成员的计划缓存信息。Nevertheless, running 尽管如此,在副本集或分片集群上运行$planCacheStats on a replica set or a sharded cluster obeys the normal read preference rules. $planCacheStats遵循正常的读取首选规则。That is, on a replica set, the operation gathers plan cache information from just one member of replica set, and on a sharded cluster, the operation gathers plan cache information from just one member of each shard replica set.也就是说,在副本集上,该操作仅从副本集的一个成员收集计划缓存信息,在分片集群上,该操作仅从每个分片副本集的一个成员收集计划缓存信息。
For each plan cache entry, the 对于每个计划缓存条目,$planCacheStats stage returns a document similar to the following:$planCacheStats阶段将返回一个类似于以下内容的文档:
Each document includes various query plan and execution stats, including:每个文档都包含各种查询计划和执行统计信息,包括:
| createdFromQuery |
|
isActive |
|
| queryHash |
|
| planCacheKey |
|
cachedPlan |
explain.queryPlanner.explain.queryPlanner。 |
works |
explain.executionStats.executionStages.works |
timeOfCreation |
|
creationExecStats |
|
candidatePlanScores |
creationExecStats array.creationExecStats数组中列出的候选人计划的分数数组。 |
indexFilterSet |
|
| host |
|
| shard |
|
The examples in this section use the following 本节中的示例使用以下orders collection:orders集合:
Create the following indexes on the collection:在集合上创建以下索引:
Note
Index 索引{ item: 1, price: 1 } is a partial index and only indexes documents with price field greater than or equal to NumberDecimal("10").{item:1,price:1}是部分索引,仅对价格字段大于或等于NumberDecimal("10")的文档进行索引。
Run some queries against the collection:对集合运行一些查询:
The following aggregation pipeline uses 以下聚合管道使用$planCacheStats to return information on the plan cache entries for the collection:$planCacheStats返回有关集合的计划缓存项的信息:
The operation returns all entries in the cache:该操作将返回缓存中的所有条目:
See also planCacheKey.
MongoDB 4.4 removes the deprecated MongoDB 4.4删除了不推荐使用的planCacheListQueryShapes command and its helper method PlanCache.listQueryShapes().planCacheListQueryShapes命令及其助手方法PlanCache.listQueryShapes()。
As an alternative, you can use the 作为替代方案,您可以使用$planCacheStats stage to obtain a list of all of the query shapes for which there is a cached plan.$planCacheStats阶段来获取有缓存计划的所有查询形状的列表。
For example, the following uses the 例如,下面使用$project stage to only output the createdFromQuery field and the queryHash field.$project阶段仅输出createdFromQuery字段和queryHash字段。
The operation returns the following query shapes:该操作返回以下查询形状:
To return plan cache information for a particular query shape, the 要返回特定查询形状的计划缓存信息,$planCacheStats stage can be followed by a $match on the planCacheKey field.$planCacheStats阶段后面可以在planCacheKey字段上跟一个$match。
The following aggregation pipeline uses 以下聚合管道使用$planCacheStats followed by a $match and $project to return specific information for a particular query shape:$planCacheStats,后跟$match和$project来返回特定查询形状的特定信息:
The operation returns the following:该操作返回以下内容:
See also planCacheKey and queryHash.另请参见planCacheKey和queryHash。