The best indexes for your application depend on several factors, including the kinds of queries you expect, the ratio of reads to writes, and the amount of free memory on your system.
Before building indexes, map out your application's queries and the fields they access. Indexes have a performance cost, but are worth it for frequent queries on large data sets. Consider the frequency of each query and whether it justifies an index.
Profile multiple index configurations against production-representative data to find which performs best. Inspect existing indexes to confirm they support your current and planned queries. Drop indexes that are no longer used.
一般来说,MongoDB 仅使用一个索引即可完成大多数查询。但是,$or 查询的每个子句可能使用不同的索引。
以下文档介绍了索引策略:
- 使用 ESR(相等、排序、范围)准则
- Arrange compound index keys to optimize equality, sort, and range operations.
- 创建索引以支持查询
- An index supports a query when it contains all the fields the query scans, greatly improving performance.
- 使用索引对查询结果进行排序
- Use these strategies to specify index field order and support efficient sort queries.
- 创建选择性索引以高效地回答查询
- Understand how index selectivity affects query performance and design indexes that answer queries efficiently.
- 唯一索引和模式验证
- Combine index properties and schema validation to implement your application design.