Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of Delete/Create vs Delete/Update

Trying to figure out best practice here.

I am writing some end points that will essentially combine a group of objects if their dates become contiguous.

Say there are three objects I want to group.

obj1 = {id: 1, start: 1/1/18, end: 1/2/18}
obj2 = {id: 2, start: 1/2/18, end: 1/3/18}
obj3 = {id: 3, start: 1/4/18, end: 1/5/18}

Are there any performance benefits or best practices to either of the following -

Create one new object which is essentially the 3 previous objects grouped together by date. Then delete the three other objects.

obj4 = {id: 4, start: 1/1/18, end: 1/5/18}
obj1.delete()
obj2.delete()
obj3.delete()

or

Update one objects fields to represent the new dates. Then delete the other two objects.

obj1.end = 1/5/18
obj1.save()
obj2.delete()
obj3.delete()

Just pseudo code here, but this is in a django app.

Thanks

like image 692
hancho Avatar asked Dec 07 '25 06:12

hancho


1 Answers

This depends on the database, but PostgreSQL (since you mentioned it in the tags) generally implements UPDATEs as a DELETE followed by an INSERT anyway, so the difference should be neglegible.

like image 137
Teo Klestrup Röijezon Avatar answered Dec 09 '25 19:12

Teo Klestrup Röijezon