Solved

Delete n number of rows at a time

Posted on 2014-03-11
5
599 Views
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,
0
Comment
Question by:trustworthy
5 Comments
 
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:
SET ROWCOUNT 1000
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
SET ROWCOUNT 1000
WHILE @r <> 0
BEGIN
   DELETE yourtable WHERE your_date_field < dateadd(day, -1, getdate())
   set @r = @@ROWCOUNT
END 

Open in new window


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

Accepted Solution

by:
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... )
0
 
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.
0
 
LVL 45

Assisted Solution

by:Kdo
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)

or

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!
0
 

Author Closing Comment

by:trustworthy
ID: 39921535
Thank you all
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

778 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