Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Update query - need suggestion Expand / Collapse
Author
Message
Posted Friday, April 9, 2010 9:57 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, February 3, 2011 7:44 AM
Points: 151, Visits: 341
I am planning to do an update on a very large table (table1 ~250million). So need to know which one of the below query would be better & faster? Appreciate your reply. Both the fields from table1 are not indexed.

UPDATE a
SET a.field1 = b.field1
FROM table1 a (NOLOCK)
INNER JOIN table2 b (NOLOCK) ON a.id = b.id AND a.status = -1

(OR)

UPDATE a
SET a.field1 = b.field1
FROM table1 a (NOLOCK)
INNER JOIN table2 b (NOLOCK) ON a.id = b.id
WHERE a.status = -1


BASKAR BV
http://geekswithblogs.net/baskibv/Default.aspx
In life, as in football, you won’t go far unless you know where the goalposts are.
Post #900674
Posted Friday, April 9, 2010 10:30 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 5:18 AM
Points: 5,014, Visits: 10,515
The two UPDATEs produce the exact same exec plan.
Just a few points:

1) NOLOCK has no effect on the table to update
2) NOLOCK can easily generate inconsistent data, get rid of it
3) Updating such a large table will make you log file explode. Do it in batches and back up the transaction log between batches, something like this

DECLARE @rows int
DECLARE @batchsize int
SET @rows = 1
SET @batchsize = 10000 -- whatever size you find appropriate

WHILE @rows > 0
BEGIN
UPDATE TOP(@batchsize) a
SET a.field1 = b.field1
FROM table1 a
INNER JOIN table2 b
ON a.id = b.id
WHERE a.status = -1

SET @rows = @@ROWCOUNT

BACKUP LOG ...
END

Obviously you would have to mark the rows already processed by the batch in order to avoid updating the same records over and over.


--
Gianluca Sartori

Get your two-cent-answer quickly
spaghettidba.com
@spaghettidba
Post #900705
Posted Friday, April 9, 2010 10:40 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:24 AM
Points: 12,887, Visits: 31,835
I think by just adding to the WHERE statement to Gianluca's fine example, you'd make sure you don't change fields where they already match, you'll prevent re-processing of the same rows...you might need to take nulls into account as well:
DECLARE @rows int
DECLARE @batchsize int
SET @rows = 1
SET @batchsize = 10000 -- whatever size you find appropriate

WHILE @rows > 0
BEGIN
UPDATE TOP(@batchsize) a
SET a.field1 = b.field1
FROM table1 a
INNER JOIN table2 b
ON a.id = b.id
WHERE a.status = -1
AND a.field1 != b.field1
--AND ISNULL(a.field1,'') != ISNULL(b.field1,'') --is there nulls?

SET @rows = @@ROWCOUNT

BACKUP LOG ...
END



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #900710
Posted Friday, April 9, 2010 10:42 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, February 3, 2011 7:44 AM
Points: 151, Visits: 341
Thanks.

Eventhough table1 is very large, table2 would have less number of records around 3000 rows so i hope batch update is not required.

Also i am planning to add "date" condition to where clause from table1 to filter the number of records from table1 eventhough logically ID from table2 does not come below the date condition. Will that where clause help? "date" field has an non-clustered index in the table.




BASKAR BV
http://geekswithblogs.net/baskibv/Default.aspx
In life, as in football, you won’t go far unless you know where the goalposts are.
Post #900712
Posted Friday, April 9, 2010 10:47 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 5:18 AM
Points: 5,014, Visits: 10,515
It depends. Compare the execution plans with and without date condition, you'll find out for sure.
If unsure, you could also try the performance of the two, changing UPDATE to SELECT.

If you could post tables and indexes script or attach the exec plan you would get better advice.


--
Gianluca Sartori

Get your two-cent-answer quickly
spaghettidba.com
@spaghettidba
Post #900715
Posted Friday, April 9, 2010 10:50 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:24 AM
Points: 12,887, Visits: 31,835
it might help...the devil is in the details.

the pseudo code example we have so far doesn't give us enough to work with...if the big table has an index on the datetime column, the WHERE statement might use an index to find the records, so it would be faster and lock less records (potentially) during the update. but depending on the number of records being updated, the optimizer might just decide it's easier for it to lock the whole table during the update; can't say for sure.

Baskar B.V (4/9/2010)
Thanks.

Eventhough table1 is very large, table2 would have less number of records around 3000 rows so i hope batch update is not required.

Also i am planning to add "date" condition to where clause from table1 to filter the number of records from table1 eventhough logically ID from table2 does not come below the date condition. Will that where clause help? "date" field has an non-clustered index in the table.




Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #900717
Posted Friday, April 9, 2010 12:55 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, February 3, 2011 7:44 AM
Points: 151, Visits: 341
This is the modified update statement based on your feedback...

UPDATE a
SET a.field1 = CASE WHEN b.field1 IS NOT NULL
THEN b.field1
ELSE a.field1
END
FROM table1 a
INNER JOIN table2 b ON a.id = b.id AND a.status = -1
where a.datekey > '20100101'

-- table is in primary filegroup and datekey clustered index under primary filegroup. After adding the where condition the clustered index scan on table1 become clustered index seek on table1 also reducing the cost from 98% to 3%. But the cost is now split to other components like hash join (34%).. earlier hash join is 1%.

But table2 clustered index seek is good since it is going to join with that big table.

Provide me if you can make some more suggestions.

volume of the tables:
table1 - 250million
table2 - 4000 records


BASKAR BV
http://geekswithblogs.net/baskibv/Default.aspx
In life, as in football, you won’t go far unless you know where the goalposts are.
Post #900779
Posted Friday, April 9, 2010 12:59 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Thursday, February 3, 2011 7:44 AM
Points: 151, Visits: 341
Forgot to mention in the previous post.

The total records which would get updated would be around 10 million records.


BASKAR BV
http://geekswithblogs.net/baskibv/Default.aspx
In life, as in football, you won’t go far unless you know where the goalposts are.
Post #900781
Posted Friday, April 9, 2010 6:08 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, August 20, 2012 10:03 PM
Points: 344, Visits: 206
How many of your records contain Nulls? Also, you may want to follow a suggestion that was put forth earlier and only update rows that are different. If there are a significant amount of Nulls or matches then you may see some improvement.

UPDATE a
SET a.field1 = b.field1
FROM table1 a
INNER JOIN table2 b ON a.id = b.id AND a.status = -1
where (a.datekey > '20100101') And (a.field1 != b.field1) And (b.field1 IS NOT NULL)
Go

Post #900908
Posted Friday, April 9, 2010 6:39 PM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Wednesday, July 16, 2014 3:01 PM
Points: 3,843, Visits: 3,836
As K Cline pointed out, put the update field comparison in the WHERE clause instead of the in a CASE statement in the UPDATE. This allows the optimizer to potentially lessen the # of rows it needs to update. The CASE statement method will not help performance.

In regards to the cost values in your execution plan, optimizing a portion of a query will most times result in the costs being split out to other tasks. This is not always a bad thing. A query has to have 100% cost somewhere so just because one task is higher than some does not always mean it is not efficient. I like to base efficiency decisions more off of logical reads than the cost value in the query plan. Improving logical reads is a more accurate measure of how much a query has improved.




John Rowan

======================================================
======================================================
Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
Post #900915
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse