In this short post, I would like to share how to log and identify slow queries in MongoDB. I have some experience on handling MongoDB on production for several years now. I found this slow queries problem often occur if your application gets bigger and bigger.
MongoDB have a very good documentation on this issue, how to analyze your database operations. You can check complete reference here :
There are two points there that says to enable your MongoDB profiler. By enabling the MongoDB profiler, you can see what operations that slows your application. It will be stored on new collection named “system.profile“.
Check out my quick slides below.
After you identifies what operations or queries that makes your application slows then you can find solutions that will solve it. Here are some technique that i usually applied to it.
1. Index. Do you already index the collections? Are you create the index correctly? (I will share more about index later)
2. Denormalize? Changing the schema design probably could help. This depends on your application data access pattern too. If your application read more often than writes, use schema that easy to read. Check my other post regarding schema design (with examples).
3. Scale up? Bigger memory, bigger CPU?
..and others. Please note this solutions might be different from one application and another. It is really DEPENDS on the application.
Thank you. I hope this helps!
Software Engineer, Blogger, Internet Savvy, Tech Enthusiast