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 «««45678»»

Deleting Large Number of Records Expand / Collapse
Author
Message
Posted Friday, February 18, 2011 4:56 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, July 18, 2013 9:59 AM
Points: 1, Visits: 12
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

:)
Post #1066268
Posted Friday, February 18, 2011 6:41 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, December 3, 2014 8:17 AM
Points: 2,917, Visits: 2,537
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
Post #1066337
Posted Friday, February 18, 2011 6:44 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, August 18, 2014 10:57 AM
Points: 56, Visits: 335
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...
Post #1066339
Posted Friday, February 18, 2011 7:04 AM
SSC-Enthusiastic

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

Group: General Forum Members
Last Login: Tuesday, September 30, 2014 3:20 PM
Points: 115, Visits: 747
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?
Post #1066367
Posted Friday, February 18, 2011 7:12 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Friday, April 4, 2014 6:27 AM
Points: 20, 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.
Post #1066373
Posted Friday, February 18, 2011 8:25 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Yesterday @ 1:17 PM
Points: 4,474, Visits: 6,407
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 at GMail
Post #1066429
Posted Friday, February 18, 2011 9:06 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, July 20, 2012 9:03 PM
Points: 3, 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?
Post #1066460
Posted Friday, February 18, 2011 9:22 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Yesterday @ 5:17 PM
Points: 18,081, Visits: 16,116
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
Post #1066468
Posted Friday, February 18, 2011 9:48 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Yesterday @ 1:17 PM
Points: 4,474, Visits: 6,407
d@mmit! I had a nice response typed in and submitted and it isn't showing up!

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 at GMail
Post #1066485
Posted Friday, February 18, 2011 4:52 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Wednesday, November 9, 2011 2:22 PM
Points: 67, 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
Post #1066706
« Prev Topic | Next Topic »

Add to briefcase «««45678»»

Permissions Expand / Collapse