Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB: Update/Upsert vs Insert

Tags:

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 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?

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?

  • I do wonder if I have index on the collection, might it speed up the update? But wont this index slow down the write portion then?
  • With the 2nd way, where I only do inserts, will it slow down then I have too many documents? Is it practical (to speed up the writes)?
  • I have also tried increasing the connection pool size. Not sure whats the optimum, but I tried 20 and I see I can handle abt 20 queries per sec thru mongostat. I expected it to be alot higher.
like image 801
Jiew Meng Avatar asked Jan 31 '16 10:01

Jiew Meng


1 Answers

If you are inserting document, MongoDB needs to check whether the document with the same ObjectId exists or not. If it exists document cannot be inserted.

Same case applies to Update. It needs to check whether the document exists or not. Otherwise, update cannot be performed. The case where your update query will slow down is when it can't find document based on your ObjectId / indexed field.

Otherwise, performance for inserting / updating document should be the same.

So Insert can be like this //(Fast)

  1. (Check for document -> Not Found -> Insert new document) Else
  2. (Check for document -> Found -> Cannot be inserted)

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

  1. (Check for document -> Not Found -> Insert new document) Else
  2. (Check for document -> Found -> Update the document)

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

  1. (Find ObjectIds (Slow) -> Not Found -> Insert new document) Else
  2. (Find ObjectIds (Slow)-> Found -> Update the documents)
like image 71
Code OverFlow Avatar answered Sep 17 '22 15:09

Code OverFlow