chapter seven

7 Indexing for query performance

 

This chapter covers

  • Understanding MongoDB query planner and execution plan
  • Creating, deleting, and viewing MongoDB indexes
  • Learning MongoDB index types
  • Showing the ESR rule of thumb
  • Measuring MongoDB Indexes Usage

Over years of work, I have encountered numerous instances where indexes were either not used correctly or not used at all. This is sub-optimal, as indexes allow for efficient query performance and overall database optimization.

In this chapter, I give you best practices for using indexes, rules of thumb, and methods for monitoring index usage and optimization.

Indexes are special data structures that store a small portion of the collection's data in an easily traversable B-tree form. An index orders the values of specific fields, supporting efficient equality matches and range-based queries. MongoDB can also use the index to return sorted results.

Without indexes, MongoDB must scan every document in a collection to return query results. This process is very poor in terms of performance. A suitable index limits the number of documents that need to be scanned.

While indexes improve query performance, they can negatively impact write operations. In collections with a high write-to-read ratio, indexes can be costly as each insert operation must update the indexes.

7.1 MongoDB query planner

7.1.1 Viewing query plan cache information

7.1.2 MongoDB plan cache purges

7.2 Supported index types

7.2.1 Creating single field indexes

7.2.2 Understanding compound indexes

7.2.3 Using multikey indexes

7.2.4 Using text indexes

7.2.5 Creating wildcard index

7.2.6 Geospatial indexes

7.2.7 Hashed indexes

7.3 Dropping indexes

7.4 MongoDB indexes attributes

7.4.1 Partial indexes

7.4.2 Sparse indexes

7.4.3 Time to live (TTL) indexes

7.4.4 Using hidden indexes

7.5 Understanding index builds

7.5.1 Monitoring in progress index builds

7.5.2 Terminating in progress index builds

7.6 Managing indexes

7.6.1 Discovering $indexStats aggregation pipeline stage

7.6.2 Modifying indexes

7.6.3 Controlling index use with hint ()

7.6.4 Using indexes with $OR queries

7.6.5 Using indexes with $NE, $NIN and $NOT operators

7.6.6 Ensuring indexes fit in RAM

7.6.7 Sorting on multiple fields

7.6.8 Introducing covered queries

7.7 When to not use an index?

7.8 Summary