Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

DELTING HUGE No. of ROWS Expand / Collapse
Author
Message
Posted Friday, August 8, 2014 7:49 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Sunday, September 21, 2014 1:53 AM
Points: 205, Visits: 232
Dears ,

I have to delete data from HUGETABLE which has around 700 million rows prior to 1st May 2014. This table has no Index, however, it has an ID column.

Now in order to avoid the log growth, I wrote the below code. Assuming that it will delete 1 million rows and at any time I cancel it; it will rollback only the last transaction. But on the other hand when I cancel it; it starts rolling back all the rows back into the table.

Any idea, how can I achieve it without running out of LOG File Space ?
Next thing I have in my mind is to schedule a job to delete around a milllion rows every 10 minutes and truncate the log at the end of the job.

Appreciate experts feedback.

begin try drop table #counter end try begin catch end catch
create table #counter (id int )
insert into #counter SELECT top 1 ID From HUGETABLE with (nolock) where DateTime < '2014-05-01'


while (select COUNT (1) from #counter) > 0
begin
begin transaction deletemol
truncate table #counter

insert into #counter SELECT top 10000000 ID From HUGETABLE with (nolock) where DateTime < '2014-05-01'

if exists (select top 1 1 from #counter )
begin
delete from HUGETABLE where ID in (select ID from #counter)
end
truncate table #counter
insert into #counter
select top 1 ID From HUGETABLE with (nolock) where AuditDateTime < '2014-05-01'
select GETDATE ()

commit transaction deletemol

DBCC SHRINKFILE (N'DB_log' , 0, TRUNCATEONLY)

END

Post #1601179
Posted Friday, August 8, 2014 7:56 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 3:11 PM
Points: 20,734, Visits: 32,498
funooni (8/8/2014)
Dears ,

I have to delete data from HUGETABLE which has around 700 million rows prior to 1st May 2014. This table has no Index, however, it has an ID column.

Now in order to avoid the log growth, I wrote the below code. Assuming that it will delete 1 million rows and at any time I cancel it; it will rollback only the last transaction. But on the other hand when I cancel it; it starts rolling back all the rows back into the table.

Any idea, how can I achieve it without running out of LOG File Space ?
Next thing I have in my mind is to schedule a job to delete around a milllion rows every 10 minutes and truncate the log at the end of the job.

Appreciate experts feedback.

begin try drop table #counter end try begin catch end catch
create table #counter (id int )
insert into #counter SELECT top 1 ID From HUGETABLE with (nolock) where DateTime < '2014-05-01'


while (select COUNT (1) from #counter) > 0
begin
begin transaction deletemol
truncate table #counter

insert into #counter SELECT top 10000000 ID From HUGETABLE with (nolock) where DateTime < '2014-05-01'

if exists (select top 1 1 from #counter )
begin
delete from HUGETABLE where ID in (select ID from #counter)
end
truncate table #counter
insert into #counter
select top 1 ID From HUGETABLE with (nolock) where AuditDateTime < '2014-05-01'
select GETDATE ()

commit transaction deletemol

DBCC SHRINKFILE (N'DB_log' , 0, TRUNCATEONLY)

END



Looks like you need to delete 700 million rows of data, how many rows of data are you keeping?




Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1601185
Posted Friday, August 8, 2014 8:02 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Sunday, September 21, 2014 1:53 AM
Points: 205, Visits: 232
around 150 million rows will be kept i.e. last three months data.

I am thinking about taking those rows into another table and then truncating/dropping the table and renaming the actual table.

But the problem is that this table is used for insertions, so there is a chance of data loss
Post #1601193
Posted Friday, August 8, 2014 8:23 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Sunday, September 21, 2014 1:53 AM
Points: 205, Visits: 232
When I cancelled the Running Query, shouldn't the SQL Server rollback only the rows of last transaction. But it is rolling back the rows which are deleted and committed in earlier iterations.

Post #1601202
Posted Friday, August 8, 2014 8:48 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 3:11 PM
Points: 20,734, Visits: 32,498
There are other issues with your query, but lets not worry about that just yet. Another question, what edition of SQL Server are you running?

Also, is the intent to keep 3 months of data in the table going forward?

If so, is that 3 months plus the current month?



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1601216
Posted Friday, August 8, 2014 8:51 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Friday, October 17, 2014 6:53 AM
Points: 1,669, Visits: 2,219
If you cancel a query in SSMS, then the entire query's actions get rolled back, although I don't know what would happen if there were separate batches within what was executed. Just using a WHILE loop isn't going to get you past that problem.

Steve
(aka sgmunson)

Internet ATM Machine
Post #1601218
Posted Friday, August 8, 2014 9:16 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 3:38 PM
Points: 35,353, Visits: 31,893
I have to get to work but here's a 60,000 ft view of what I would do...

1. DO A FULL BACKUP!!!!!!!!!!!!
2. MAKE SURE POINT-IN-TIME BACKUPS ARE BEING TAKEN111!
3. Create an empty identical table.
4. Do a cascaded-rename (takes about 63ms) to rename the old table to something else and rename the new table to what the old table was named. This new table will continue in the place of the old table.
5. Insert the desired old rows from the old table into the new table using a WHILE loop that does it in 3 million row batchs (most systems will reach a tipping point shortly after that so I wouldn't go much higher if at all).
6. When you're absolutely sure that everything worked correctly, DROP the old table.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1601228
Posted Friday, August 8, 2014 9:22 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Sunday, September 21, 2014 1:53 AM
Points: 205, Visits: 232
Ahan , so that's why it did not go the way I thought. SSMS entire query is getting rolled back
Post #1601230
Posted Friday, August 8, 2014 9:26 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Sunday, September 21, 2014 1:53 AM
Points: 205, Visits: 232
Jeff Moden (8/8/2014)
I have to get to work but here's a 60,000 ft view of what I would do...

1. DO A FULL BACKUP!!!!!!!!!!!!
2. MAKE SURE POINT-IN-TIME BACKUPS ARE BEING TAKEN111!
3. Create an empty identical table.
4. Do a cascaded-rename (takes about 63ms) to rename the old table to something else and rename the new table to what the old table was named. This new table will continue in the place of the old table.
5. Insert the desired old rows from the old table into the new table using a WHILE loop that does it in 3 million row batchs (most systems will reach a tipping point shortly after that so I wouldn't go much higher if at all).
6. When you're absolutely sure that everything worked correctly, DROP the old table.


Thanks Jeff

Backup is done and point in time backups are being taken

I will follow your suggestion. I believe 63 ms would mean a very slight downtime might be faced by some trigger or end user application.
By the way the table is hit by a trigger on another table so the ultimate end user might not face anything.

Any further suggestion about the downtime ? I mean what should I be expecting during that 63 ms ?

Thanks again.
Post #1601232
Posted Friday, August 8, 2014 9:28 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Sunday, September 21, 2014 1:53 AM
Points: 205, Visits: 232
Lynn Pettis (8/8/2014)
There are other issues with your query, but lets not worry about that just yet. Another question, what edition of SQL Server are you running?

Also, is the intent to keep 3 months of data in the table going forward?

If so, is that 3 months plus the current month?


Yes its the current month and last three months i.e. May, June, July and August.

I am using SQL Server 2008 R2
Post #1601233
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse