MongoDB Index Best Practices
In this article, we shall cover MongoDB Index Guidelines and Best Practices. Indexing has been in use over the decades and its use is not just limited to SQL or NoSQL or other databases.
Indexing is a great technique to help to search the required documents efficiently.
Here instead of performing a total database scan (scanning through every document), you searched through the ordered index first thereby gaining the performance.
MongoDB supports creating or dropping indexes considering query patterns.
Today in this article, we will cover below aspects,
Basic Indexes Benefits
- Indexing supports the efficient execution of queries.
- It’s the best means of controlling the database scan.
- Lets you control the scan of every document efficiently. Please note without indexes each and every document will do a full scan in a collection.
- Indexing helps to limit the number of documents it must inspect.
- It provides the ability of a query to narrow results using the index
- Indexes increased query performance.
Indexes as AntiPattern
Every good pattern has the ability to behave as an Antipattern if not used correctly for the given use case or context of the issue you are trying to solve.
- Don’t create Indexes if using them rarely.
- Do Not duplicate indexes of indexes that already exist in the form of other indexes. Example Compound indexes etc.
- Build indexes on only fields that are used often in the query.
Indexes are resource-intensive
Each index created in the Mongo collection consumes disk space for storage and consumes RAM when the query is executed bringing additional overhead on the CPU and Disk usage.
Each index requires at least 8 kB of data space.
Indexes can impact the storage engine’s performance
So indexes are not a free means to use anytime without proper consideration of the query.
High Write-to-Read ratio
In Collections with a high write-to-read ratio, each write operation will be required to update indexes as well for the given documents. This could cause negative performance for write operations.
The same is different for the collection with a High Read-to-Write ratio.
Such read operations often gain performance when used with indexes.
Use Compound Indexes over Individual Field indexes
As we understood indexes are resource-sensitive, it’s recommended to use Compound indexes over individual separate field indexes.
Understand your Query pattern
Before you could create indexes you must analyze the query pattern.
Having proper execution statistics of a query will help you come up with better indexes.
You can always analyze a query with and without an index to come up with better indexes.
Use inbuilt and automated methods from MongoDB to provide you with the statistics.
The above query once executed gives you the query execution statistics.
Remove Unnecessary Indexes
Now we understood indexes are resource-sensitive. If you happen to observe any indexes that are unused or unnecessary please remove these indexes ASAP.
Use Proper Type of Indexes
MongoDB provides a number of index types to support specific types of data and queries. Please use what suits your query patterns.
Types of Indexes
- Single Field
- Compound Index
- Multikey Index
- Text Indexes
- Hashed Indexes
- Geospatial Index
Use Properties of Indexes
One can create the above types of indexes but it’s important what type of properties can be associated with those indexes.
Do you need to create indexes as unique or partial indexes etc?
Properties of Indexes
- Unique Indexes
- Partial Indexes
- Sparse Indexes
- TTL Indexes
- Hidden Indexes
Case Sensitive Indexes
Case-Insensitive Queries without Case-Insensitive Indexes could result in a slow query.
As a good measure, one can create Case-Insensitive Indexes for better performance query execution.
A query with the same collation as a case-insensitive index returns a case-insensitive result and executes very speedily.
Index Name Length
- A single collection supports up to 64 indexes max.
- Supports up to 32 fields in a compound index.
Index Maintenance
Indexes in MongoDB require maintenance to stay up-to-date with the data changes. MongoDB automatically updates the indexes during write operations, ensuring that the indexes reflect the latest state of the data.
However, index maintenance does introduce some overhead, so it’s important to strike a balance between having the necessary indexes and managing the associated performance impact.
Regular Index Optimization
It’s crucial to periodically review and optimize your indexes based on the query patterns and workload of your application.
This involves analyzing the query performance using explain()
and making adjustments to the index structure, such as adding or removing indexes, modifying index types, or adjusting index field order.
I shall be extending the above guidelines further in the upcoming updates. Hope you find this useful.
References :
Do you have any comments or ideas or any better suggestions to share?
Please sound off your comments below.
Happy Coding !!
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.