- Reference >
mongo
Shell Methods >- Query Plan Cache Methods >
- PlanCache.list()
PlanCache.list()¶
On this page
Definition¶
-
PlanCache.
list
(<pipeline>)¶ New in version 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.Parameter Type Description pipeline Array Optional. Aggregation pipeline to filter/process the query shapes.
You can run this method from a
mongod
ormongos
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.
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.
Consideration¶
Restrictions¶
PlanCache.list()
is not allowed in transactions.
Query Hash and Query Shapes¶
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
.
Read Preference¶
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.
Required Access¶
On systems running with authorization
, the user
must have the planCacheRead
privilege for the collection.
Examples¶
Note
- 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. - For details on the output, see $planCacheStats output.
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:
Return Information for All Entries in the Query Cache¶
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.
List Query Shapes¶
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.
Find Cache Entry Details for a Query Shape¶
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.