Can I modify existing index in MongoDB without dropping it?

陌路散爱 提交于 2019-12-18 05:48:10

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/13769603/can-i-modify-existing-index-in-mongodb-without-dropping-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!