我可以修改MongoDB中的现有索引而不删除它吗? [英] Can I modify existing index in MongoDB without dropping it?

查看:765
本文介绍了我可以修改MongoDB中的现有索引而不删除它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以修改MongoDB中的现有索引而不删除它吗?我在文档中没有看到任何关于它的内容。

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

我在String字段上有一个非唯一索引。收集约6M文件。这是副本集。

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)当索引不存在时,某些查询会非常慢。
2)添加新索引(在我的项目中)会在DB上产生非常高的负载,这会明显减慢我的网站速度。

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.

推荐答案

没有办法像你描述的那样改变索引,如果有,我认为在性能方面的结果是相似的 - 在这个操作进行时,数据库如何使用半创建/改变的索引示例?

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/管理/索引/ #index-building-replica-sets

更新

Update

从2.6版本开始,可以使用辅助文件构建背景索引(参见发布说明以获取详细信息)。这不会被反向移植到之前的版本,因此上述注释对于2.6之前的版本将是正确的。

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.

这篇关于我可以修改MongoDB中的现有索引而不删除它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