SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Deleting Large Number of Records


Deleting Large Number of Records

Author
Message
bhart-902952
bhart-902952
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 13
Seems a bit overkill. A quick way around having to do the extra leg work is:

Setup a variable table **
Toss your ID set based on your critiera
Setup a while loop to delete blocks of IDs (in thousands) from your main table using you temp table IDs as guide

** For super data sets use a temp table and put the appropirate indexes in place

This approah is quick, easy, doesn't lock the table and can be done in production hours

Smile
sjimmo
sjimmo
SSCarpal Tunnel
SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)

Group: General Forum Members
Points: 4890 Visits: 2907
Lynn
rob.lobbe (9/15/2009)
--------------------------------------------------------------------------------
This works if you are the ONLY one using the database.
Running in a production system you can't just go about changing the recovery model.

as for a 'batch' delete

select 1
while @@rowcount > 0
begin
delete top () ....
end

if you are concerned about log growth have ANOTHER process manage it.
I'm not sure if you are talking about my code or not, but if you are, it will work in production


I read this article with fascination as I just finished this week deleting 6 years worth of records from a warehouse, with several tables needing 1.4 B records removed. Unfortunately our maintenance window is very small, and full. Deleteing records was a slow tedious process. I created procedures very similiar to what you had written, using batches of 10,000 as well as the rowlock hint. We were able to run this throughout the production day with minimal impact on the users. I checked frequently as obviously they took priority. Actually, without this we would still be working at it. That maintenance window I spoke of was only 4 hrs.

Steve Jimmo
Sr DBA
“If we ever forget that we are One Nation Under God, then we will be a Nation gone under." - Ronald Reagan
john.campbell-1020429
john.campbell-1020429
SSC Veteran
SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)SSC Veteran (284 reputation)

Group: General Forum Members
Points: 284 Visits: 336
I agree with Ten Centuries on 'keep it simple'. Before I start this kind of operation, I have a backup of either the database or the table that I can restore if needed. I then usually switch the database to simple so I am not making huge transaction log backups. In most production environments there would not be time to track, organize and restore all of them.....

Because of the nature of most of these tables, lack of partitioning and other issues, deleting the top 10000 records will usually bring production to a screeching halt because of the locking blocking issues.

In most cases, I identify the group of records to delete by their clustered key and copy that clustered key value to a scratch table. I then set up a while loop to select top top xx records from my scratch table into a temp table, delete the records from the production table joined to the temp table by the clustered key, and then delete the records from the scratch table. I can do a count on the scratch table to tell me how many records I have to go at any time.

This makes for much more work but the benefits are that you can start your delete record size at a moderate, for your table, size. Monitor the database and server, check for locks and blocks and adjust the size of the individual deletes either up or down depending on the activity on the database and server. An additional benefit is that you can kill the operation at any time and if there is a rollback, it is only a small one, and you can just restart the code when you are ready to run again. In heavily used tables a wait of 1 to 5 seconds at the bottom of the loop is sometimes called for.

If production is up and running with no major delays and complaints about poor performance, does it really matter if you take 2 weeks to delete 200 + million records from the sales table.....

This is not a job that I walk away from, but monitor rather closely, and will turn off or on depending on the business needs. It's more work for me, but production is running, generating money so that I can be paid :-) and it gets the job done, which is what it is all about anyway...
David Data
David Data
SSC Eights!
SSC Eights! (887 reputation)SSC Eights! (887 reputation)SSC Eights! (887 reputation)SSC Eights! (887 reputation)SSC Eights! (887 reputation)SSC Eights! (887 reputation)SSC Eights! (887 reputation)SSC Eights! (887 reputation)

Group: General Forum Members
Points: 887 Visits: 828
Is deleting in batches likely to save elapsed time?

I frequently have to delete a million+ records from each of several many-million record tables, based on a short indexed field.
SS 2008, SIMPLE recovery model, plenty of disk space, and it doesn't matter if the table is locked for the duration.

The deletes can add several minutes to my ETL process. Would it be worth experimenting with batched deletes in this case, or would the change probably have little effect on elapsed time?
khalprin
khalprin
SSC Journeyman
SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)

Group: General Forum Members
Points: 80 Visits: 70
I know just enough about this to be dangerous...so I have a basic question.

One of the posters (GSquared) offered this method:


select 1;
while @@rowcount > 0
delete top (1000)
from table
where x= y;



Is this simply for deleting in batches so the table remains available, or does this also affect the transaction log file size differently than a single "delete from table where x=y" ?

Thanks.
TheSQLGuru
TheSQLGuru
SSC-Dedicated
SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)

Group: General Forum Members
Points: 33503 Visits: 8682
One potentially HUGE performance item I didn't see touched on is if the column(s) in the WHERE clause are indexed you should do some initial testing to adjust batch size such that you get index seeks and bookmark lookups for the DELETE action. On very large tables this can provide a TREMENDOUS increase in both performance (avoids iterative large table scans) AND concurrency (don't lock table).

Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
arsrini
arsrini
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 10
Nice article Lynn...Is there a similar code for SQL 7? I have 13 databases which have over 500 million records and I need to delete those that fall in the date range between 01/01/2001 and 31/12/2004. Could you help?
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)SSC Guru (68K reputation)

Group: General Forum Members
Points: 68819 Visits: 18570
Nice one Lynn. Glad to see the re-publish on it



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

TheSQLGuru
TheSQLGuru
SSC-Dedicated
SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)

Group: General Forum Members
Points: 33503 Visits: 8682
d@mmit! I had a nice response typed in and submitted and it isn't showing up! Sad

Anyway, let me try to recreat.

One thing I didn't see mentioned is if the WHERE clause field(s) are NCindexed it can be HUGELY beneficial for both performance AND concurrency to set the batch size (which I usually HARD CODE) such that you get index seeks/bookmark lookups for the DML action at hand. This avoids repeated table scans, which for (very) large tables can be REALLY REALLY bad thing to do for both perf and concurrency. I have used this to great effect many times in the past.

Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
dpersson-635827
dpersson-635827
SSC Journeyman
SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)

Group: General Forum Members
Points: 79 Visits: 198
This is how I do it with a little bit of output. You can shorten it up by removing the RaisError output message.


While (1=1)
Begin

Delete Top (10000) From
[DimProductStyle]
From
[DimProductStyle] With (nolock)
Left Join [Stp_Ods_Live]..[InvProductStyle] With (nolock)
On [DimProductStyle].[StyleCode] = [InvProductStyle].[StyleCode]
Where
[InvProductStyle].[StyleCode] Is Null

Declare @RowCountDelete integer; Set @RowCountDelete = @@RowCount

If (@RowCountDelete > 0)
Raiserror('%i Records Deleted',0,1,@RowCountDelete) With NoWait;
Else
break;
End
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search