Solved

Delete n number of rows at a time

Posted on 2014-03-11
5
633 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 143

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 66

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:Kent Olsen
Kent Olsen 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

PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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
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.

632 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