Solved

Delete n number of rows at a time

Posted on 2014-03-11
5
591 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

744 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

12 Experts available now in Live!

Get 1:1 Help Now