mongo-db
  1. mongo-db-query-plan-cache-methods

Query Plan Cache Methods (MongoDB Shell)

When a MongoDB server receives a query, it generates an execution plan to optimize the query performance. The execution plan is stored in a cache called the plan cache. In this page, we will learn about the MongoDB query plan cache methods.

Syntax

The MongoDB query plan cache provides several methods for interacting with cache entries. Here is the basic syntax for the query plan cache methods:

db.getCollection( '<collection_name>' ).getPlanCache().method();

Example

Let's say we have a collection orders with an index on the customer_id field. Here is an example of using the getPlansByQuery() method to get the execution plan for a query:

use mydb
db.getCollection('orders').find({customer_id: "my_customer_id"}).explain("executionStats")
db.getCollection('orders').getPlanCache().clear()
db.getCollection('orders').find({customer_id: "my_customer_id"}).explain("executionStats")

The first query executed will give us the execution plan for the query for customer_id field.

{
    "queryPlanner": {
        "plannerVersion 1,
        "namespace": "mydb.orders",
        "indexFilterSet": false,
        "parsedQuery": {
            "customer_id": {
                "$eq": "my_customer_id"
            }
        },
        "winningPlan": {
            "stage": "FETCH",
            "inputStage": {
                "stage": "IXSCAN",
                "keyPattern": {
                    "customer_id": 1
                },
                "indexName": "customer_id_1",
                "isMultiKey": false,
                "multiKeyPaths": {
                    "customer_id": []
                },
                "isUnique": false,
                "isSparse": false,
                "isPartial": false,
                "indexVersion": 2,
                "direction": "forward",
                "indexBounds": {
                    "customer_id": [
                        "my_customer_id",
                        "my_customer_id"
                    ]
                }
            }
        },
        "rejectedPlans": []
    },
    "executionStats": {
        "executionSuccess": true,
        "nReturned": 0,
        "executionTimeMillis": 0,
        "totalKeysExamined": 0,
        "totalDocsExamined": 0,
        ...
        ...
    }
}

The getPlanCache() method will give us the Cache plan for this query executed. Lets say, we want to clear this query execution plan from cache, then we can use the clear() method to do that.

db.getCollection('orders').getPlanCache().clear()

Now, if we execute the same query again, MongoDB will create a new execution plan.

db.getCollection('orders').find({customer_id: "my_customer_id"}).explain("executionStats")

Output

The output for each query will be different, depending on the query itself, the size of the collection, the number of cached plans, and other factors. The output will include information about the execution plan and statistics such as the number of documents scanned and the execution time.

Explanation

The query plan cache in MongoDB helps optimize query performance. The cache stores execution plans for frequently used queries. When the same query is executed multiple times, MongoDB uses the cached execution plan instead of generating a new plan each time.

MongoDB provides a set of methods in the shell to interact with the plan cache. These methods include methods to retrieve a list of cached plans, retrieve a plan by query shape, clear a query plan, and inspect plan metrics.

Use

The query plan cache methods in MongoDB help with optimization of your MongoDB instance. With these methods, you can examine cached plans, clear plans when needed, and get insights into plan statistics.

Important Points

  • MongoDB stores frequently used query execution plans in a query plan cache.
  • The query plan cache methods provide a way to interact with the cache.
  • You can inspect plan metrics, retrieve cached plans, and clear the plan cache.

Summary

In this page, we learned about the MongoDB query plan cache methods. We saw some basic syntax and examples for interacting with the plan cache, and we discussed the importance of the query plan cache for optimizing query performance in MongoDB. By using these methods in your MongoDB instances, you can optimize your queries and improve overall performance.

Published on: