MongoDB:分析 - 删除
我创建一个单独的集合,每晚刷新数据,我做的第一件事就是删除其中当前的所有条目。通过分析,我发现此删除命令可能需要长达 101190 毫秒的时间。
这正常吗?为了加快这个查询的速度,我应该做些什么吗?似乎清空集合应该是相当即时的。
感谢您的帮助!
I create a separate collection nightly refreshing data, and the first thing I do is remove all the entries which are currently in it. Through profiling I find that this remove command can take upwards of 101190 milliseconds..
Is this normal? Is there something I should be doing in order to speed this query up? it seems that just emptying a collection should be fairly instant.
Thank you for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在没有过滤条件的情况下执行
remove()
可能会非常慢,因为必须从集合数据文件和任何存在的索引(包括 _id 上的默认索引)中删除每个文档。如果您要删除集合中的所有文档,特别是如果您仅使用默认索引或者可以在应用程序代码中轻松重新创建索引,则可以
drop()
该集合相反,然后开始添加新数据。您可以使用
renameCollection
命令 就像(在伪 Python 中):Doing a
remove()
with no filter condition can be very slow, since each document must be removed from the collection data files and any indexes which exist (including the default index on _id).If you are removing all the documents in a collection, and especially if you are only using the default index or you can re-create your indexes easily from within your application code, you can
drop()
the collection instead, and then begin adding your new data.You can further minimize the time that the collection appears to be empty or partially full by using the
renameCollection
command like (in pseudo-Python):dcrosta 描述了
remove()
可能很慢的原因,这也是为什么清除集合中的所有数据不是一个好主意。由于您可以在 MongoDB 中动态创建或删除集合,因此更快更好的方法是简单地删除集合,然后插入新文档(集合将自动重新创建)。
因此,只需先发出
db.collname.drop()
即可。dcrosta has described the reason why
remove()
could be slow, and that's also why that's not a good idea to clear all data in a collection.Since you can create or drop collection on the fly in MongoDB, a much quicker and better way is to simply drop the collection, then insert new documents (collection will be re-created automatically).
So just issue a
db.collname.drop()
first.