Solved

Improve update query performance with T-SQL

Posted on 2011-09-06
5
251 Views
Last Modified: 2012-05-12
I have a SQL table with millions of rows which I need to update. At the moment this is taking a massive amount of time which I am hoping can be improved. The SQL in question is just a basic update statment which is adding 50 to an existing number (as basic as it gets), but the scale of the change means this is taking ages to complete.

Another problem is that the change is causing the transaction log to fill up to maximum capacity, this is causing the script to end prematurly with a warning. This happens whether I have the change wrapped in a transaction or not, I have tried shrinking the database prior to the change but this still occurs either way.

I have also tried breaking the script up into portions using the ID field, i.e. update all rows where ID is like '1%', '2%' etc, but this still takes forever.

Does anyone have any idea how I can do this as efficiently as possible?
0
Comment
Question by:Blowfelt82
  • 2
  • 2
5 Comments
 
LVL 38

Expert Comment

by:Aaron Tomosky
ID: 36488985
Can you post the current query?
0
 

Author Comment

by:Blowfelt82
ID: 36489029
UPDATE REGISTRATION SET REF_NO = REF_NO + 50

0
 
LVL 5

Accepted Solution

by:
DavidMorrison earned 500 total points
ID: 36489079
Is this field in any non clustered indexes?

if so dropping them, doing the update, and then re-adding will speed up the process.

the transaction log issue will be due to your logging mode I would imagine, if you can change this it would help.

Another thing to consider is partition switching

Thanks

Dave
0
 
LVL 38

Expert Comment

by:Aaron Tomosky
ID: 36489093
Is refno an integer? Is there a pk or some kind of unique index? Is there a timestamp column also being updated?
0
 

Author Closing Comment

by:Blowfelt82
ID: 36489865
Indexes was the cause of this, after dropping them the script took seconds!
Cheers.
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Select2 jquery help 9 45
Distinct values from two tables 14 18
t-sql month question 8 42
Convert int to military time 8 20
Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

706 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

16 Experts available now in Live!

Get 1:1 Help Now