Docs HomeMongoDB Manual

Ensure Indexes Fit in RAM确保索引适合RAM

For the fastest processing, ensure that your indexes fit entirely in RAM so that the system can avoid reading the index from disk.为了获得最快的处理速度,请确保索引完全适合RAM,这样系统就可以避免从磁盘读取索引。

To check the size of your indexes, use the db.collection.totalIndexSize() helper, which returns data in bytes:要检查索引的大小,请使用db.collection.totalIndexSize()帮助程序,该帮助程序以字节为单位返回数据:

> db.collection.totalIndexSize()
4617080000

The above example shows an index size of almost 4.3 gigabytes. 上面的示例显示的索引大小几乎为4.3 GB。To ensure this index fits in RAM, you must not only have more than that much RAM available but also must have RAM available for the rest of the working set. 为了确保该索引适合RAM,您不仅必须有超过该数量的可用RAM,而且必须有可用于工作集其余部分的RAM。Also remember:还要记住:

If you have and use multiple collections, you must consider the size of all indexes on all collections. The indexes and the working set must be able to fit in memory at the same time.如果拥有并使用多个集合,则必须考虑所有集合上所有索引的大小。索引和工作集必须能够同时容纳在内存中。

There are some limited cases where indexes do not need to fit in memory. 在某些有限的情况下,索引不需要适应内存。See Indexes that Hold Only Recent Values in RAM.请参阅RAM中仅包含最近值的索引

Tip

See also: 另请参阅:

Indexes that Hold Only Recent Values in RAMRAM中仅包含最近值的索引

Indexes do not have to fit entirely into RAM in all cases. 在任何情况下,索引都不必完全适合RAM。If the value of the indexed field increments with every insert, and most queries select recently added documents; then MongoDB only needs to keep the parts of the index that hold the most recent or "right-most" values in RAM. 如果索引字段的值随着每次插入而增加,并且大多数查询都选择最近添加的文档;那么MongoDB只需要将索引中保存最新或“最右边”值的部分保存在RAM中。This allows for efficient index use for read and write operations and minimize the amount of RAM required to support the index.这允许有效地使用索引进行读写操作,并最大限度地减少支持索引所需的RAM量。