MongoDB Indexing, How indexing can improve the speed of your database queries

MongoDB Indexing, How indexing can improve the speed of your database queries

When we want to improve the speed and efficiency of query requests or read operations, there are several approaches we can consider. One crucial approach to consider is database indexing.

MongoDB indexing is the process of creating an index on a collection of documents in MongoDB to improve your query performance. An index is a data structure that contains the values of specific fields of the documents in a collection, along with a reference to the location of the document on a disk/memory.

A database index is like a book index. Instead of looking through the whole book, you will take a shortcut and look at an ordered list with references to the content. So your queries will run faster. A query that does not use an index is called a collection scan or COLLSCAN. The server will look through the entire collection to find the results. This is what we will do if we are looking at information in a book without an index. So you will start from the first page and read the entire book perhaps to find the requested part. you can imagine how much that will take you, especially as the book becomes bigger. Similarly, you want to make the server avoid scanning all documents because the process will be very slow, especially for large databases.

Get full details on how the index works :

Let’s run a query on our list of users, to find all users where the age is higher than 55

db.users.find({ age: { $gte: 55 }, { geneder: 1 });

After running this command, we get the following results:

Now let’s try the same command but this time we are going to add an explanation to the chaining. We can use explain in MongoDB to see what happens behind the scenes when we run a query. There are multiple verbosity modes to this method, for us, we will choose to run executionStats mode since this will help us to understand the effect of using the index on our queries.

db.users.find({ age: { $gte: 25 }, { geneder: 1 }).explain("executionStats");

The result of this query is very large, we will be interested in the nested document “executionStats

Focus now on the key totalDocsExamined to see how many documents MongoDB examined to return the result of the query, which is exactly the number of documents in our collection. So to run this query MongoDB had to look through all the documents and see if they satisfy the query. You can also see on the needTime field that this query took 969 ms to run the query.

To enable MongoDB to better respond and run queries, especially those that we need most, query patterns in your application need to be indexed.

Create an index :

Now, let’s use indexes and see how our query performance will change. So I am filtering documents based on the age of the users. So, I will need to create an index on the age field. To do this using the command line you can use createIndex command. You do this by specifying the key you are indexing, then the direction of your index. Just like sort, 1 means ascending -1 means descending.

db.users.createIndex({ age: 1 })

It won’t take longer than a few seconds unless your collection is very large.

You can also use createIndices*, Whatever suits you!*

Now if we re-run explain our query, we see that:

You can see that the number of documents examined and the time needed is less than using the query without the index.

MongoDB indexes work almost identically to relational database indexes. So, if you are familiar with indexes, this tutorial will be just to discover the syntax for you.

Removing indexes:

To remove an index via the command line, it’s pretty simple. You just need to specify the index that you want to remove inside the method dropIndex. Just like this :

Advantages and Disadvantages of indexing

MongoDB indexes provide an efficient way to search and retrieve data from a database. However, they also come with some advantages and disadvantages that you should be aware of.

Advantages of MongoDB Index:

  1. Improved Performance: MongoDB indexes can significantly improve the performance of queries by reducing the number of documents that need to be searched.

  2. Faster Read Operations: Indexes can speed up read operations like finding, sorting, and grouping documents.

  3. Flexible Queries: MongoDB indexes support a wide range of queries, including complex queries that involve multiple fields.

  4. Easy to Create and Manage: MongoDB indexes can be easily created and managed using simple commands or with the help of third-party tools.

  5. Improved Concurrency: Indexes can improve concurrency by allowing multiple read operations to occur simultaneously.

Disadvantages of MongoDB Index:

  1. Increased Storage: Indexes take up additional disk space, which can be a disadvantage for large databases with limited storage capacity.

  2. Slower Write Operations: Indexes can slow down write operations like inserting, updating, and deleting documents since the index needs to be updated every time a change is made to the collection.

  3. Index Maintenance Overhead: Creating and maintaining indexes can add overhead to database operations and increase CPU and memory usage.

  4. Indexes Cannot be Used in All Cases: Not all queries benefit from the use of indexes, and in some cases, a full table scan may be more efficient than using an index.

Summary

In summary, MongoDB indexing is a powerful technique to improve the performance and efficiency of query requests and read operations in databases. Indexes help to reduce the number of documents that need to be searched, speed up read operations, and support a wide range of queries. However, indexes also come with some disadvantages, such as increased storage, slower write operations, and maintenance overhead. It is important to carefully consider the benefits and drawbacks of indexing before implementing it in your MongoDB database. Properly indexing the most frequently used queries can significantly improve the performance of your application.

Did you find this article valuable?

Support Elias Soykat's blog by becoming a sponsor. Any amount is appreciated!