DEV Community

MongoDB for MongoDB

Posted on • Originally published at mongodb.com on

Improving MongoDB Performance with Automatically Generated Index Suggestions

Beyond good data modeling, there are a few processes that teams responsible for optimizing query performance can leverage: looking for COLLSCANS in logs, analyzing explain results, or relying on third-party tools. While these efforts may help you resolve some of the problems you’re noticing, they often require digging into documentation, time, and money, all the while your application remains bogged down with issues.

MongoDB Atlas, the fully managed database service, helps you resolve performance issues with a greater level of ease by providing you with tools to ensure that your data is accessed as efficiently as possible. This post will provide a basic overview of how to access the MongoDB Atlas Performance Advisor, a tool that reviews your queries for up to two weeks and provides recommended indexes where appropriate.

Getting Started

This short tutorial makes use of the following:

  • A demo data set generated with mgodatagen
  • A dedicated MongoDB Atlas cluster (the Performance Advisor is available for M10s or larger)
  • MongoDB shell install (to create indexes)

My database has two million documents in two separate collections:

If an application tries to access these documents without the right indexes in place, a collection scan will take place. The database will scan the full collection to find the required documents, and any documents that are not in memory are read from disk. This can dramatically reduce performance and cause your application to respond slower than expected.

Case in point, when I try to run an unindexed query against my collections, MongoDB Atlas will automatically create an alert indicating that the query is not well targeted.

Reviewing Performance Advisor

The Performance Advisor monitors slow-running queries (anything that takes longer than 100 milliseconds to execute) and suggests new indexes to improve query performance.

To access this tool, go to your Atlas control panel and click your cluster's name. You’ll then find "Performance Advisor" at the top.

Click the link and you'll be taken to the page where you'll see any relevant index recommendations, based on the fixed time period at the top of the page.

In this example, I will review the performance of my queries from the last 24 hours. The Performance Advisor provides me with some recommendations on how to improve the speed of my slow queries:

It looks like the test collection with the field "name" could use an index. We can review the specific changes to be made by clicking the "More Info" button.

I can copy the contents of this recommendation and paste it into my MongoDB Shell to create the recommended index. You’ll notice a special option, { background: true }, is passed with the createIndex command. Using this command ensures that index creation does not block any operations. If you’re building new indexes on production systems, I highly recommend you read more about index build operations.

Now that the recommended index is created, I can review my application's performance and see if it meets the requirements of my users. The Atlas alerts I received earlier have been resolved, which is a good sign:

Noticeable slowdowns in performance from unindexed queries damage the user experience of your application, which may result in reduced engagement or customer attrition. The Performance Advisor in MongoDB Atlas gives you a simple and cost-efficient way to ensure that you’re getting the most out of the resources you’ve provisioned.

To get started, sign up for MongoDB Atlas and deploy a cluster in minutes.

Top comments (0)