Long Running Update statement
Hi
I am running an update against a table with over 100,000,000 rows which takes forever. Is there a better way to write a query
like this?
UPDATE [dbo].[Test_Table]
SET [Column_34] =(
CASE
WHEN [Column_2] > 25 AND [Column_2] <= 34 THEN 1
ELSE 0 END)
February 5th, 2015 10:34pm
Hello,
Do you have an index on [Column_2]? If not, it's going to do a table scan... which may take a while...
-Sean
February 5th, 2015 11:34pm
Instead of updating everything in one shot, you can update in chunks. You can use TOP to select x number of rows to update, however, you would need some criteria to check and update only records that have not been updated. What does column_34 column values
are like right now? If the column_34 values are not either 0 or 1 to start off, you can use that as your condition. Updating in chunks will also help curb the transaction log growth.
February 6th, 2015 1:22am
What indexes you've on the table?
Did you check execution plan? What does it suggest?
February 6th, 2015 4:15am
Yes I have an index on [Coulumn_2]
February 6th, 2015 10:45am
Good suggestion, thanks
February 6th, 2015 12:27pm
How much rows does it update? You can also use a TOP clause , especially if SQL Server decides to scan the table due to lack of the useful index.
UPDATE
TOP (1000) [dbo].[Test_Table]
SET [Column_34] =(
CASE
WHEN [Column_2] > 25 AND [Column_2] <= 34 THEN 1
ELSE 0 END)
February 7th, 2015 10:51am