Optimize Query Performance优化查询性能

On this page本页内容

Create Indexes to Support Queries创建索引以支持查询

For commonly issued queries, create indexes. 对于常见的查询,请创建索引If a query searches multiple fields, create a compound index. 如果查询搜索多个字段,请创建复合索引Scanning an index is much faster than scanning a collection. 扫描索引比扫描集合快得多。The indexes structures are smaller than the documents reference, and store references in order.索引结构比文档引用小,并且按顺序存储引用。

Example实例

If you have a posts collection containing blog posts, and if you regularly issue a query that sorts on the author_name field, then you can optimize the query by creating an index on the author_name field:如果您有一个包含博客帖子的posts集合,并且如果您定期发出一个在author_name字段上排序的查询,那么您可以通过在author_name字段上创建索引来优化查询:

db.posts.createIndex( { author_name : 1 } )

Indexes also improve efficiency on queries that routinely sort on a given field.索引还可以提高对给定字段进行常规排序的查询的效率。

Example实例

If you regularly issue a query that sorts on the timestamp field, then you can optimize the query by creating an index on the timestamp field:如果定期发出按timestamp字段排序的查询,则可以通过在timestamp字段上创建索引来优化查询:

Creating this index:创建此索引:

db.posts.createIndex( { timestamp : 1 } )

Optimizes this query:优化此查询:

db.posts.find().sort( { timestamp : -1 } )

Because MongoDB can read indexes in both ascending and descending order, the direction of a single-key index does not matter.因为MongoDB可以按升序和降序读取索引,所以单键索引的方向并不重要。

Indexes support queries, update operations, and some phases of the aggregation pipeline.索引支持查询、更新操作和聚合管道的某些阶段。

Index keys that are of the BinData type are more efficiently stored in the index if:如果满足以下条件,则BinData类型的索引键将更有效地存储在索引中:

  • the binary subtype value is in the range of 0-7 or 128-135, and二进制子类型值在0-7或128-135范围内,并且
  • the length of the byte array is: 0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, or 32.字节数组的长度是:0、1、2、3、4、5、6、7、8、10、12、14、16、20、24或32。

Limit the Number of Query Results to Reduce Network Demand限制查询结果的数量以减少网络需求

MongoDB cursors return results in groups of multiple documents. MongoDB游标以多个文档为一组返回结果。If you know the number of results you want, you can reduce the demand on network resources by issuing the limit() method.如果知道所需结果的数量,可以通过发出limit()方法来减少对网络资源的需求。

This is typically used in conjunction with sort operations. 这通常与排序操作结合使用。For example, if you need only 10 results from your query to the posts collection, you would issue the following command:例如,如果从查询到posts集合只需要10个结果,则可以发出以下命令:

db.posts.find().sort( { timestamp : -1 } ).limit(10)

For more information on limiting results, see limit()有关限制结果的更多信息,请参阅limit()

Use Projections to Return Only Necessary Data使用投影只返回必要的数据

When you need only a subset of fields from documents, you can achieve better performance by returning only the fields you need:当您只需要文档中的字段子集时,您可以通过只返回所需的字段来实现更好的性能:

For example, if in your query to the posts collection, you need only the timestamp, title, author, and abstract fields, you would issue the following command:例如,如果在对posts集合的查询中,只需要timestamptitleauthorabstract字段,则会发出以下命令:

db.posts.find( {}, { timestamp : 1 , title : 1 , author : 1 , abstract : 1} ).sort( { timestamp : -1 } )

For more information on using projections, see Project Fields to Return from Query.有关使用投影的更多信息,请参阅要从查询返回的投影字段

Use $hint to Select a Particular Index使用$hint选择特定索引

In most cases the query optimizer selects the optimal index for a specific operation; however, you can force MongoDB to use a specific index using the hint() method. 在大多数情况下,查询优化器为特定操作选择最佳索引;但是,可以使用hint()方法强制MongoDB使用特定索引。Use hint() to support performance testing, or on some queries where you must select a field or field included in several indexes.使用hint()支持性能测试,或者在某些查询中,您必须选择一个字段或包含在多个索引中的字段。

Use the Increment Operator to Perform Operations Server-Side使用增量运算符在服务器端执行操作

Use MongoDB's $inc operator to increment or decrement values in documents. 使用MongoDB的$inc运算符来增加或减少文档中的值。The operator increments the value of the field on the server side, as an alternative to selecting a document, making simple modifications in the client and then writing the entire document to the server. 运算符增加服务器端字段的值,作为选择文档、在客户端进行简单修改,然后将整个文档写入服务器的替代方法。The $inc operator can also help avoid race conditions, which would result when two application instances queried for a document, manually incremented a field, and saved the entire document back at the same time.$inc运算符还可以帮助避免竞争条件,这将导致两个应用程序实例查询一个文档,手动递增一个字段,并同时将整个文档保存回原处。

←  Evaluate Performance of Current OperationsWrite Operation Performance →