Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I modify existing index in MongoDB without dropping it?

Can I modify existing index in MongoDB without dropping it ? I don't see anything about it in documentation.

I have an non-unique index on String field. Collection is ~6M documents. It's replica set.

I know I can delete index and add new one. But it's problematic due to two reasons:

1) at time while index doesn't exist some queries will be very very slow. 2) adding new index (in my project) creates very high load on DB which visibly slows down my web-site.

like image 992
expert Avatar asked Dec 07 '12 19:12

expert


1 Answers

There is no way to alter an index as you describe, and if there was I think the outcome in terms of performance would be similar - how would the database use the half created/altered index while this operation was going on for example?

Instead I would recommend using the background option to build the index on a single node, if that is your configuration - it will take longer but will not interfere with your normal operation as much. Once it is finished you can drop the old index at your leisure.

However, if you have a replica set (recommended) you should be aware that index creation is always (currently) done in the foreground on the secondary. If you want to avoid load on your secondaries, then you should follow the steps outlined here to take a member out one at a time and build the index required before rejoining the set:

http://docs.mongodb.org/manual/administration/indexes/#index-building-replica-sets

Update

Background index builds on secondaries will be possible starting with the 2.6 release (see release notes for details). This is not going to be backported to prior versions, so the above note will be true for versions prior to 2.6.

Finally, As a general note, indexes built in the background will generally be larger and a less efficient than those built in the foreground, so the methodology above will still have its uses.

like image 172
Adam Comerford Avatar answered Sep 28 '22 07:09

Adam Comerford