Improving MongoDB Performance with Automatically Generated Index Suggestions
Tags: mongodb, database, webdev
Published: 2018-04-25 16:30:05

---
title: "Improving MongoDB Performance with Automatically Generated Index Suggestions"
published: true
tags: mongodb, databases, webdev
canonical_url: https://www.mongodb.com//blog/post/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](https://docs.mongodb.com/manual/reference/explain-results/#collection-scan-vs-index-use) in logs, analyzing [explain results](https://docs.mongodb.com/manual/reference/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](https://www.mongodb.com/cloud/atlas), 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:

![](https://webassets.mongodb.com/_com_assets/cms/IndexSuggestions1-toupcbun4k.png)

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.

![](https://webassets.mongodb.com/_com_assets/cms/IndexSuggestions2-2dkp5h1lc3.png)

### 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.

![](https://webassets.mongodb.com/_com_assets/cms/IndexSuggestions3-un65388oee.png)

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.

![](https://webassets.mongodb.com/_com_assets/cms/IndexSuggestionsPerformanceAdvisor-kp4t26h5jl.png)

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:

![](https://webassets.mongodb.com/_com_assets/cms/IndexSuggestions5-obxb1jxsgj.png)

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.

![](https://webassets.mongodb.com/_com_assets/cms/IndexSuggestions6-km3sxp7ogc.png)

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](https://docs.mongodb.com/manual/core/index-creation/).

![](https://webassets.mongodb.com/_com_assets/cms/IndexSuggestions7-eb07if9yx3.png)

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:

![](https://webassets.mongodb.com/_com_assets/cms/IndexSuggestions8-zg98hcyr6p.png)

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](https://www.mongodb.com/cloud/atlas).