On this page本页内容
PlanCache.
list
(<pipeline>)¶New in version 4.4.版本4.4中的新功能。
Returns an array of plan cache entries for a collection.
The method is only available from the plan cache object
of a specific collection; i.e.
pipeline | Array | Optional. Aggregation pipeline to filter/process the query shapes. |
You can run this method from a mongod
or mongos
instance. When run on a sharded cluster, the method returns plan cache entry information from a single member in each shard replica set. This member is identified with the shard and host fields. See also Read Preference.
The method PlanCache.list()
wraps the $planCacheStats
aggregation pipeline. That is,
is equivalent to
For details on the output, see $planCacheStats output.
See also参阅
Not all queries automatically place a query plan in the cache. PlanCache.list()
returns an empty array if there are currently no query shapes with cached query plans.
PlanCache.list()
is not allowed in transactions.
To help identify slow queries with the same query shape, starting in MongoDB 4.2, each query shape is associated with a queryHash. The queryHash
is a hexadecimal string that represents a hash of the query shape and is dependent only on the query shape.
Note
As with any hash function, two different query shapes may result in the same hash value. However, the occurrence of hash collisions between different query shapes is unlikely.
The query optimizer only caches the plans for those query shapes that can have more than one viable plan.
Each entry in the plan cache is associated with a queryHash
.
PlanCache.list()
observes the read preference in selecting the host(s) from which to return the plan cache information.
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 PlanCache.list()
on a replica set or a sharded cluster obeys the normal read preference rules. 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.
On systems running with authorization
, the user must have the planCacheRead
privilege for the collection.
Note
PlanCache.list()
returns an empty array if there are currently no query shapes with cached query plans.The examples in this section use the following orders
collection:
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")
.
Run some queries against the collection:
The following returns the query shapes that have cached plans for the orders
collection:
The method returns an array of the query shapes currently in the cache. In the example, the orders
collection had cached query plans associated with the following shapes:
For details on the output, see $planCacheStats output.
MongoDB 4.4 removes the deprecated planCacheListQueryShapes
command and its helper method PlanCache.listQueryShapes()
.
As an alternative, you can use the PlanCache.list()
to obtain a list of all of the query shapes for which there is a cached plan. For example, the following operation passes in a pipeline with a $project
stage to only output the createdFromQuery field and the queryHash field.
The operation returns the following query shapes:
For details on the output, see $planCacheStats output.
To return plan cache information for a particular query shape, pass in a pipeline with a $match
on the planCacheKey field.
The operation returns the following:
For details on the output, see $planCacheStats output.