February 18, 2011 at 4:56 am
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
🙂
February 18, 2011 at 6:41 am
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
February 18, 2011 at 6:44 am
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...
February 18, 2011 at 7:04 am
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?
February 18, 2011 at 7:12 am
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.
February 18, 2011 at 8:25 am
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
February 18, 2011 at 9:06 am
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?
February 18, 2011 at 9:22 am
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[/url]
Learn Extended Events
February 18, 2011 at 9:48 am
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 on googles mail service
February 18, 2011 at 4:52 pm
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
February 19, 2011 at 6:45 am
Great discussion and helpful solutions.
1) many of us are a little too touchy about critiques of our code
2) tell your management how much disk space you need to run sql server and that it's a cost of doing business. Otherwise tell them to use a pencil and notepad to keep track of data. So many posts about shrinking files, "ballooning" log files etc etc. Just buy the danged disk space and suck it up.
3) I don't know if this is a trend, but in our shop backups were turned over to the systems team using Commvault. Any backups done outside of that product could break the log chain. Naturally this severely ties my hands. Also, any change in recovery model will cause Commvault to react according to it's programming, often causing an immediate full backup.
4) You "could" go to simple recovery on a production database, but if the company asks you to restore to a point in time, and you can't do it because you stopped transaction log backups, you might be looking for another job.
I've actually considered changing modes during our weekend maintenance because Commvault can't keep up and it's log backups are now occasionally finishing many hours apart, rather than the scheduled 15 minute interval. But I'm hoping this will give me the ammunition I need to establish weeknight maintenance windows so it's not all done on the weekend.
February 20, 2011 at 4:07 am
A great article - my only concern is that out production database is replicated to two other servers and log shipped to a third, so you couldn't use this process as is without breaking the log chain. But I think I can use the general gist of the process to only delete X rows at a time, by putting the delete code into a SQL Agent job and setting it to run every say 10 minutes e.g. to delete a million rows, you could have a job that deletes 10,000 rows and have it run 100 times.
Viewing 13 posts - 61 through 72 (of 72 total)
You must be logged in to reply to this topic. Login to reply