Dragos Pitica

efficient querying

At MetaBroadcast we are constantly working on developing our APIs and it’s common that the amount of data for the production systems might not be always simple to manage. This blog post will look at what indexes are and how to use them in Mongo DB collections.

what

Theoretically,

a database index is a data structure that improves the speed of data retrieval operations on a database table at the cost of additional writes and storage space.

Practically, think of a Mongo DB index as a phone book index. Let’s say you want to find a specific service name or phone number without using an index. Well, the only option would be to go through every single page of the book until you find it. Depending on the size of the book, this can get tedious and time-consuming.

On the other hand, instead of examining each document in the collection, Mongo DB indexes store a small portion of the collection’s data and makes the search process much faster and simpler.

why

When you have multiple clients and micro services constantly querying for large sets of data, it’s obvious that you might come through performance issues. Indexes can speed up read & update queries significantly, even up to 1000%. However, they slow down writes by almost 30%, but it’s still a big improvement if you do more reads than writes.

To get an actual example to apply indexes on, I’ll use a collection called Content which contains many content objects with the following structure:


Let’s run a couple of queries on a specific field, a default one and one using the index, and look at the performance differences.


As you can observe, the first query only took 10ms, but we’re also only dealing with about 2115 documents. However, the bigger the collection gets, the longer the duration will be. The second query still matches 458 results, but only 458 objects were scanned and the query took 0ms. This is a noticeable improvement!

how

First of all, here is an interesting blog post published by Fred on configuring correctly indexes in Mongo DB.

So, after you become familiar with the indexing concept, you’re pretty much finished since managing indexes in Mongo DB is pretty simple and straightforward. This snippet includes some common procedures for managing existing indexes.


A very useful procedure for index quering is the explain() method which I mentioned above. The method results describe the details of how MongoDB executes the query. Since version 3.0 of mongo shell the method output format has changes and it also supports a verbosity parameter.

How about you? Have you dealt with any querying performance issues lately? Let us know in the comments!

If you enjoyed the read, drop us a comment below or share the article, follow us on Twitter or subscribe to our #MetaBeers newsletter. Before you go, grab a PDF of the article, and let us know if it’s time we worked together.

blog comments powered by Disqus