Docs Home → Develop Applications → MongoDB Manual
Compound Indexes复合索引
On this page本页内容
Compound indexes collect and sort data from two or more fields in each document in a collection. 复合索引从集合中每个文档的两个或多个字段中集合数据并对其进行排序。Data is grouped by the first field in the index and then by each subsequent field.数据按索引中的第一个字段分组,然后按每个后续字段分组。
For example, the following image shows a compound index where documents are first grouped by 例如,下图显示了一个复合索引,其中文档首先按userid
in ascending order (alphabetically). userid
升序(按字母顺序)分组。Then, the 然后,每个scores
for each userid
are sorted in descending order:userid
的scores
按降序排列:
To create a compound index, use the following prototype:要创建复合索引,请使用以下原型:
db.<collection>.createIndex( {
<field1>: <sortOrder>,
<field2>: <sortOrder>,
...
<fieldN>: <sortOrder>
} )
Use Cases使用案例
If your application repeatedly runs a query that contains multiple fields, you can create a compound index to improve performance for that query. 如果应用程序重复运行包含多个字段的查询,则可以创建复合索引来提高该查询的性能。For example, a grocery store manager often needs to look up inventory items by name and quantity to determine which items are low stock. 例如,杂货店经理经常需要按名称和数量查找inventory
物品,以确定哪些物品inventory
不足。You can create a compound index on both the 您可以在item
and quantity
fields to improve query performance.item
字段和quantity
字段上创建复合索引,以提高查询性能。
Get Started起步
To create a compound index, see Create a Compound Index.若要创建复合索引,请参阅创建复合索引。
Details详细信息
This section describes technical details and limitations for compound indexes.本节介绍了复合索引的技术细节和限制。
Field Limit字段限制
A single compound index can contain up to 32 fields.单个复合索引最多可包含32个字段。
Field Order字段顺序
The order of the indexed fields impacts the effectiveness of a compound index. 索引字段的顺序会影响复合索引的有效性。Compound indexes contain references to documents according to the order of the fields in the index. 复合索引根据索引中字段的顺序包含对文档的引用。To create efficient compound indexes, follow the ESR (Equality, Sort, Range) rule.要创建高效的复合索引,请遵循ESR(相等、排序、范围)规则。
Sort Order排序顺序
Indexes store references to fields in either ascending (索引以升序(1
) or descending (-1
) sort order. 1
)或降序(-1
)存储对字段的引用。For compound indexes, sort order can determine whether the index supports a sort operation. 对于复合索引,排序顺序可以确定索引是否支持排序操作。For more information, see Compound Index Sort Order.有关详细信息,请参阅复合索引排序顺序。
Hashed Index Fields哈希索引字段
In MongoDB 4.4 and later, compound indexes may contain a single hashed index field.在MongoDB 4.4及更高版本中,复合索引可能包含一个哈希索引字段。In MongoDB 4.2 and earlier, compound indexes cannot contain any hashed index fields.在MongoDB 4.2及更早版本中,复合索引不能包含任何哈希索引字段。
Index Prefixes索引前缀
Index prefixes are the beginning subsets of indexed fields. 索引前缀是索引字段的起始子集。Compound indexes support queries on all fields included in the index prefix.复合索引支持对索引前缀中包含的所有字段进行查询。
For example, consider this compound index:例如,考虑以下复合索引:
{ "item": 1, "location": 1, "stock": 1 }
The index has these index prefixes:索引具有以下索引前缀:
{ item: 1 }
{ item: 1, location: 1 }
MongoDB can use the compound index to support queries on these field combinations:MongoDB可以使用复合索引来支持对以下字段组合的查询:
item
item
and和location
item
,、location
, and和stock
MongoDB can also use the index to support a query on the MongoDB还可以使用索引来支持对item
and stock
fields, since the item
field corresponds to a prefix. item
和stock
字段的查询,因为item
字段对应于前缀。However, only the 但是,只有索引中的item
field in the index can support this query. item
字段才能支持此查询。The query cannot use the 查询不能使用stock
field which follows location
.location
后面的inventory
字段。
Index fields are parsed in order; if a query omits an index prefix, it is unable to use any index fields that follow that prefix.索引字段按顺序解析;如果查询省略了索引前缀,则无法使用该前缀后面的任何索引字段。
MongoDB cannot use the compound index to support queries on these field combinations:MongoDB不能使用复合索引来支持对以下字段组合的查询:
location
stock
location
and和stock
Without the 如果没有item
field, none of the preceding field combinations correspond to a prefix index.item
字段,前面的字段组合都不会对应于前缀索引。
Remove Redundant Indexes删除冗余索引
If you have a collection that has both a compound index and an index on its prefix (for example, 如果集合的前缀上同时具有复合索引和索引(例如,{ a: 1, b: 1 }
and { a: 1 }
), if neither index has a sparse or unique constraint, you can remove the index on the prefix ({ a: 1 }
). { a: 1, b: 1 }
和{ a: 1 }
),如果两个索引都没有稀疏约束或唯一约束,则可以删除前缀上的索引({ a: 1 }
)。MongoDB uses the compound index in all of the situations that it would have used the prefix index.MongoDB在所有使用前缀索引的情况下都使用复合索引。
Sparse Compound Indexes稀疏复合索引
Compound indexes can contain different types of sparse indexes. 复合索引可以包含不同类型的稀疏索引。The combination of index types determines how the compound index matches documents.索引类型的组合决定了复合索引与文档的匹配方式。
This table summarizes the behavior of a compound index that contains different types of sparse indexes:此表总结了包含不同类型稀疏索引的复合索引的行为:
geospatial fields. geospatial 字段的值时,才对其进行索引。 | |
text fields. text 字段匹配时,才对其进行索引。 |
Learn More了解更多信息
To learn how to create efficient compound indexes, see The ESR (Equality, Sort, Range) Rule.要了解如何创建高效的复合索引,请参阅ESR(相等、排序、范围)规则。