December 6, 2006 at 11:00 pm
Hi Everyone,
I have a rather large table (130,000,000 rows), and I want to get it down to a manageable 1,000,000 rows, by removing old data based on a date field. What is the best way to do this? I don't want to use DELETE because I'm sure this will fill up the transaction log.
In DB2, we would do a REORG WITH DISCARD so there is no log activity. Does SQL Server have an equivalent?
Thanks.
December 7, 2006 at 1:32 am
With that number of rows that you want to delete and the few that you need to keep, I'd suggest that you copy the rows you do want to keep to a temp table, truncate the massive table (logged as page deallogations, not row deletes) then copy the saved data back.
HTH
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
December 7, 2006 at 6:29 am
Another trick is to delete a section. Say you want to delete all records before Jan 1, 2006. Create a loop
declare @date datetime
set @Date = '1/1/1960'
while @Date <= '1/1/2006'
begin
delete MyTable where RefDate < @Date
set @Date = dateadd (day, 1, @Date)
end
Russel Loski, MCSE Business Intelligence, Data Platform
December 7, 2006 at 3:52 pm
I would go for Gail's advice.
Before doing so, change your database recovery model to bulk-logged. Use bcp to copy the 1M rows, truncate your table, then use BULK INSERT to bring back the 1M rows.
December 8, 2006 at 2:51 pm
I tend to agree with Gail. Here's a couple of modification and potential 'gotchas'. We have to start out with the "gotchas" first because they may alter your choice of actions ...'
Now a fsomewhat generic plan with a few modified steps:
RegardsRudy KomacsarSenior Database Administrator"Ave Caesar! - Morituri te salutamus."
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply