Question

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 )
Was it helpful?

Solution

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

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top