TITLE!

Uncategorized

How not to lock your database server for large deletes.

by 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
2 Comments more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!