添加文档时是否需要重建MongoDB索引 [英] Do I have to rebuild MongoDB index when I add documents

查看:354
本文介绍了添加文档时是否需要重建MongoDB索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现了mongodb索引的强大功能.在几个字段上建立索引使我可以将某些操作的速度提高1000倍甚至更多.我为可能看起来很愚蠢的问题道歉:在数据库中的特定字段上建立索引之后,每次添加和删除文档时,索引都会自动重建吗?还是我必须明确地调用它? (在开发阶段,我可能会删除每个文档,然后再次添加所有文档)

I have just discovered the extraordinary power of mongodb indexes. Building indexes on just a few fields has allowed me to speed up some operations 1000 fold, and more. My apologies for what might seem a stupid question: once I have built an index on particular fields in my database, will that rebuild itself automatically every time I add and remove documents? Or do I have to call it explicitly? (in a development phase, I might remove EVERY document, and then add them all again)

推荐答案

MongoDB会自动为您更新索引,以反映集合中文档的当前状态.您不必担心索引更新!

MongoDB automatically keeps indexes up-to-date for you, reflecting the current status of the documents in the collections. You do not have to worry about indexes update!

关于索引的一些警告.引用文档:

A little caveat about indexes. Quoting the doc:

尽管索引可以提高查询性能,但也可以使用索引 一些操作上的考虑.请参阅有关以下内容的操作注意事项 索引以获取更多信息.

Although indexes can improve query performances, indexes also present some operational considerations. See Operational Considerations for Indexes for more information.

例如,如果您需要对索引进行批量插入而又不想减慢该过程,则有时删除索引,然后插入庞大的文档集合并最终重建索引可能会很有用.从头开始(如果需要,也可以在后台).上面链接中的更多信息和其他注意事项.

For example, if you need to do a bulk insert hitting an index and you don't want to slow down that process, sometimes it could be useful deleting the index, then inserting the huge collection of documents and finally rebuild the index from scratch (also in background, if necessary). Further info and other considerations in the link above.

这篇关于添加文档时是否需要重建MongoDB索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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