What We Learned by Working with MongoDB’s Collection Index


Why a query that ought to private been rapid, used to be tiring and how this led us to learn the kind to optimize filters

Edoardo Zanon

Image for post

Image for post

We private now been the utilization of MongoDB successfully since its early variations and, when you can per chance private bump into articles in opposition to MongoDB utilization, we deem it is an awfully honest staunch and broken-down product for the duties it has been designed for.

One of many most most important challenges of the utilization of MongoDB is the management of its sources throughout the query segment. By sources, we indicate the treasured CPU, but additionally memory and disk IOPS. This article goes in-depth on MongoDB indexing and shares what we learned while optimizing our queries.

MongoDB affords Indexes to present a enhance to query performance. Without indexes, MongoDB must manufacture a series scan (it analyzes every doc in a series, to decide these paperwork that match the query assertion). If an acceptable index exists for a query, MongoDB uses that index to restrict the need of inspected paperwork.

Indexes exhaust recordsdata constructions called b-tree, they invent the quest extraordinarily rapid. As nicely as, to plan the shortest processing time, MongoDB tries to suit indexes entirely in RAM. On this map, the design can wait on some distance off from finding out the index from disk.

Within the course of the query direction of, the query planner, a component of MongoDB, decides which index suits better the query. When the quest happens by the index, it is performed in memory and is mammoth-efficient. In case your query can’t be solved entirely by the index, Mongo (gradually) performs a scan to web the paperwork from the disk. That is in most cases an costly operation and could per chance well cease up in tiring queries.

To clarify an index, you dazzling must specify a space of attributes with a obvious pronounce (1 to insert the fields in ascending pronounce, -1 for descending). MongoDB will rob care of sorting them in the b-tree recordsdata structure per the pronounce you supplied for the attributes. The important attribute self-discipline is the foundation of the tree and the last are the leaves.

Unfortunately, there is now not this kind of thing as a index that works for all queries. In pronounce for you to optimize the performance of your database it is advisable to attain how recordsdata is structured and how it is advisable to bag entry to that recordsdata, in pronounce to make the very best index for the important queries.

Within the event you are making an index for a query, this rule of thumb permits you to decide the pronounce of fields in the index:

  • The fields in opposition to which equality prerequisites of the queries are shuffle.
  • The fields to be listed could per chance well still think the Kind pronounce of the query.
  • The fields representing the Fluctuate of recordsdata to be accessed.

Nonetheless this rule doesn’t match all instances.

Your recordsdata structure plays an awfully critical characteristic in the index definition

For the rationale of this article, let’s deem the series ‘audit’ (mimicking our audit series that records the user operations in our platform).

Every doc in the ‘audit’ series has the next structure:


_id: ObjectId

tenant: string,

form: Array[string],

space: string,

removed: boolen,

silent: fraudulent,

targetId: string,



Let’s elevate we must wait on the export of a share of the audit, filtered by date vary (the utilization of _id), user space, and form of action.


