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

Query Plan Cache Commands - (Database and Collection Commands)

MongoDB stores recently used query plans in memory, known as the query plan cache. This cache stores different query plans for different operations. In this page, we will discuss the different commands available to manage the query plan cache in MongoDB.

Syntax

Query plan cache commands use the planCache* keyword and can be run against both the database and collection level. Here's a sample syntax:

db.runCommand({ planCacheClear: "collectionName" })
db.runCommand({ planCacheClear: { dbname: "databaseName", collname: "collectionName" } })

Example

Here's an example of a few query plan cache commands.

db.runCommand({ planCacheList: "collectionName" })

db.runCommand({ planCacheClear: "collectionName" })

db.runCommand({ planCacheClear: { dbname: "databaseName", collname: "collectionName" } })

Output

The output of each command varies depending on the specific command used. planCacheList lists cached query plans for the specified collection. planCacheClear removes cached query plans for the specified collection or database. All output is returned in JSON format.

Explanation

Query plan cache commands allow you to manage the cached query plans in MongoDB to optimize query performance. MongoDB uses the query plan cache to store recently used query plan for different operations. If a suitable query plan is found in the cache, it can save significant resources and improve query performance.

The planCacheList command lists cached query plans for a specific collection. This can be useful for reviewing frequently used queries and identifying potential performance issues.

The planCacheClear command removes cached query plans for a specific collection or database. This can be useful for clearing out the query plan cache and forcing MongoDB to generate new query plans, which can improve performance.

Use

Query plan cache commands are useful for managing the query plan cache in MongoDB and optimizing query performance. They allow you to review and clear cached query plans to ensure that MongoDB is using the most efficient query plan for each operation.

Important Points

  • Query plan cache commands use the planCache* keyword.
  • The planCacheList command lists cached query plans for a specific collection.
  • The planCacheClear command clears cached query plans for a specific collection or database.

Summary

In this page, we have discussed the different commands available to manage the query plan cache in MongoDB. We covered the syntax, example, output, explanation, use, important points, and summary of query plan cache commands. By managing the query plan cache effectively, you can optimize query performance and ensure that MongoDB is using the most efficient query plan for each operation.

Published on: