- Reference >
- Operators >
- Aggregation Pipeline Stages >
- $planCacheStats
$planCacheStats¶
On this page
Definition¶
-
$planCacheStats
¶ New in version 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. 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 onmongos
instances as well as onmongod
instances. In 4.2,$planCacheStats
stage can only run onmongod
instances.$planCacheStats
includes new fields: the host field and, when run against amongos
, the shard field.mongo
shell provides the methodPlanCache.list()
as a wrapper for$planCacheStats
aggregation stage.MongoDB removes the following:
planCacheListPlans
andplanCacheListQueryShapes
commands, andPlanCache.getPlansByQuery()
andPlanCache.listQueryShapes()
methods.
Use
$planCacheStats
orPlanCache.list()
instead.
See also
Considerations¶
Pipeline¶
$planCacheStats
must be the first stage in an aggregation
pipeline.
Restrictions¶
$planCacheStats
is not allowed in:- transactions
$facet
aggregation stage
$planCacheStats
requires read concern levellocal
.
Access Control¶
On systems running with authorization
, the user
must have the planCacheRead
privilege for the collection.
Read Preference¶
$planCacheStats
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 $planCacheStats
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.
Output¶
For each plan cache entry, the $planCacheStats
stage returns a
document similar to the following:
Each document includes various query plan and execution stats, including:
Field | Description |
---|---|
createdFromQuery | A document that contains the specific query that resulted in this cache entry; i.e. |
isActive |
A boolean that indicates whether the entry is active or inactive.
See also |
queryHash | A hexadecimal string that represents the hash of the query
shape. For more information, see
|
planCacheKey | A hexadecimal string that represents the hash of the key used to
find the plan cache entry associated with this query. The plan
cache key is a function of both the query shape and the
currently available indexes for that shape. For more information, see
|
cachedPlan |
The details of the cached plan. See explain.queryPlanner . |
works |
The number of “work units” performed by the query execution plan
during the trial period when the query planner evaluates
candidate plans. For more information, see
explain.executionStats.executionStages.works |
timeOfCreation |
Time of creation for the entry. |
creationExecStats |
An array of execution stats documents. The array contains a document for each candidate plan. For details on the execution stats, see
|
candidatePlanScores |
An array of scores for the candidate plans listed in the
creationExecStats array. |
indexFilterSet |
A boolean that indicates whether the an index filter exists for the query shape. |
host | The hostname and port of the When run on a sharded cluster, the operation 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. New in version 4.4. |
shard | The name of the shard from which Only available if run on a sharded cluster. New in version 4.4. |
Examples¶
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 aggregation pipeline uses $planCacheStats
to
return information on the plan cache entries for the collection:
The operation returns all entries in the cache:
See also planCacheKey.
List Query Shapes¶
MongoDB 4.4 removes the deprecated planCacheListQueryShapes
command
and its helper method 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.
For example, the following uses the $project
stage to only
output the createdFromQuery
field and the queryHash
field.
The operation returns the following query shapes:
Find Cache Entry Details for a Query Shape¶
To return plan cache information for a particular query shape, the
$planCacheStats
stage can be followed by a
$match
on the planCacheKey
field.
The following aggregation pipeline uses $planCacheStats
followed by a $match
and $project
to return
specific information for a particular query shape:
The operation returns the following:
See also planCacheKey and queryHash.