Our app uses a table in a database and we have a Sql job set up to run overnight which executes a stored procedure which just deletes data older that from the table.
DELETE FROM
MyTable
WHERE
CreatedOn < DATEADD(D, -1 * 1, GETUTCDATE());
Now depending on the data stored on that table (on the previous day), the job completes fast or very long. When It is long, the app throws timeout error inserting into the table. Obviously this is reported by the users using the app overnight.
I have following options :-
a) Change the time of the job so that minimum number of users get affected.
b) Delete certain number of rows at a time rather than delete all of them together.
delete top (10) MyTable where CreatedOn < DATEADD(D, -1 * 1, GETUTCDATE());
while @@rowcount > 0
begin
delete top (10) MyTable where CreatedOn < DATEADD(D, -1 * 1, GETUTCDATE());
end
Could anyone think of a better option?
"Could anyone think of a better option?"-- table partitioning, if you're using an edition that supports it. – Jon Seigel Feb 4 at 22:55