Solved

Most efficient way to update?

Posted on 2006-06-28
2
764 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PL/SQL query 14 50
SQL Server 2012 Row Selection 2 30
SQL JOIN 6 35
Split Data in 1 column into 2 columns 8 15
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now