views:

136

answers:

2

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 )
+4  A: 

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


alt text


alt text

AdaTheDev
I prefer this syntax as well. It's much more explicit.
Michael Krauklis
A: 

While this doesn't speak to performance, I like the first example better so that I could vet it easier without changing the underlying structure of the code. Notice where and what I put in comments:

UPDATE myTable 
SET    fieldx = 1
--SELECT *
FROM   myTable AS mt 
      , myView AS mv 
WHERE  mt.id = mv.id
Yoav