Reputation: 6105
I have a table, TBL1
, with only GUIDs.
I have another table, TBL2
, in which the primary key is GUID and it also has some other columns. I want to update one of the columns in TBL2
table based on whether the GUID is in TBL1
.
Which of the following queries is faster and/or more reliable to use for that?
MERGE INTO [db].[dbo].[TBL1] AS target
USING [db].[dbo].[TBL2] as source
ON target.GUID = source.GUID
WHEN MATCHED THEN
UPDATE SET
StatusColumn = 0;
or
UPDATE [db].[dbo].[TBL1]
SET StatusColumn=0
WHERE GUID IN (SELECT GUID FROM [db].[dbo].[TBL2])
or maybe something else?
Upvotes: 2
Views: 1702
Reputation: 171216
The answer to this question can only come from the execution plan. From the plan you posted (http://i.imgur.com/6vB2t.png) we can see the following:
Diagnosing this without a plan is just guessing. Look at the plan and/or measure. The measuring provides the answer, but the plan provides the understanding of the answer.
Upvotes: 3
Reputation: 60518
I think the fastest way would probably be a third option using a join:
UPDATE t1 SET StatusColumn=0
FROM db.dbo.TBL1 t1
INNER JOIN db.dbo.TBL2 t2 ON t1.guid = t2.guid
Upvotes: 1