Databases 9 min read

Mastering MongoDB Indexes: B‑Tree, Selectivity, and Compound Index Strategies

This article explains MongoDB indexing fundamentals, covering B‑Tree structures, index selectivity, unique and compound indexes, practical code examples, and guidelines to optimize query performance and reduce I/O operations.

360 Zhihui Cloud Developer
360 Zhihui Cloud Developer
360 Zhihui Cloud Developer
Mastering MongoDB Indexes: B‑Tree, Selectivity, and Compound Index Strategies

Understanding and using indexes is crucial for optimizing MongoDB performance. This guide consolidates key concepts about MongoDB indexes to help improve your grasp of their mechanisms.

B‑Tree Index

B‑Tree is MongoDB's default index structure, featuring a hierarchical tree where the top node (head block) points to branch blocks, which in turn point to leaf blocks containing key values and pointers to document locations on disk.

When searching for a record such as "BAKER", MongoDB traverses from the head block to the appropriate branch and leaf blocks, ultimately locating the key and its disk pointer.

Leaf blocks link to previous and next leaf blocks, enabling ascending or descending scans and supporting range queries with $gt or $lt operators.

Advantages of B‑Tree indexes include predictable performance (typically no more than three or four I/O operations per document), efficient handling of large collections (depth rarely exceeds four levels), and support for range queries due to linked leaf nodes.

However, maintaining B‑Trees during data modifications can be costly, especially when leaf blocks require splitting and new blocks must be allocated.

Index Selectivity

Selectivity measures how useful an index is based on the uniqueness of its values; highly unique fields (e.g., birth date) are selective, while low‑cardinality fields (e.g., gender) are not.

Selective indexes are more efficient because they directly target specific values, and the MongoDB optimizer prefers the most selective index available.

Unique Index

A unique index prevents duplicate values for the indexed fields. Attempting to create a duplicate unique index or inserting a document with duplicate values results in an error.

Unique indexes are primarily used to enforce data integrity, but they also provide high selectivity, making queries very efficient.

Compound Index

A compound index includes multiple fields, offering greater selectivity than single‑field indexes. It is especially effective when queries reference all indexed fields in find() or $match clauses.

<code>db.people.createIndex({ "Surname": 1, "Firstname": 1 });</code>

This index allows rapid retrieval of documents matching both Surname and Firstname, outperforming separate single‑field indexes.

When the indexed fields appear together in queries, the compound index is highly beneficial; otherwise, its usefulness may be limited.

Compound Index Guidelines

Create a compound index on fields that frequently appear together in find() or $match conditions.

Place fields that are sometimes used alone at the beginning of the index.

Order fields to maximize selectivity; for example, { "Surname": 1, "Firstname": 1 } is more useful than the reverse if surname queries are more common.

Consider index compression effects; highly selective leading fields are preferred, but actual performance may require testing different orders.

Conclusion

Deep understanding of MongoDB's internal indexing mechanisms is essential for effective performance optimization.

IndexingmongodbDatabase performanceB-treeCompound Index
360 Zhihui Cloud Developer
Written by

360 Zhihui Cloud Developer

360 Zhihui Cloud is an enterprise open service platform that aims to "aggregate data value and empower an intelligent future," leveraging 360's extensive product and technology resources to deliver platform services to customers.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.