Delete n number of rows at a time

Posted on 2014-03-11
Last Modified: 2014-03-11
Hi all,

I need to delete 1000 records at a time starting from now-1 day,
and I need it for both DB2 and SQLServer.

If it's 13:50 3/11/2014,
I need to delete 1000 records at a time starting from 13:50 3/10/2014

Is there a sql statement that I can (repeatedly) construct and then call repeatedly  until it is done?

Thank you,
Question by:trustworthy
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 166 total points
ID: 39920975
in sql server, you have 2 options to delete 1000 rows max:
DELETE yourtable WHERE ....

Open in new window

 DELETE TOP 1000 yourtable WHERE ... 

Open in new window

>if it's 13:50 3/11/2014, I need to delete 1000 records at a time starting from 13:50 3/10/2014

I presume that's related to the field in the column, hence the WHERE condition.
so, you shall do something like this:
WHERE yourfield < dateadd(day, -1, getdate())

>that I can (repeatedly) constru
deleting 1000 rows at a time, repeatetly will eventually use MORE resources than doing the job "at once", though there are exceptions, especially if the table to delete from has high number of lockings ... having a clustered index on the date/time field will help there a lot.

apart from that, you can do this, taking example 1 above:
declare @r int
set @r = 1
WHILE @r <> 0
   DELETE yourtable WHERE your_date_field < dateadd(day, -1, getdate())
   set @r = @@ROWCOUNT

Open in new window

I presume that db2 is very similar in this regards
LVL 37

Accepted Solution

momi_sabag earned 167 total points
ID: 39920980
could you post here the structure of your tables?
basically you should be able to do it using sub-queries
something like

for db2
delete from table
where primary_key in (select primary key from your_table where... fretch first 1000 rows only)

for sql
delete from table
where primary_key in (select top 1000 primary key from your_table where... )
LVL 65

Expert Comment

by:Jim Horn
ID: 39921055
All of a3's above solutions will work.  

If your table / total number of rows you're deleting is huge, another benefit of the WHILE loop solution is that you can also insert rows into a 'log' table, if you feel the need to want to know the status of this code while it is executing.
LVL 45

Assisted Solution

Kdo earned 167 total points
ID: 39921108
Use Momi_Sabag's suggestion.  :)

You can also use that technique when the timestamp column is not the primary key, but it is indexed.  (If the column isn't indexed and you want to delete a specific 1,000 rows all techniques could have performance issues.)

delete from table
where primary_key in (select primary key from your_table where... fetch first 1000 rows only)


delete from table
where date_column in (select date_column from your_table where... fetch first 1000 rows only)

Hi Momi.  Haven't seen you around in a bit.  I hope that all is well with you!

Author Closing Comment

ID: 39921535
Thank you all

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel conversion issue with Sql server 14 47
Help with SQL Server Stoplist 2 20
Updating a column using a lookup on another table. 6 20
SQL Server Question 5 28
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now