Indexing Design Fundamentals / Creating Indexes
MongoDB provides a flexible and dynamic indexing system that allows developers to create, modify, hide, and drop indexes on collections at any time without any database downtime.
So as your application evolves, you can have confidence that your indexing strategy can evolve too without the pain and complexity.
In this video, we'll show you how to create indexes with the MongoDB Shell or MongoSH.
Then we'll discuss managing indexes over time. Let's take a moment to look at where we are in managing the indexing life cycle for our bank messaging app.
We identified queries for key workloads that can benefit from indexes, and we planned our indexing strategy to support those queries using as few indexes as possible.
Our next steps are to create those indexes on the messages collection and manage those indexes.
MongoDB offers several options for creating and managing indexes. The Atlas Data Explorer, the Atlas CLI, MongoDB Compass, MongoDB Shell, and the MongoDB drivers all use the create index command when building an index.
In this demo, we'll focus on running the create indexes database command in the MongoDB shell.
Here, we're using the create indexes database command to create multiple indexes with different properties.
We can create the first index that will support queries one and two. Then we create a partial index to support the third query.
Note that when we use the database command to create our indexes, we need to provide a name for each index. We recommend using create indexes whenever possible to efficiently create multiple indexes in a single operation.
This optimizes resource usage and reduces the impact on database performance.
When you run the command, you will receive a success message that looks something like this.
We can see that the operation succeeded here and that two indexes were created. This collection started with one default index on underscore ID, and we just created two more. So now there are three indexes in total.
Great. Now that we've created our indexes, let's talk about index management. Index management in MongoDB involves several key practices, including planning, monitoring, and maintaining indexes as your data and application evolve.
We've covered planning in this skill. Let's talk about monitoring and maintaining indexes.
Maintenance is a continuous process where you periodically evaluate how often your indexes are being used, hide indexes that you want to consider dropping to evaluate the impact, drop unused or redundant indexes, and update others to align with changing query requirements.
Here are some of the MongoSH methods that can help with monitoring and maintenance.
Get indexes returns a list of all indexes present on a given collection.
This is a helpful method to use to confirm that your indexes were created.
The hide indexes method allows you to hide an existing index from the query planner so that it will not be used to support your queries.
MongoDB recommends that you use this method to evaluate the impact of dropping an index before actually doing so. If you decide that an index needs to be removed after testing it out with hide index, drop index, or drop indexes can be used to remove indexes from a collection.
Finally, the explain method provides the query execution plan, showing how MongoDB processes the query and utilizes available indexes.
We'll learn more about the explain method later. For more information on these methods and tools, check out MongoDB's documentation.
To monitor and maintain your indexes, you can also use MongoDB Atlas, MongoDB Compass, or third party tools.
These options can help you by providing insight into index performance and usage patterns, index efficiency, and detecting unused or redundant indexes.
For more automated guidance, MongoDB Atlas offers the Performance Advisor, a powerful tool that analyzes your query patterns and recommends indexes that could enhance performance.
Nice work. Let's recap what we covered in this lesson. First, we learned how to create indexes in the MongoDB shell using the create indexes database command.
Then we discussed index management in MongoDB, which involves planning, monitoring, and maintaining indexes as your data and application evolve.
Finally, we discussed tools and methods that can be used to monitor queries and indexes, like Performance Advisor with MongoDB Atlas, MongoDB Compass, MongoDB Shell methods, or third party tools.
