you're reading...
SQL Server, T-SQL

Batch Large Deletes

A common issue with large DML operations, like deletes, is the impact on system resources especially tempdb. Even in simple mode large delete operations can have a significant impact on your database & tempdb resources.

I often use the following code to batch my delete operations utilizing the t-sql windowing function capabilities (You could always use GROUP BY and HAVING cnt > 1):

SET @r = 1;
WHILE @r > 0
   DELETE TOP (5000) del FROM
   (select *, rn=row_number() over (partition by [Office],preparer order by [Office],Preparer)
    FROM [Database].[Schema].[YourTable]) del
    where rn > 1;
    SET @r = @@ROWCOUNT;
    CHECKPOINT;    — if simple
    — BACKUP LOG … — if full

This code is relatively efficient, fast and works well.

About ldgaller

Accomplished Data Warehouse Architect, DBA and Software Architect with over 15 years of professional experience and demonstrated success designing and implementing solutions that improve business functionality and productivity. Highly diverse technical background with proven ability to design, develop and implement technology on an enterprise level. I approach all projects with passion, diligence, integrity, and exceptional aptitude.


No comments yet.

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: