Indexing Rules
You can use indexes to avoid full table scans and improve query performance.
- A column index can have up to 512 bytes, an index name can have up to 64 characters, and a composite index can have up to16 columns.
- The total length of <Database name>.<Collection name>.$<Index name> cannot exceed 128 characters.
- Use indexes with high selectivity. Indexes with low selectivity will return large number of rows for each key value.
- Write operations on a collection will trigger more I/O operations on indexes in the collection. Ensure that the number of indexes in a collection does not exceed 32.
- Do not create indexes that will not be used. Unused indexes loaded to the memory will cause a waste of memory. In addition, useless indexes generated due to changes in service logic must be deleted in a timely manner.
- Indexes must be created in the background instead of foreground.
- An index must be created for the sort key. If a composite index is created, the column sequence of the index must be the same as that of the sort key. Otherwise, the index will not be used.
- Do not create an index based on the leading-edge column of a composite index. If the leading-edge column of a composite index is the column used in another index, the smaller index can be removed. For example, a composite index based on "firstname" and "lastname" can be used for queries on "firstname". In this case, the firstname-based index is unnecessary.
- The time required for creating an index depends on the data volume. You are advised to design the index in advance.
- You can run the db.currentOp command and view the index creation progress in the command output (keyword: createIndexes).
Last Article: Naming Rules
Next Article: Sharding Rules
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.