CRUD icon indicating copy to clipboard operation
CRUD copied to clipboard

use upsert instead of 2 queries per row

Open pxpm opened this issue 2 years ago • 0 comments

WHY

BEFORE - What was wrong? What was happening before this PR?

We were using DB:: transactions to wrap the reordering updates on database. We did this because we were looping over all the tree elements and doing two database queries for each of the elements.

Wrapping the queries in a DB transaction does not make less queries, it just means that if any of the queries fail we are able to revert the whole process. So for ordering 10 items, we would still make 20 queries.

I switched to use a combination of upsert and validating the ids to do the WHOLE process in only two queries.

  • One query gets the db id's
  • One query updates all the values

If we do it all in one query, if the query fails it fails as a whole, and we don't need to wrap this in a transaction.

This would allow us to also fix some compatibility issue with MongoDB reported in https://github.com/Laravel-Backpack/CRUD/issues/4909

pxpm avatar Oct 11 '23 13:10 pxpm