MongoDB:更新/Upsert与插入 [英] MongoDB: Update/Upsert vs Insert

查看:87
本文介绍了MongoDB:更新/Upsert与插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我注意到进行多次更新之间的巨大性能差异(通过批量操作)与插入(多个文档).我想知道我是否正确:

Recently I notice a huge performance difference between doing multiple upserts (via bulk operations) vs an insert (multiple documents). I would like to know if I am correctly on this:

  • Upsert/Updates就像find()update()一样,因此它可以做两件事:读和写
  • 插入将使写入变得更快
  • Upsert/Updates will be like a find() and update() so it does 2 things read and write
  • Insert will just write so its a lot faster

因此,性能差异如何?

Thus the performance difference?

如果是这种情况,我想知道是否需要定期进行大量编写,而不是更新文档,而是使用createdOn字段编写了一个新文档.然后进行查询,我只查询按createdOn DESC排序的文档.我想知道这是否是个好方法吗?或者,还有更好的方法?

If this is the case, I wonder if I need a lot of writes regularly, instead of updating a document, I write a new document with a createdOn field. Then to query, I will just query for documents, sorted by createdOn DESC. I wonder if this is a good method? Or is there a better way?

  • 我确实想知道集合中是否有索引,这是否可以加快更新速度?但是,此索引会不会减慢写入部分的速度?
  • 使用第二种方法,即我只进行插入操作,那么当我有太多文档时,它会减慢速度吗?实用(加快写入速度)吗?
  • 我也尝试增加连接池的大小.不知道什么是最佳选择,但是我尝试了20次,我发现通过mongostat每秒可以处理20个左右的查询.我期望它会更高.

推荐答案

如果要插入文档,Mongodb需要检查是否存在具有相同objectId的文档.如果存在,则无法插入文档.

If your inserting document, Mongodb needs to check whether the document with the same objectId is exists or not. If its exists document cannot be inserted.

相同情况适用于更新.它需要检查文档是否存在.否则无法执行更新.如果找不到基于ObjectId/Indexed字段的文档,更新查询就会变慢.

Same case apply to Update. It needs to check whether the document exists or not. else update cannot be performed. The case where your update query will slow if your not finding document based on your ObjectId / Indexed field.

插入/更新文档的其他性能应相同.

Else performance for inserting / updating document should be same.

例如.....

所以插入可以像这样///(快速)

So Insert can be like this //(Fast)

  1. (检查文档->找不到->插入新文档)其他
  2. (检查文档->找到->无法插入)

并使用upsert更新(可用ObjectId)///(快速)

And Update with upsert (ObjectId available) //(Fast)

  1. (检查文档->找不到->插入新文档)其他
  2. (检查文档->找到->更新文档)

或使用upsert更新(没有ObjectId)//这很慢

Or Update with upsert (Without ObjectId) //This is slow

  1. (查找ObjectId(慢)->找不到->插入新文档)否则
  2. (查找ObjectId(慢)->找到->更新文档)

这篇关于MongoDB:更新/Upsert与插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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