Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update or Delete which is fast?

Tags:

mongodb

I am using mongoDB for an application. This application requires high frequency of read, write and update.

I am just concerned about update and delete functions. Which one is fast among these two. I am indexing the collection on one attribute. Update and Delete both fulfils my purpose, but I am not sure which one is perfect and have better performance.

like image 517
Alex Avatar asked Nov 04 '22 08:11

Alex


2 Answers

I would suggest that rather than deciding on whether you use Update or Delete for your solution, you look more on the SafeMode attribute.

SafeMode.True indicates that you are expecting a response from the server that will contain among other things, a confirmation of whether the command succeeded or failed. This option blocks the execution until you receive a response from the server.

SafeMode.False will not expect any response, and it is basically an optimistic command. You expect for it to work, but have no way to confirm it. Waiting for the response does not block the execution, therefore, you gain performance because all you need to do is to send the request.

Now you need to consider that Deletes will free us space on the server, but you will lose history and traceability of the data. Updates will allow you to keep historic entries, but you will need to make sure your queries exclude the 'marked for deletion' entries.

It is obviously up to you to find whether a Delete or Update is better, but I think the focus should be on whether you use SafeMode true or false to improve performance.

like image 128
agarcian Avatar answered Nov 10 '22 07:11

agarcian


A rather odd question but here are the things you can base your decision on :

  • Deleting will keep the collection at an optimum size. Updating (I assume you mean something like setting a deleted flag to true) will result in an ever growing collection which eventually will make things slower.
  • In-place updates (updates that do not result in the document having to be moved due to an increase in size) are always faster than updates or deleted that require documents to be (re)moved.
  • Safe = false writes will significantly improve throughput of updates and deletes at the expense of not being able to check if the update/remove was succesful.
like image 27
Remon van Vliet Avatar answered Nov 10 '22 06:11

Remon van Vliet