Uncategorized
How not to lock your database server for large deletes.
by Alec Horn on May.14, 2009, under Uncategorized
So one of my coworkers recently brought down a website accidently since someone broke the session cleaning job. So, he of course runs the procedure to delete all of the old data, and kaboom it places a lock on the table while it deletes a few hundred thousand rows.
Cute except that without that table being able to be accessed no requests would technically go through. The workaround? Simply do small units of work at a time:
WHILE 1=1 BEGIN DELETE TOP (1000) FROM [db].[dbo].[table] WHERE Expires < @now; IF @@rowcount = 0 BREAK; END