Solved

sql server 2005 - eliminate blocks of 500 by 500.

Posted on 2013-06-20
17
386 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
[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
  • 7
  • 3
  • 3
  • +2
17 Comments
 
LVL 11

Expert Comment

by:Louis01
ID: 39261929
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
ID: 39261937
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
ID: 39261987
this previously answered question (PAQ) may be of assistance - it discusse using batches.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 11

Assisted Solution

by:Louis01
Louis01 earned 167 total points
ID: 39261988
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
ID: 39261989
0
 

Author Comment

by:enrique_aeo
ID: 39262025
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
ID: 39262027
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
 

Author Comment

by:enrique_aeo
ID: 39272740
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
ID: 39272753
I attached some data
script-depuracion---Copy.txt
0
 

Author Comment

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

Assisted Solution

by:Jim P.
Jim P. earned 333 total points
ID: 39286763
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
ID: 39288421
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.
ID: 39288673
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
ID: 39295326
for my test I have 304 rows
0
 
LVL 38

Accepted Solution

by:
Jim P. earned 333 total points
ID: 39295354
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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Throw Error 7 35
Need help with a query 14 39
Need SSIS project 2 28
SQL Query Across Multiple Tables - Help 5 21
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.​
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.

749 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