"_id": {


"$lte": ObjectId("5e4f55de19383d7303d1d8b5")


"form": {

"$in": ["TYPE1", "TYPE2", "TYPE3, "TYPE4", "TYPE5", "TYPE6", "TYPE7", "TYPE8", "TYPE9", "TYPE10", "TYPE11", "TYPE12", "TYPE13","TYPE14","TYPE15","TYPE16"]


"silent": fraudulent,

"space": {"$in": ["COMPLETE"]},

"removed": fraudulent,

"tenant": "tenant_1"

}, {_id :-1}).restrict(50)

There are several indexes on hand on this series, the design created them to wait on an eye on assorted queries and the query planner could per chance well rob again of them, here’s a listing of the on hand indexes that the query planner evaluates for our query:






If we attain the query we stare it takes 50ms to total. Nonetheless there is now not this kind of thing as a index that satisfies the foundations described before. Why is this query rapid? To cherish why lets still attain the expose .point to() and that can allow us to survey that it uses the index ‘tennant_1_status_1_silent_1__id_-1’.

Having a survey dazzling on the execution time couldn’t be enough to validate the query performance.

To take a look at if a query is working successfully it is a must-must make exhaust of the point to() expose and take a look at the connection between “nReturned”, which is the need of paperwork that match the query, and “totalDocsExamined”, which is the need of paperwork analyzed throughout the query direction of. On this case, our result used to be:


take a look at out: 14

nReturned: 50

totaldocumentsExamined: 178

totalkeysExamined: 178


It seems the execution time used to be a stroke of perfect fortune attributable to the indisputable truth that the distribution of recordsdata, on this case, used to be good to us.

The greater the distinction between returned paperwork and examined paperwork, the increased is the query execution duration. For honest staunch performance, the ratio between nReturned and totalkeysExamined must be as end as that you can per chance well have faith to 1:1.

Essentially, if we desire to take a look at the staunch energy of the index, we can dazzling shuffle a depend() on the outdated query. The execution time is 30,1 seconds. The index feeble used to be the identical, why is there this kind of distinction?

To manufacture the depend with this index, MongoDB fetches from disk (or cache) any doc contained on this index, on memoir of there are some fields on this query, that aren’t integrated in the index. As nicely as, now not all scanned paperwork match the query. As a , we can stare CPU utilization is round 10% and disk IOPS is extraordinarily excessive (~1660 IOPS)

By following the rule previously described, the very best index for this query would be:


Nonetheless, if we shuffle the depend() again, we still stare an execution time of 30,1 seconds. By working a point to() we hit upon that the query planner ignores the recent index we created by following the documentation advice and keeps the utilization of the outdated one.


Analyzing the discarded conception of the point to, we hit upon that our index has a excessive desire of “take a look at out” (repositioning of the cursor in the index throughout the pre-scan of the b-tree) which negatively impacts the effectiveness of the query. Our interpretation is that the date filtering utilized to any component of the $in clause causes a excessive desire of “take a look at out”, and generate an operation that is evaluated as too costly from the query planner.

What makes us think that we are honest staunch on our hypothesis is that if we alternate our query, by lowering the need of parts for $in operator on “form” filter to perfect five objects, the query planner chooses the “honest staunch” index and the execution time drops to about 60ms.

The following graph explains the structure of the index.

The previeous index is unsuitable for our case and recordsdata distribution.

To create our query as rapid as that you can per chance well have faith, we modified the index to:


On this situation, recordsdata affects the advance of the index.

Our recordsdata is disbursed over 4 years, the inserting rate on this series is continuous over time. Our query must filter the guidelines in a vary integrated from 1 to 30 days. After we make the index, if we swap the place of “form” and “_id” the guidelines analyzed by the query must be lower than before.

The following blueprint explains the direction desire throughout the index exploring.

If we shuffle the point to() expose now, we can stare that:


take a look at out: 1

nReturned: 50

totaldocumentsExamined: 0

totalkeysExamined: 50


Now the “take a look at out” is 1. The scan of the index is more helpful and results in a sooner query.

The “totalkeysExamined” now private a ratio with “nReturned” equal to 1. Within the worst-case scenario, after we filter by perfect one “form”, “totalkeysExamined” could per chance well also be excessive, but in our case, the performance the utilization of this index is smartly marvelous (130ms query response time), because it is now not a customer-coping with query but an administrative one with a fairly low bag entry to frequency.

This query would additionally work honest with out inserting the self-discipline “form” contained in the index for the rationale that recordsdata analyzed throughout the query are decreased (CPU utilization is lower). Nonetheless, to additional give a enhance to performance and wait on some distance off from taking recordsdata from disk and involving sources, we inserted it anyway to plan “totaldocumentsExamined: 0”

Having the total fields in the index and private enough RAM to comprise that, is additionally a most efficient observe suggested by MongoDB to decrease the utilization of disk.

Within the next graph, we can stare the performance of the 2 indexes. With the last index, the need of examined keys in the index is still excessive. This metric presentations that the query could per chance well work better with one more index, even supposing the utilization of the cluster sources (CPU and disk IOPS) used to be decreased. The two tests private been accomplished cleansing the cluster caching (object already fetched by disk).

  • Left graph: two queries shuffle with the ragged index
  • Correct form graph: two queries shuffle with the recent index

Nice, lets. That is a tradeoff between efficiency and rate. Shall we add both indexes, but that could per chance well require an increased quantity of sources. As an example, our index uses 1.4 GB of memory with staunch recordsdata, the tradeoff between the resource utilization and the query performance is nice as it is now.

In some unspecified time in the future, lets be compelled to add one more index, in particular if our recordsdata changes it skewing.

There is now not this kind of thing as a perfect index, databases are getting smarter and they also could per chance enact a legitimate job at identifying honest staunch indexes a lot of the instances, but it is a must-must be ready to dig deeper into how the query planner works, how your recordsdata distribution is formed and the map in which it plays in your private indexes, which a lot of the instances will be a tradeoff between performance and resource utilization.

Read More

Leave A Reply

Your email address will not be published.