Skip to main content
All CollectionsIndexing
Intro to indexes
Intro to indexes

Explore how to speed up application performance using indexes.

B
Written by Bommy Cha
Updated over a month ago

In this article, you’ll learn:

Let's start with the basics.

An index is a data structure that improves the speed of data retrieval operations on a database table with some write and storage overhead.

When you create an index on a table, the database creates a separate data structure that holds a subset of the data in the table, organized in a way that allows the database to quickly search and retrieve the data based on the values in one or more columns.

How indexes improve the performance of your MongoDB application:

Faster queries

When you perform a query on a collection, MongoDB scans the entire collection to find the documents that match the query criteria. Creating an index on the fields used in the query means MongoDB can use the index to find the matching documents faster.

Better sorting

When sorting the results of a query, MongoDB can use an index to avoid the need for an in-memory sort, which can be slow and resource-intensive.

Improved write performance

Properly designed indexes can improve write performance by reducing the amount of time that MongoDB spends updating indexes.

Now that you know what an index is, let’s talk about how to use them effectively.

Best practices for creating indexes for your application:

  1. Identify the queries that are taking the longest time to run. Determine which columns in the queries are the most selective (have the most unique values) and are most frequently used in the WHERE clause. Create indexes on those columns. Note: If you are on a dedicated cluster, The Atlas Performance Advisor can help with this.

  2. Consider creating compound indexes (indexes on multiple columns) if the queries involve multiple criteria in match or sort.

  3. Avoid over-indexing. Indexes take up space and slow down writes, so only create indexes that you actually need.

How to determine if you need to create an index:

Review query plan and execution statistics with the MongoDB Shell, MongoDB Compass, or Atlas Performance Advisor.

  1. View the current operations that are being executed on your database, including the time each operation has been running, its status, and the number of documents it has scanned with the "db.currentOp()" command.

  2. Analyze a query’s execution plan and see if it is using an index with the “explain” method. If a query is taking a long time to run, you can use "explain()" to identify which queries are causing performance degradation.

  3. For dedicated clusters (M10+), monitor database performance with Atlas Performance Advisor. The profiler can help you identify slow queries and operations that are taking a long time to execute, and also recommend indexes that would help.

What types of indexes are there?

Atlas Search indexes enable complex full-text search. These indexes are ideal when building search functionality into an application, such as developing autocomplete for common user search results, or building AI-powered and semantic search applications with vector embeddings.

Atlas Search builds on Lucene, an open source low-level library operated by Apache. Atlas search operators create and execute Lucene queries to satisfy $search, full-text, and vector search indexes and queries. To learn more about profiling the Lucene queries that Atlas Search operators create, check out this doc.

Visit our docs for more information on indexes and how to leverage them or try the Indexes course on MongoDB University.

Did this answer your question?