Delete huge data from a table without Transaction Log Full


Suggested approach resolves problem of transaction log growth but it does not comply with consistancy rule. Delete is not executed as a single transaction (otherwise we will have problem with transaction log). Database recovery model should be set to single before deletion start. And here is the script

SET NOCOUNT ON

SET ROWCOUNT 100000

declare @r int

set @r = 10

WHILE @r > 0

begin

delete ord

from  orders w (nolock)

right outer join orderdetails ord on ord.ID = w.DetailID

where w.xpitordernumber is null

  and conditionalfield = 1

set @r = @@ROWCOUNT

end

  So the table orders will be purged by 100k rows batches wihout holding huje amount of locks,transaction log growth etc. Microsoft says that set rowcount won’t be supported for data modification operations (such as delete, update, insert) however it still works on my SQL Server 2008 RTM.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: