SQL Server Admin
T-SQL Articles

April 02, 2008

How to delete records from a large table in small portions




At times, you need to delete records from a large table. Performing a delete operation on such a table can become quiet resource intensive. In order to increase performance, you can delete the rows in small portions. Let us see how using this query:


-- Create a temporary table CustomersTemp
SELECT * into Northwind.dbo.CustomersTemp from
Northwind.dbo.Customers

-- Procedure to delete in small groups
CREATE PROC spoc_deleteinsmallgroups
@NoOfRowsToDelete int
AS
DECLARE @CurrentRowCount int

-- Count the number of rows in the table
SELECT @CurrentRowCount = (SELECT COUNT(*) FROM Northwind.dbo.CustomersTemp)

-- Loop and delete records in small bits till the rowcount is 0
WHILE(@CurrentRowCount > 0)
BEGIN
DELETE TOP (@NoOfRowsToDelete) FROM Northwind.dbo.CustomersTemp
-- RECOUNT
SELECT @CurrentRowCount = (SELECT COUNT(*) FROM Northwind.dbo.CustomersTemp)
Print @CurrentRowCount
END

-- Execute the procedure and pass the number of rows
-- to delete at a time
EXEC spoc_deleteinsmallgroups 25


Did you like this post?
kick it on DotNetKicks.com
subscribe via rss subscribe via e-mail
print this post follow me on twitter



 
  Feedback:

comments

11 Responses to "How to delete records from a large table in small portions"
  1. Eric said...
    April 6, 2008 7:50 PM

    Huh? Is this SQL Server? You are deleting all the rows, right? Doesn't SQL server have a truncate command?

    Just drop the table and rebuild it.

  2. Suprotim Agarwal said...
    April 6, 2008 7:55 PM

    Hi,

    Thanks for your comment Eric. What if the user does not want to delete all the rows..just few rows that match a condition. (Eg: 50k rows in a table containing 2 lakh rows).

  3. the Dreamer said...
    April 29, 2008 7:49 PM

    Good work Agarwal,

    A small suggestion to it.

    Count(*) will really be time consuming if the size is too much. Instead you can use the following query to get the total row count.

    select rowcnt
    from sysindexes
    where id = object_id('customers')
    and indid = 1

  4. Suprotim Agarwal said...
    April 29, 2008 8:32 PM

    Thanks thedreamer,

    That's a good suggestion. I will try it out and check the difference in the time taken.

  5. PReddy said...
    October 10, 2008 11:18 AM

    Getting count for every iteration inside the While loop is a waste of resources. Just Check the @@RowCout after delete and Break out of loop. Here is a better version of it...
    While (1=1)
    Begin
    Delete Top X from Table
    If @@rowcount = Break Else Continue
    End

  6. Anonymous said...
    October 12, 2008 9:37 PM

    preddy..did you try it on a larger table with more than 100000 records..I think agarwal's query performs better over tehre..

  7. Anonymous said...
    October 15, 2008 1:51 PM

    pouah! try it on a table where you've got more than 190 Millions of rows ! when you knows that a simple select take 6 hours...just use the top, and then create a job

  8. techcreeze said...
    December 2, 2008 10:50 PM

    the Dreamer's idea to get rowcount is cool.

    I have also mentioned the same in my blog
    http://techcreeze.blogspot.com

  9. Anonymous said...
    February 24, 2009 1:02 AM

    Hi the same delete qry is giving error saying Incorrect syntax near top. Has anybody actually tried this? Top count works with delete command???

  10. Suprotim Agarwal said...
    February 24, 2009 6:47 AM

    Anonymous: It does work well. Please select the database as Northwind before trying it.

  11. Anonymous said...
    July 28, 2010 12:54 PM

    Does this work with SQL Server 2000? I am getting the same error as the last guy.

 

Copyright © 2009-2011 All Rights Reserved for SQLServerCurry.com by Suprotim Agarwal | Terms and Conditions