NoSQL Zone is brought to you in partnership with:

' ! Moshe Kaplan constantly helps successful firms getting to the next level and he is thrilled to uncover some of his secrets. Mr. Kaplan is a seasoned project management and cloud technologies lecturer. He is also known to be a cloud and SCRUM evangelist Moshe is a Co-Founder. He was a R&D Director at Essence Security, led RockeTier and served as a board member in the IGT and as a department head at a top IDF IT unit. Moshe holds M.Sc and B.Sc from TAU. Moshe is a DZone MVB and is not an employee of DZone and has posted 59 posts at DZone. You can read more from them at their website. View Full User Profile

MongoDB Index Tuning and Dex

  • submit to reddit

One of the first things to deal with when your system is rolled into production (or when you have your acceptance tests) is checking the system performance performance and tuning it.
When we talk about databases, we usually talk about index tuning, and MongoDB is no different.

How to Detect the Slow Queries
First, enable your MongoDB slow queries log. It's easy, just run db.setProfilingLevel(1, 1) from the command line.

How to Analyze the Slow Queries
Well, you can use the built-in queries (after all, the profiling is saved in a MongoDB collection). However, Dex, a tool from MongoLab, can help you shorten the time to index...

It's a Python-based product that can be easily installed.  It provides a list of recommended indexes based on MongoLab best practices.

How to Install and Run

  1. Install python: yum -y install python
  2. Install pip
  3. Install Dex using pip: pip install dex
  4. Create a log file: touch mongodb.log
  5. Run it: sudo dex -p -v -f mongodb.log mongodb://user:password@serverDNS:serverPort/database. Don't forget to use the various flags to get errors in case of wrong configurations.

Should We Just Copy and Paste the New Indexes?
As you may know, every added index results in a performance degradation when INSERT, DELETE, and UPDATE statements are performed. Therefore, we should carefully select the indexes to be added:

  1. We should check that the new indexes do not overlap existing indexes. If there is an overlap, we should remove/unify the indexes' definitions (use the .db.collection.getIndexes() method to retrieve the data.

  2. We should check that the queries are frequently used in order to avoid saving a few seconds once a month while doing a SELECT, and pay for that at peak times with INSERTs.
Bottom Line
MongoDB query tuning is easy: Just Tune It!

Keep Performing,
Moshe Kaplan
Published at DZone with permission of Moshe Kaplan, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)