June 23, 2008 at 10:43 am
I have been trying to find a good sql statement which would delete all records within a particular database which is older than 2 years.
I need to keep all information 2 years and newer but can get rid of the rest after 2 years.
What would be the best solution to do this? I am using SQL 2005
June 23, 2008 at 11:20 am
You will need a "last_updated" column on all your tables. Do you have that ?
* Noel
June 24, 2008 at 2:51 am
If you have a column that has the date the record was inserted stored in it, then i would create a SQL Job to run every night and execute the following sql:
delete from myTable where myDateColumn < dateadd(yy, -2, getdate())
Matt.
June 24, 2008 at 2:54 am
and make sure you disable the index first, then do the delete, and then enable the index. Make sure you have enough space for the transaction logs as well, otherwise, delete in batches
June 24, 2008 at 9:25 pm
The first delete will probably be a good size one... but if you delete every day, the number of rows older than 2 years won't be that big... you won't need to do it in batches... and you don't need to disable indexes to do it... 😉
This type of thing can be made to run so fast, that I've got one table where everytime something does an insert to it, a trigger fires and deletes everything older than 60 days... same idea as 2 years... if you delete often enough, you won't have to delete much at any one time (except the very first time)...
--Jeff Moden
Change is inevitable... Change for the better is not.
June 26, 2008 at 6:10 am
You might want to determine if there might be any reason (legal requirements or self-preservation) to keep an archive of the deleted records in a separate database. This would allow better performance in the original db while preserving the historical information. This could be done as part of a Delete function or in a trigger. If the reason to do the delete is disk space constraints, then this might be impractical.
Toni
June 26, 2008 at 5:52 pm
toniupstny (6/26/2008)
You might want to determine if there might be any reason (legal requirements or self-preservation) to keep an archive of the deleted records in a separate database. This would allow better performance in the original db while preserving the historical information. This could be done as part of a Delete function or in a trigger. If the reason to do the delete is disk space constraints, then this might be impractical.Toni
Heh... yeah... if you think SOX is tough, wait until you get hit with the new "E-Discovery" requirements... makes SOX look like a walk in the park...
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply