Javatpoint Logo
Javatpoint Logo

MongoDB Query Plan Cache Methods

#1. db.collection.getPlanCache()

Using the plan cache object of a collection allows you to access this method. We need to use db.collection.getPlanCache() method to retrieve the plan cache object. The method returns an interface that has been used for collection to access the query plan cache and provides a method to view and clear the query plan cache.

The query shapes can be cached by the query optimizer that has more than one implementable plan.

The following methods are available through the interface:

PlanCache.help: This method is accessible through the plan cache object of the specified collection an displays the methods available for the specified collection's query plan cache, i.e., db.collection.getPlanCache().help().

PlanCache.listQueryShapes(): This method is accessible through the plan cache object of the specified collection and displays the query shapes for which cached query plan exists, i.e. db.collection.getPlanCache().listQueryShapes().

PlanCache.getPlansByQuery(): This method is accessible through the plan cache object of the specified collection and displays the cached query plans for the argumented query shape., i.e. db.collection.getPlanCache().getPlansByQuery().

PlanCache.clearPlansByQuery(): This method is accessible through the plan cache object of a specific collection and clears the cached query plans for the argumented query shape., i.e. db.collection.getPlanCache().clearPlansByQuery()

PlanCache.clear(): This method is accessible through the plan cache object of a specific collection and clears all the cached query plans for a collection., i.e. db.collection.getPlanCache().clear()

MongoDB Query Plan Cache Methods

#2. PlanCache.clear()

The clear method is only available from the objects of plan cache to remove all cached query plans for the specified collection.

For example - Clearing the cache for the student collection.


MongoDB Query Plan Cache Methods

#3. PlanCache.clearPlansByQuery()

This method clears the cached query plans for the specified query shape and available from the plan cache object of a specific collection; i.e.

The PlanCache.clearPlansByQuery() method accepts the following parameters:

Query document: This parameter is the query predicate of the query shape. The structure of the predicate and the field names are significant to the shape and the values in the query predicate are insignificant.

Projection document: It is the optional parameter, required if specifying the sort parameter. It can be used for the projection associated with the query shape.

Example

If a collection orders has the following query shape:

The following operation removes the query plan cached for the shape:

Output:

MongoDB Query Plan Cache Methods

#4. PlanCache.help()

Displays the methods available to view and modify a collection's query plan cache. The method is only available from the plan cache object of a specific collection; i.e.

#5. PlanCache.listQueryShapes()

The method is deprecated from the version 4.2 and was used to display the query shapes for which the cached query plans exist. You can use this method if you are using the older version of MongoDB. All the query shapes are associated with query hash to help in identification of the queries that are slow with the same query shape.

Syntax:

Example:

The order collection has the cashed plan associated with the query shape. The method will return an array of the query shape that is currently inside the cache.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA