Query Optimization查询优化
On this page本页内容
Indexes improve the efficiency of read operations by reducing the amount of data that query operations need to process. This simplifies the work associated with fulfilling queries within MongoDB.索引通过减少查询操作需要处理的数据量来提高读取操作的效率。这简化了在MongoDB中完成查询的相关工作。
Create an Index to Support Read Operations创建索引以支持读取操作
If your application queries a collection on a particular field or set of fields, then an index on the queried field or a compound index on the set of fields can prevent the query from scanning the whole collection to find and return the query results. 如果应用程序查询某个特定字段或字段集的集合,则查询字段上的索引或字段集上的复合索引会阻止查询扫描整个集合以查找并返回查询结果。For more information about indexes, see the complete documentation of indexes in MongoDB.有关索引的更多信息,请参阅MongoDB中索引的完整文档。
An application queries the 应用程序在inventory
collection on the type
field. inventory
集合的type
字段上查询。The value of the type
field is user-driven.type
字段的值由用户驱动。
var typeValue = <someUserInput>;
db.inventory.find( { type: typeValue } );
To improve the performance of this query, add an ascending or a descending index to the 若要提高此查询的性能,请在inventory
collection on the type
field. [1] In mongosh
, you can create indexes using the db.collection.createIndex()
method:type
字段上为inventory
集合添加升序或降序索引。[1] 在mongosh
中,可以使用db.collection.createIndex()
方法创建索引:
db.inventory.createIndex( { type: 1 } )
This index can prevent the above query on 此索引可以防止上面对type
from scanning the whole collection to return the results.type
的查询扫描整个集合以返回结果。
To analyze the performance of the query with an index, see Analyze Query Performance.要使用索引分析查询的性能,请参阅分析查询性能。
In addition to optimizing read operations, indexes can support sort operations and allow for a more efficient storage utilization. 除了优化读取操作外,索引还可以支持排序操作,并允许更高效的存储利用率。See 有关索引创建的详细信息,请参阅db.collection.createIndex()
and Indexes for more information about index creation.db.collection.createIndex()
和索引。
[1] | |
Query Selectivity查询选择性
Query selectivity refers to how well the query predicate excludes or filters out documents in a collection. 查询选择性是指查询谓词排除或筛选集合中文档的程度。Query selectivity can determine whether or not queries can use indexes effectively or even use indexes at all.查询选择性可以确定查询是否可以有效地使用索引,甚至可以完全使用索引。
More selective queries match a smaller percentage of documents. 更有选择性的查询与较小百分比的文档匹配。For instance, an equality match on the unique 例如,唯一性_id
field is highly selective as it can match at most one document._id
字段上的相等匹配是高度选择性的,因为它最多可以匹配一个文档。
Less selective queries match a larger percentage of documents. 选择性较低的查询与较大百分比的文档匹配。Less selective queries cannot use indexes effectively or even at all.选择性较低的查询无法有效使用索引,甚至根本无法使用索引。
For instance, the inequality operators 例如,不等式运算符$nin
and $ne
are not very selective since they often match a large portion of the index. $nin
和$ne
不是很有选择性,因为它们通常与指数的很大一部分匹配。As a result, in many cases, a 因此,在许多情况下,带索引的$nin
or $ne
query with an index may perform no better than a $nin
or $ne
query that must scan all documents in a collection.$nin
或$ne
查询的性能可能并不比必须扫描集合中所有文档的$nin
或$ne
搜索好。
The selectivity of 正则表达式的选择性取决于表达式本身。regular expressions
depends on the expressions themselves. For details, see regular expression and index use.有关详细信息,请参阅正则表达式和索引的使用。
Covered Query涵盖的查询
A covered query is a query that can be satisfied entirely using an index and does not have to examine any documents. An index covers a query when all of the following apply:覆盖查询是指可以完全使用索引来满足的查询,并且不必检查任何文档。当以下所有条件都适用时,索引将覆盖查询:
all the fields in the query are part of an index, and查询中的所有字段都是索引的一部分,并且all the fields returned in the results are in the same index.结果中返回的所有字段都在同一索引中。no fields in the query are equal to查询中没有字段等于null
(i.e.{"field" : null}
or{"field" : {$eq : null}}
).null
(即{"field" : null}
或{"field" : {$eq : null}}
)。
For example, a collection 例如,集合inventory
has the following index on the type
and item
fields:inventory
在type
字段和item
字段上具有以下索引:
db.inventory.createIndex( { type: 1, item: 1 } )
This index will cover the following operation which queries on the 此索引将涵盖以下查询type
and item
fields and returns only the item
field:type
和item
字段并仅返回item
字段的操作:
db.inventory.find(
{ type: "food", item:/^c/ },
{ item: 1, _id: 0 }
)
For the specified index to cover the query, the projection document must explicitly specify 对于覆盖查询的指定索引,投影文档必须显式指定_id: 0
to exclude the _id
field from the result since the index does not include the _id
field._id:0
以从结果中排除_id
字段,因为索引不包括_id
字段。
Embedded Documents嵌入式文档
An index can cover a query on fields within embedded documents.索引可以覆盖对嵌入文档中字段的查询。
For example, consider a collection 例如,考虑一个具有以下形式文档的集合userdata
with documents of the following form:userdata
:
{ _id: 1, user: { login: "tester" } }
The collection has the following index:集合具有以下索引:
{ "user.login": 1 }
The { "user.login": 1 }
index will cover the query below:{ "user.login": 1 }
索引将涵盖以下查询:
db.userdata.find( { "user.login": "tester" }, { "user.login": 1, _id: 0 } )
To index fields in embedded documents, use dot notation.若要为嵌入文档中的字段编制索引,请使用点表示法。
Multikey Covering多键覆盖
Multikey indexes can cover queries over the non-array fields if the index tracks which field or fields cause the index to be multikey.如果索引跟踪导致索引为多键的字段,则多键索引可以覆盖对非数组字段的查询。
Multikey indexes多键索引 cannot cover queries over array fields.无法覆盖对数组字段的查询。
Performance性能
Because the index contains all fields required by the query, MongoDB can both match the query conditions and return the results using only the index.由于索引包含查询所需的所有字段,MongoDB既可以匹配查询条件,也可以仅使用索引返回结果。
Querying only the index can be much faster than querying documents outside of the index. 只查询索引可能比在索引之外查询文档快得多。Index keys are typically smaller than the documents they catalog, and indexes are typically available in RAM or located sequentially on disk.索引键通常比它们编目的文档小,索引通常在RAM中可用或按顺序位于磁盘上。
Limitations局限性
Restrictions on Indexed Fields对索引字段的限制
Geospatial indexes can't cover a query.地理空间索引无法覆盖查询。Multikey indexes多键索引cannot cover queries over array fields.无法覆盖对数组字段的查询。TipSee also:另请参阅:
Restrictions on Sharded Collection对分片集合的限制
When run on 当在mongos
, indexes can only cover queries on sharded collections if the index contains the shard key.mongos
上运行时,如果索引包含分片键,则索引只能覆盖对分片集合的查询。
explain
To determine whether a query is a covered query, use the 要确定查询是否为覆盖查询,请使用db.collection.explain()
or the explain()
method and review the results.db.collection.explain()
或explain()
方法并查看结果。
For more information see Measure Index Use.有关详细信息,请参阅度量索引使用。