Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn off (temporary) indexes in MongoDB

Is it possible to suspend index maintenance in MongoDB to improve insertion speed and turn on (or rebuild) indexes afterwards? According to documentation it looks like after ensureIndex() the index maintained during all subsequent inserts and updates.

like image 366
Andrei Pushkin Avatar asked Sep 07 '12 07:09

Andrei Pushkin


People also ask

Can we disable index in MongoDB?

No option to disable index in mongodb, either you can drop and recreate the index or wait for the insert/update to complete.

Does MongoDB automatically create indexes?

MongoDB automatically determines whether to create a multikey index if the indexed field contains an array value; you do not need to explicitly specify the multikey type.

Which command is used to remove the index in MongoDB?

Starting in MongoDB 5.2, you can use db. collection. dropIndexes() to drop existing indexes on the same collection even if there is a build in progress on another index.

What are partial indexes in MongoDB?

Partial indexes only index the documents in a collection that meet a specified filter expression. By indexing a subset of the documents in a collection, partial indexes have lower storage requirements and reduced performance costs for index creation and maintenance.


1 Answers

Indexes are updated synchronously with the insert/update. So there's no way to "pause" this. If you're anticipating a large batch insert, you could drop the index, perform the insert and then rebuild the index. Of course, this has some implications:

  1. your queries will suffer from missing index, while you are inserting data.
  2. index rebuild might be too expensive in terms of burned CPU and invalidated caches (if you have lots of data)
like image 125
Sergio Tulentsev Avatar answered Sep 22 '22 19:09

Sergio Tulentsev