Ensure Indexes Fit in RAM

For the fastest processing, ensure that your indexes fit entirely in RAM sothat the system can avoid reading the index from disk.

To check the size of your indexes, use thedb.collection.totalIndexSize() helper, which returns data inbytes:

  1. > db.collection.totalIndexSize()
  2. 4294976499

The above example shows an index size of almost 4.3 gigabytes. To ensurethis index fits in RAM, you must not only have more than that much RAMavailable but also must have RAM available for the rest of theworking set. Also remember:

If you have and use multiple collections, you must consider the sizeof all indexes on all collections. The indexes and the working set must be able tofit in memory at the same time.

There are some limited cases where indexes do not needto fit in memory. See Indexes that Hold Only Recent Values in RAM.

See also

collStats and db.collection.stats()

Indexes that Hold Only Recent Values in RAM

Indexes do not have to fit entirely into RAM in all cases. If thevalue of the indexed field increments with every insert, and most queriesselect recently added documents; then MongoDB only needs to keep theparts of the index that hold the most recent or “right-most” values inRAM. This allows for efficient index use for read and writeoperations and minimize the amount of RAM required to support theindex.