Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it more efficient to execute one update with multiple columns vs multiple updates

What do you think is better. Update every time all columns of an table even if there are not all values changes, or update only the changed columns with multiple updates. The idea is, rather than to update every single change immediately, waiting for a few changes and then update all columns, but i don't want to implement a logic who determines which columns has been changed.

UPDATE myTable 
SET col1 = newVal1, 
    col2 = oldVal2,
    col3 = newVal3,
   ... 
WHERE x = y

vs.

UPDATE myTable SET col1 = newVal1 WHERE x = y
UPDATE myTable SET col3 = newVal3 WHERE x = y
...

I am using SQL Server 2014 Express.

like image 543
mitch Avatar asked Dec 15 '22 02:12

mitch


1 Answers

The first query will perform much better because, it just needs to do a single scan/seek on the table. But in the first query you can update the column which has new values to be updated:

UPDATE myTable 
SET col1 = newVal1, 
    col3 = newVal3,
... 
where x = y

But the second query has to scan/seek the table for each Update which will take more time than the first query.

like image 148
Pரதீப் Avatar answered Mar 30 '23 00:03

Pரதீப்