Why log shows PlanCacheKey even after clearing PlanCache

Why MongoDB logs show PlanCacheKey after clearing PlanCache using db.commaond(“PlanCacheClear”:collectionnanme)?

Is there any way to stop caching query plans?

Hello,

In the log, it’s expected to always see a PlanCacheKey as the query is executed already and even if the cache was cleared, since the query is executed a plan cache key is generated.

For example if I run a query for the first time in explain mode, I will notice a PlanCacheKey is created for that particular query shape.

If you want to verify if the PlanCacheClear command successfully cleared cached plans, you can list the current plans for a collection using PlanCache.list().

To demonstrate this idea, please check the below test:

// running the plan cache list returns a list of cached plans
db.orders.getPlanCache().list()
[
  {
    version: '1',
    queryHash: '8545567D',
    planCacheKey: 'F66BA4BD',
    isActive: false,
    works: Long("4"),
    timeOfCreation: ISODate("2023-02-24T16:55:47.486Z"),
    createdFromQuery: { query: { item: 'abc' }, sort: {}, projection: {} },
    cachedPlan: {
      stage: 'FETCH',
      inputStage: {
        stage: 'IXSCAN',
        keyPattern: { item: 1 },
//redacted the rest of the list for readability

// executed the planCacheClear command
Enterprise replset [direct: primary] test> db.runCommand( { planCacheClear: "orders" })
{
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp({ t: 1677258251, i: 1 }),
    signature: {
      hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0),
      keyId: Long("0")
    }
  },
  operationTime: Timestamp({ t: 1677258251, i: 1 })
}

// checking the plan cache list again to verify and it returns an empty
// array indicating that the plan cache is cleared
Enterprise replset [direct: primary] test>  db.orders.getPlanCache().list()
[]

For more information about PlanCache.list(), please check this documentation link.

Regards,
Mohamed Elshafey

I agree that PlanCacheKey is key of new entry to PlanCache .

Then, what would be reason of less execution time when the query is executed again after clearing PlanCache ?