This article will cover the following topics around Indexes:
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 can improve the performance of your MongoDB application:
Faster queries
When you perform a query on a collection, MongoDB must scan the entire collection to find the documents that match the query criteria. If you create an index on the fields used in the query, MongoDB can use the index to find the matching documents more quickly, resulting in faster query performance.
Better sorting
When you sort 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
Although creating indexes can slow down write operations, properly designed indexes can actually 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.
Guidelines on creating indexes for your application:
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.
Consider creating compound indexes (indexes on multiple columns) if the queries involve multiple criteria in match or sort.
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:
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.
Analyze a query’s execution plan and see if it is using an index with the “explain90” method. If a query is taking a long time to run, you can use "explain()" to identify which queries are causing the performance issue.
For dedicated clusters (M10+), you can monitor the performance of your database 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?
Visit our docs for more information on indexes and how to leverage them or try the Indexes course on MongoDB University.