2

My case is that I run several queries in Mongo and measure the response time. I know that Mongo uses Memory Mapped files and cache some data in RAM.

If I create an index and run a query the execution time is 2,07 seconds. When I run the qeury again the execution time is 0,017 seconds.

This means that the file is mapped into memory by the OS on the first access and the OS will keep it in cache until it decides to page it out.

Because I want to make some benchmarks I want somehow to remove the mapped results from memory.

free -g:

               total       used       free     shared    buffers     cached

Mem:            29          29          0          0          0         29
-/+ buffers/cache:          0         29
Swap:            0          0          0

I try db.collection.getPlanCache().clear() but it doesnt work.

Then I try:

sudo service mongod stop

I reboot my machine

sync; echo 3 > /proc/sys/vm/drop_caches
sudo service mongod start

free -g:

               total       used       free     shared    buffers     cached

Mem:            29          0          0          0          0         0
-/+ buffers/cache:          0         29
Swap:            0          0          0

In above it seems that cache is free now. But when I execute again the query the execution time is 0,017 seconds. It seems that mongo has manage to cache or preserve the data somewhere. The desired execution time would be 2,07 seconds as I clear the cache.

Can anyone tell me how I can actually delete the cached results in Mongo?

  • ,I think you don't have planCacheWrite authorization access of that user. – Md Haidar Ali Khan May 31 '18 at 9:37
  • how can i ceck this? – Telperinquar May 31 '18 at 9:37
  • Could you confirm through "db.getRoles()" to that user , against to that database. – Md Haidar Ali Khan May 31 '18 at 9:39
  • when i run on primary db,getRoles() i get [ ] – Telperinquar May 31 '18 at 9:40
  • That means you don't have any particular role assigned to that database collections. – Md Haidar Ali Khan May 31 '18 at 9:43
0

As per MongoDB documentation here PlanCache.clear() removes all cached query plans for a collection.

The method is only available from the plan cache object of a specific collection; i.e.

db.collection.getPlanCache().clear()

Required Access

On systems running with authorization, a user must have access that includes the planCacheWrite action.

For your further refernce Manage Users and Roles in MongoDB and Collection-Level Access Control in MongoDB

  • ok thank you I will try this and I will inform if it goes well – Telperinquar May 31 '18 at 9:48
  • In mongod.conf I have set authorization:"disabled". This means that I don't need to create a role? – Telperinquar May 31 '18 at 13:37
0

This will give your all possible option to play with MongoDb Cache.

db.referenceDataPoints.getPlanCache().help(); 

PlanCache help

`db.referenceDataPoints.getPlanCache().help()` - show PlanCache help
`db.referenceDataPoints.getPlanCache().listQueryShapes()` - displays all query shapes in a collection
`db.referenceDataPoints.getPlanCache().clear()` - drops all cached queries in a collection
`db.referenceDataPoints.getPlanCache().clearPlansByQuery(query[, projection, sort, collation])` - drops query shape from plan cache
`db.referenceDataPoints.getPlanCache().getPlansByQuery(query[, projection, sort, collation])` - displays the cached plans for a query shape

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.