[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 817
  • Last Modified:

Most efficient way to update?

The issue we have is with efficiency of processing data in SQL Server with updates.

If you have one table (A) with 100k records and 200 columns and you need to update two columns from different source tables (B and C).  B will update 50k records and C will update 100k records.  Combined they will update 125k records.

Is it more efficient to update the columns in A all at once 1) or one at a time2)?

Basically the question is: does an update pull the entire record sets with all the columns into memory or just the relevant columns?

1)
UPDATE A
SET column1 = B.column1,
column2 = C.column2
FROM A
LEFT OUTER JOIN B
ON A.key = B.key
LEFT OUTER JOIN C
ON A.key = C.key
WHERE A.date = ‘2005-01-01’
AND B.type = 1
AND C.type = 2
 
2)
UPDATE A
SET column1 = B.column1,
FROM A
LEFT OUTER JOIN B
ON A.key = B.key
WHERE A.date = ‘2005-01-01’
AND B.type = 1

UPDATE A
SET column2 = C.column2
FROM A
LEFT OUTER JOIN C
ON A.key = C.key
WHERE A.date = ‘2005-01-01’
AND C.type = 2

 
0
rzup
Asked:
rzup
2 Solutions
 
Kevin3NFCommented:
>>Basically the question is: does an update pull the entire record sets with all the columns into memory or just the relevant columns?<<

Pulls the entire page the the relevant record is on, not just columns...I suspect it pulls all of the records to be able to do it all in one transaction without paging, assuming there's enough RAM in the box
0
 
ShogunWadeCommented:
Thje answer to which is more efficient depends on the data in the tables.     Look as the estimated query plans for each.   check the logical reads for each.    If concurrency is not an issue when you perform the update try using the WITH(TABLOCK) hint, as this will minimise the overhead of locking (often a contributory factor of poor performance on large updates)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now