Solved

Most efficient way to update?

Posted on 2006-06-28
2
790 Views
Last Modified: 2008-03-10
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
Comment
Question by:rzup
2 Comments
 
LVL 21

Accepted Solution

by:
Kevin3NF earned 250 total points
ID: 17002834
>>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
 
LVL 18

Assisted Solution

by:ShogunWade
ShogunWade earned 250 total points
ID: 17007072
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have a large data set and a SSIS package. How can I load this file in multi threading?
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question