简体   繁体   中英

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 )

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM