I am writing a procedure to delete all rows from multiple tables in n days.
Simple simple query easy to write
DELETE FROM [myTable] WHERE [Created] < GETDATE()-30
One problem is that there is no index in the date field - I could add one, but I worked on it by doing something like:
SELECT @var = MAX([ID]) FROM myTable WHERE Created < GETDATE()-30; DELETE FROM myTable WHERE ID < @var
Does this sound like an acceptable method?
The problem is that the table is huge, and this query will probably delete hundreds of thousands of rows in each run.
Running it on a (slightly slow) test server takes about an hour or so, and it kills the table from other processes trying to read / write to it.
I do not mind that it takes some time (albeit faster) - but I can not get it to lock the table for an hour while it is working, as there is a constant read / write (mostly writing).
Knowing my database is pretty basic since I am not a dba encoder.
Could someone give me a decent method to accomplish this task - in the most efficient way.
jb.
source share