Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server - join or subquery in update statements?

Is there a reason to use one of these UPDATE statements over the other with regards to performance?

 UPDATE myTable
 SET    fieldx = 1
 FROM   myTable AS mt
      , myView AS mv
 WHERE  mt.id = mv.id


 UPDATE myTable
 SET    fieldx = 1
 WHERE  id IN ( SELECT id
                     FROM   myView )
like image 647
Chris Burgess Avatar asked Dec 13 '25 12:12

Chris Burgess


1 Answers

They'll probably come out with the same execution plan, meaning there is no difference. To confirm, just try each one in SSMS with the "Include Execution Plan" option switched on.

In fact, the one I would go for is:

UPDATE mt
SET mt.fieldx = 1
FROM myTable mt
    JOIN myView mv ON mt.ID = mv.ID

I much prefer this syntax. Though, it too will come out with the same execution plan.

To demonstrate this, I ran a test with each variant of the UPDATE statement. As the execution plans below show, they all came out the same - all perform the same:
alt text http://img707.imageshack.us/img707/7801/60422461.png


alt text http://img683.imageshack.us/img683/7874/41210682.png


alt text http://img708.imageshack.us/img708/5020/20506532.png

like image 183
AdaTheDev Avatar answered Dec 16 '25 22:12

AdaTheDev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!