Database Manual / CRUD Operations / CRUD Concepts / Query Optimization

Write Operation Performance写入操作性能

Indexes索引

Each index on a collection adds some amount of overhead to the performance of write operations.集合上的每个索引都会给写操作的性能增加一些开销。

For each insert or delete write operation on a collection, MongoDB either inserts or removes the corresponding document keys from each index in the target collection. 对于集合上的每次插入或删除写入操作,MongoDB都会从目标集合中的每个索引中insertdelete相应的文档键。An update operation may result in updates to a subset of indexes on the collection, depending on the keys affected by the update.update操作可能会导致对集合上索引子集的更新,具体取决于受更新影响的键。

Note

MongoDB only updates a sparse or partial index if the documents involved in a write operation are included in the index.MongoDB仅在写入操作中涉及的文档包含在索引中时更新稀疏索引或部分索引。

In general, the performance gains that indexes provide for read operations are worth the insertion penalty. However, in order to optimize write performance when possible, be careful when creating new indexes and evaluate the existing indexes to ensure that your queries actually use these indexes.一般来说,索引为读取操作提供的性能提升值得插入代价。但是,为了尽可能优化写入性能,在创建新索引和评估现有索引时要小心,以确保您的查询确实使用了这些索引。

For indexes and queries, see Query Optimization. For more information on indexes, see Indexes and Indexing Strategies.有关索引和查询,请参阅查询优化。有关索引的更多信息,请参阅索引索引策略

Journaling日志记录

To provide durability in the event of a crash, MongoDB uses write ahead logging to an on-disk journal. MongoDB writes the in-memory changes first to the on-disk journal files. 为了在发生崩溃时提供持久性,MongoDB使用预写日志记录到磁盘日志中。MongoDB首先将内存中的更改写入磁盘上的日志文件。If MongoDB should terminate or encounter an error before committing the changes to the data files, MongoDB can use the journal files to apply the write operation to the data files.如果MongoDB在提交对数据文件的更改之前终止或遇到错误,MongoDB可以使用日志文件对数据文件应用写入操作。

While the durability assurance provided by the journal typically outweigh the performance costs of the additional write operations, consider the following interactions between the journal and performance:虽然日志提供的持久性保证通常超过了额外写入操作的性能成本,但请考虑日志和性能之间的以下相互作用:

  • If the journal and the data file reside on the same block device, the data files and the journal may have to contend for a finite number of available I/O resources. Moving the journal to a separate device may increase the capacity for write operations.如果日志和数据文件位于同一块设备上,则数据文件和日志可能必须争夺有限数量的可用I/O资源。将日志移动到单独的设备可能会增加写入操作的容量。
  • If applications specify write concerns that include the j option, mongod will decrease the duration between journal writes, which can increase the overall write load.如果应用程序指定了包含j选项写入关注mongod将缩短日志写入之间的持续时间,这可能会增加整体写入负载。
  • The duration between journal writes is configurable using the commitIntervalMs run-time option. 日志写入之间的持续时间可以使用commitIntervalMs运行时选项进行配置。Decreasing the period between journal commits will increase the number of write operations, which can limit MongoDB's capacity for write operations. 缩短日志提交之间的时间间隔将增加写操作的数量,这可能会限制MongoDB的写操作容量。Increasing the amount of time between journal commits may decrease the total number of write operation, but also increases the chance that the journal will not record a write operation in the event of a failure.增加日志提交之间的时间可能会减少写入操作的总数,但也会增加日志在失败时不记录写入操作的可能性。

For additional information on journaling, see Journaling.有关日记的更多信息,请参阅日记