Solved

sql server 2005 - eliminate blocks of 500 by 500.

Posted on 2013-06-20
17
376 Views
Last Modified: 2013-07-03
Please experts,

I need this requirement:
- Delete records leaving records of the last 60 days.
- The range of debugging for the deletion of records is 3 months. Within these three months should eliminate blocks of 500 by 500.

Since this table contains a lot of records, it is necessary to clean the table for periods, considering the date

I am using sql server 2005, i can not use cursor.

You need to clear the transaction records 500 records without locking the entire table completely and releasing locks on each transaction
0
Comment
Question by:enrique_aeo
  • 7
  • 3
  • 3
  • +2
17 Comments
 
LVL 11

Expert Comment

by:Louis01
Comment Utility
I do not understand what you mean by the "3 months" part, but hopefully this helps:

To delete the oldest 500 records older than 60 days (identified by DateTime column myTable.recordDateTime) in a table called myTable:

delete top 500
from myTable
where myTable.recordDateTime >= dateadd(day, -60, getdate())
order by myTable.recordDateTime desc

Open in new window

0
 

Author Comment

by:enrique_aeo
Comment Utility
THE table is very large, that is why you want to delete in groups of three months and within those three months of 500 in 500
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
this previously answered question (PAQ) may be of assistance - it discusse using batches.
0
 
LVL 11

Assisted Solution

by:Louis01
Louis01 earned 167 total points
Comment Utility
If you have an index on myTable.recordDateTime (in my sample above), limiting to to 3 month blocks is not going to make any difference.

By default a TABLOCK (Full table lock) is used. For specifying different locking, see this link for a locking operation that would suit your needs.

For instance:
delete top 500
from myTable
WITH (ROWLOCK) 
where myTable.recordDateTime >= dateadd(day, -60, getdate())
order by myTable.recordDateTime desc 

Open in new window


A note about ROWLOCK though:
The with (rowlock) is a hint that instructs the database that it should keep locks on a row scope. That means that the database will avoid escalating locks to block or table scope.

You use the hint when only a single or only a few rows will be affected by the query, to keep the lock from locking rows that will not be deleted by the query. That will let another query read unrelated rows at the same time instead of having to wait for the delete to complete.

If you use it on a query that will delete a lot of rows, it may degrade the performance as the database will try to avoid escalating the locks to a larger scope, even if it would have been more efficient.

You need to test this though, the ROWLOCK is just a hint and not a guarantee
0
 
LVL 11

Expert Comment

by:Louis01
Comment Utility
0
 

Author Comment

by:enrique_aeo
Comment Utility
Please,

Anyway I need your script support

The range of debugging for the deletion of records is 3 months. Within these three months should eliminate blocks of 500 by 500.
0
 
LVL 9

Expert Comment

by:sarabhai
Comment Utility
Please show the table structure,

use  WITH (ROWLOCK) for delete statement.
And using temp table add all ids that going to delete from the table and then delete it from table one by one using loop.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:enrique_aeo
Comment Utility
This is the table
--Create table and its columns
CREATE TABLE [dbo].[pruebas_rln] (
      [id] [int] NOT NULL IDENTITY (1, 1),
      [sesion] [varchar](34) NULL,
      [pais] [varchar](2) NULL,
      [tienda] [varchar](10) NULL,
      [kiosco] [varchar](10) NULL,
      [tipoConsulta] [varchar](1) NULL,
      [tipoProducto] [varchar](20) NULL,
      [tipoTarjeta] [varchar](20) NULL,
      [numTarjeta] [varchar](20) NULL,
      [tipoDocumento] [varchar](1) NULL,
      [numDocumento] [varchar](20) NULL,
      [codigoPantalla] [varchar](20) NULL,
      [tiempoVisita] [int] NULL,
      [tiempoRespuesta] [int] NULL,
      [impresionRealizada] [varchar](1) NULL,
      [fechaRegistro] [datetime] NULL);
0
 

Author Comment

by:enrique_aeo
Comment Utility
I attached some data
script-depuracion---Copy.txt
0
 

Author Comment

by:enrique_aeo
Comment Utility
Please experts, you support is very important for me
0
 
LVL 38

Assisted Solution

by:Jim P.
Jim P. earned 333 total points
Comment Utility
Try this code:

declare @DeleteDate as datetime
select @DeleteDate = cast( convert(varchar(10),dateadd(days, -60, getdate()),101) as datetime) -- this gets you 60 days ago at midnight

while (select count(1) from [dbo].[pruebas_rln] where fechaRegistro <  @DeleteDate) >0
begin 
     delete top (500)
     from   [dbo].[pruebas_rln]
     fechaRegistro >  @DeleteDate
end 

Open in new window


It will delete the records 500 at a time that are older than 60 days.

NOTE: This is aircode, but I have done similar in the past.
0
 

Author Comment

by:enrique_aeo
Comment Utility
Hi experts

I test but too long

declare @contador int = 0
declare @DeleteDate as datetime
select @DeleteDate = cast( convert(varchar(10),dateadd(day, -60, getdate()),101) as datetime) -- this gets you 60 days ago at midnight
--print @DeleteDate

while (select count(1) from [dbo].[pruebas_rln] where fechaRegistro <  @DeleteDate) >0
begin
     delete top (50)
     from   [dbo].[pruebas_rln]
     where fechaRegistro >  @DeleteDate;

       --@contador = @contador +1;
       print @contador
end

Hello experts, I tried it but the query takes more than five minutes, I'm not sure but I think it has entered an infinite loop, please support
0
 
LVL 38

Expert Comment

by:Jim P.
Comment Utility
How many record are there if you

declare @DeleteDate as datetime
select @DeleteDate = cast( convert(varchar(10),dateadd(day, -60, getdate()),101) as datetime) -- this gets you 60 days ago at midnight
--print @DeleteDate

select count(1) from [dbo].[pruebas_rln] where fechaRegistro <  @DeleteDate

Open in new window

0
 

Author Comment

by:enrique_aeo
Comment Utility
for my test I have 304 rows
0
 
LVL 38

Accepted Solution

by:
Jim P. earned 333 total points
Comment Utility
Is your test system significantly different from your production system?

Is the table doing cascade deletes to sub-tables?

This is a case where we can't see your systems, databases, and your setups. In addition, my mind reading license expired about ten years ago and I haven't been able to get it renewed.

So if you run the code below:

declare @DeleteDate as datetime
declare @StopDate as datetime
select @StopDate = cast( convert(varchar(10),dateadd(days, -60, getdate()),101) as datetime) -- this gets you 60 days ago at midnight

select @DeleteDate = cast( convert(varchar(10),dateadd(days, -120, getdate()),101) as datetime) -- this gets you 120 days ago at midnight

while   @DeleteDate< @StopDate 
begin 
     delete top (500)
     from   [dbo].[pruebas_rln]
     fechaRegistro  = @DeleteDate

-- increment @DeleteDate by 1 day
    select @DeleteDate = dateadd(day,1,@DeleteDate)
     print @DeleteDate
end 

Open in new window


But if you are doing cascade deletes, it will take time. If you are running on a crap test system it will take time. If there is an error, it will eventually respond. If the fechaRegistro  is encoded as UTC and the rest of the system is set to EST, it has to calculate it.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

771 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

11 Experts available now in Live!

Get 1:1 Help Now