发布于 2015-09-14 14:51:30 | 110 次阅读 | 评论: 0 | 来源: 网络整理

概述

There are many factors that can affect performance of operations in MongoDB, including index use, query structure, data modeling, application design and architecture, as well as operational factors including architecture and system configuration. This document addresses key application optimization strategies, and includes examples and links to relevant reference material.

策略

This section describes techniques for optimizing database performance with MongoDB with particular attention to query performance and basic client operations.

使用索引

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:

db.posts.ensureIndex( { 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:

Creating this index:

db.posts.ensureIndex( { 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.

Indexes support queries, update operations, and some phases of the aggregation pipeline for more information.

限制条数

MongoDB cursors return results in groups of multiple documents. If you know the number of results you want, you can reduce the demand on network resources by issuing the cursor.limit() method.

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:

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

For more information on limiting results, see cursor.limit()

使用规划只返回需要的数据

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:

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

For more information on using projections, see Result Projections.

使用数据库分析器来评估性能

MongoDB provides a database profiler that shows performance characteristics of each operation against the database. Use the profiler to locate any queries or write operations that are running slow. You can use this information, for example, to determine what indexes to create.

For more information, see Database Profiling.

使用 db.currentOp() 来评估性能

The db.currentOp() method reports on current operations running on a mongod instance. For documentation of the output of db.currentOp() see 当前操作报告.

使用 $explain 来评估性能

The explain() method returns statistics on a query, and reports the index MongoDB selected to fulfill the query, as well as information about the internal operation of the query.

Example

To use explain() on a query for documents matching the expression { a: 1 }, in the collection records, use an operation that resembles the following in the mongo shell:

db.records.find( { a: 1 } ).explain()

使用 $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. Use hint() to support performance testing, or on some queries where you must select a field or field included in several indexes.

使用增量运算符执行服务器端操作

Use MongoDB’s $inc operator to increment or decrement values in documents. 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.

执行服务器端代码执行

For some kinds of operations, you can perform operations on the mongod server itself rather than writing a client application to perform a simple task. This can eliminate network overhead for client operations for some basic administrative operations. Consider the following example:

Example

For example, if you want to remove a field from all documents in a collection, performing the operation directly on the server is more efficient than transmitting the collection to your client and back again.

For more information, see the 服务器端JavaScript page.

使用封顶集合

封顶集合 are circular, fixed-size collections that keep documents well-ordered, even without the use of an index. This means that capped collections can receive very high-speed writes and sequential reads.

These collections are particularly useful for keeping log files but are not limited to that purpose. Use capped collections where appropriate.

使用自然排序

To return documents in the order they exist on disk, return sorted operations using the $natural operator. Natural order does not use indexes but can be fast for operations when you want to select the first or last items on disk. This is particularly useful for capped collections.

也可以参考

sort() and limit().

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务