Mongodb 简明教程

MongoDB - Indexing Limitations

在本章中,我们将了解索引限制及其其他组件。

In this chapter, we will learn about Indexing Limitations and its other components.

Extra Overhead

每个索引都会占用一些空间,并导致每个插入、更新和删除操作产生开销。因此,如果你很少将你的集合用于读取操作,那么不使用索引是有道理的。

Every index occupies some space as well as causes an overhead on each insert, update and delete. So if you rarely use your collection for read operations, it makes sense not to use indexes.

RAM Usage

由于索引存储在 RAM 中,因此你应该确保索引的总大小不超过 RAM 限制。如果总大小增加到超过 RAM 大小,它将开始删除某些索引,导致性能下降。

Since indexes are stored in RAM, you should make sure that the total size of the index does not exceed the RAM limit. If the total size increases the RAM size, it will start deleting some indexes, causing performance loss.

Query Limitations

索引无法用于使用以下内容的查询中 −

Indexing can’t be used in queries which use −

  1. Regular expressions or negation operators like $nin, $not, etc.

  2. Arithmetic operators like $mod, etc.

  3. $where clause

因此,建议始终检查查询的索引用法。

Hence, it is always advisable to check the index usage for your queries.

Index Key Limits

从 2.6 版本开始,如果现有索引字段的值超过索引键限制,MongoDB 将不会创建索引。

Starting from version 2.6, MongoDB will not create an index if the value of existing index field exceeds the index key limit.

Inserting Documents Exceeding Index Key Limit

如果文档的索引字段值超过索引键限制,MongoDB 将不会插入任何文档到索引集合中。对于 mongorestore 和 mongoimport 实用程序也有同样的情况。

MongoDB will not insert any document into an indexed collection if the indexed field value of this document exceeds the index key limit. Same is the case with mongorestore and mongoimport utilities.

Maximum Ranges

  1. A collection cannot have more than 64 indexes.

  2. The length of the index name cannot be longer than 125 characters.

  3. A compound index can have maximum 31 fields indexed.