FAQ: Indexes
This document addresses some common questions regarding MongoDBindexes. For more information on indexes, seeIndexes.
How do I create an index?
To create an index on a collection, use thedb.collection.createIndex()
method. Creating an index is anadministrative operation. In general, applications should not calldb.collection.createIndex()
on a regular basis.
Note
Index builds can impact performance; seeHow does an index build affect database performance?. Administrators should consider theperformance implications before building indexes.
How does an index build affect database performance?
MongoDB index builds against a populated collection require an exclusiveread-write lock against the collection. Operations that require a reador write lock on the collection must wait until themongod
releases the lock.
Changed in version 4.2.
- For feature compatibility version (fcv)
"4.2"
,MongoDB uses an optimized build process that only holds theexclusive lock at the beginning and end of the index build. The restof the build process yields to interleaving read and writeoperations. - For feature compatibility version (fcv)
"4.0"
,the default foreground index build process holds the exclusive lockfor the entire index build.background
index builds do _not_take an exclusive lock during the build process.
For more information on the index build process, seeIndex Builds on Populated Collections.
Index builds on replica sets have specific performance considerationsand risks. See Index Builds in Replicated Environments for moreinformation. To minimize the impact of building an index on replicasets, including shard replica sets, use a rolling index build procedureas described in Build Indexes on Replica Sets.
To return information on currently running index creation operations,see Active Indexing Operations. To kill a running index creationoperation on a primary or standalone mongod
, usedb.killOp()
. The partially built index will bedeleted.
You cannot terminate a replicated index build on secondary members ofa replica set. You must first drop
the index on the primary. The secondaries will replicate the dropoperation and drop the indexes after the index build completes.All further replication blocks behind the index build and drop.
How do I see what indexes exist on a collection?
To list a collection’s indexes, use thedb.collection.getIndexes()
method.
How can I see if a query uses an index?
To inspect how MongoDB processes a query, use theexplain()
method.
How do I determine which fields to index?
A number of factors determine which fields to index, includingselectivity, the support for multiplequery shapes, and size of the index. For more information, seeOperational Considerations for Indexes andIndexing Strategies.
How can I see the size of an index?
The db.collection.stats()
includes anindexSizes
document which provides sizeinformation for each index on the collection.
Depending on its size, an index may not fit into RAM. An index fitsinto RAM when your server has enough RAM available for both the indexand the rest of the working set. When an index is too large tofit into RAM, MongoDB must read the index from disk, which is a muchslower operation than reading from RAM.
In certain cases, an index does not need to fit entirely into RAM.For details, see Indexes that Hold Only Recent Values in RAM.
How do write operations affect indexes?
Write operations may require updates to indexes:
- If a write operation modifies an indexed field, MongoDB updates allindexes that have the modified field as a key.
Therefore, if your application is write-heavy, indexes might affectperformance.