The best indexes for your application must take a number of factors into account, including the kinds of queries you expect, the ratio of reads to writes, and the amount of free memory on your system.应用程序的最佳索引必须考虑许多因素,包括您期望的查询类型、读写比以及系统上的可用内存量。
When developing your indexing strategy you should have a deep understanding of your application's queries. Before you build indexes, map out the types of queries you will run so that you can build indexes that reference those fields. Indexes come with a performance cost, but are more than worth the cost for frequent queries on large data sets. Consider the relative frequency of each query in the application and whether the query justifies an index.在制定索引策略时,您应该对应用程序的查询有深入的了解。在构建索引之前,请绘制出要运行的查询类型,以便构建引用这些字段的索引。索引会带来性能成本,但对于大型数据集的频繁查询来说,其成本是值得的。考虑应用程序中每个查询的相对频率,以及查询是否证明了索引的合理性。
The best overall strategy for designing indexes is to profile a variety of index configurations with data sets similar to the ones you'll be running in production to see which configurations perform best. Inspect the current indexes created for your collections to ensure they are supporting your current and planned queries. If an index is no longer used, drop the index.设计索引的最佳总体策略是使用与您将在生产中运行的数据集类似的数据集来分析各种索引配置,以查看哪些配置性能最佳。检查为集合创建的当前索引,以确保它们支持您当前和计划中的查询。如果不再使用索引,请删除该索引。
Generally, MongoDB only uses one index to fulfill most queries. However, each clause of an 通常,MongoDB只使用一个索引来完成大多数查询。但是,$or query may use a different index.$or查询的每个子句可能使用不同的索引。
The following documents introduce indexing strategies:以下文档介绍了索引策略:
Use the ESR (Equality, Sort, Range) Guideline使用ESR(相等、排序、范围)指南The ESR (Equality, Sort, Range) Guideline is a guide to creating indexes that support your queries efficiently.ESR(相等、排序、范围)指南是创建高效支持查询的索引的指南。Create Indexes to Support Your Queries创建索引以支持查询An index supports a query when the index contains all the fields scanned by the query. Creating indexes that support queries results in greatly increased query performance.当索引包含查询扫描的所有字段时,索引支持查询。创建支持查询的索引可以大大提高查询性能。Use Indexes to Sort Query Results使用索引对查询结果进行排序To support efficient queries, use the strategies here when you specify the sequential order and sort order of index fields.为了支持高效的查询,在指定索引字段的顺序和排序顺序时,请使用此处的策略。Create Selective Indexes to Answer Queries Efficiently创建选择性索引以高效回答查询Understand how index selectivity plays a role in query performance and learn to design indexes to answer a given query most efficiently.了解索引选择性如何在查询性能中发挥作用,并学习设计索引以最有效地回答给定的查询。Unique Indexes and Schema Validation唯一索引和模式验证Strategically combine index properties and schema validation to efficiently implement your application design.策略性地结合索引属性和模式验证,以有效地实现应用程序设计。