Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > Sybase > How to cleanup a huge table?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-08-08, 14:28
anitaarbabi anitaarbabi is offline
Registered User
 
Join Date: Jun 2008
Posts: 4
How to cleanup a huge table?

Hi,
I'm trying to trim down a huge table in sybase database. We have around 25 million rows in the table from 1997. We want to keep the most recent three years and delete the rest. The thing is we cannot shut down the database and it should remain functional during the process.

I tried doing a DELETE FROM TABLE WHERE DATE < 'date'. I had to cancel the query due to it taking too long and nearly taking down the server.

pleeeease heeeelp.

Thanks in advance.
Anita
Reply With Quote
  #2 (permalink)  
Old 06-08-08, 15:15
iinfi iinfi is offline
Registered User
 
Join Date: May 2006
Posts: 42
you will need to run it in parts.
like delete records frm 1997 to 1998 tonite.
further 1998-99 tomorrow nite.
likewise
Reply With Quote
  #3 (permalink)  
Old 06-08-08, 16:12
mike_bike_kite mike_bike_kite is offline
Registered User
 
Join Date: Jun 2007
Location: London
Posts: 944
Would this do it for you. I imagine it would take a day to finish but you could alter the 20000 value to suit your needs. At least this way you wouldn't need to baby sit the process. It'll just delete 20k records then sleep for a minute until finished. I don't think it would hold any locks.

Code:
declare @cnt int set rowcount 20000 select @cnt = 0 while @@rowcount > 0 begin select "Done", @cnt select @cnt = @cnt + 20000 wait for delay '00:01' delete TABLE where DATE < 'your cut off date' end set rowcount 0

Mike
Reply With Quote
  #4 (permalink)  
Old 06-10-08, 16:44
corral corral is offline
Registered User
 
Join Date: Aug 2002
Location: Madrid, Spain
Posts: 83
Assuming a brief period with the table unavailable, you may consider this. It's a variation of the usual way of bcp'ing out the data to be saved, truncate the table, drop indexes, bcp in only the good data and recreate the indexes.
- Build a similar table, loaded with the data you wish to keep
- Ban access to the table
- Use sp_rename to swap table names. Now, the good table is the one you built with fewer data
- Copy the latest inserts, those arrived after you built the table
- Grant access again

A warning about sp_rename: views, stored procedures and triggers will still refer to the old table, so they need to be droped and created if contain references to the table.

Regards,
Mariano Corral

Quote:
Originally Posted by anitaarbabi
Hi,
I'm trying to trim down a huge table in sybase database. We have around 25 million rows in the table from 1997. We want to keep the most recent three years and delete the rest. The thing is we cannot shut down the database and it should remain functional during the process.
Reply With Quote
  #5 (permalink)  
Old 06-10-08, 23:16
anitaarbabi anitaarbabi is offline
Registered User
 
Join Date: Jun 2008
Posts: 4
Hi Mike,

I don't get what your code does. Could you please explain a little more?

Anita
Reply With Quote
  #6 (permalink)  
Old 06-11-08, 04:11
aflorin27 aflorin27 is offline
Registered User
 
Join Date: Apr 2008
Location: Iasi, Romania
Posts: 66
The key statement in his code is "set rowcount":

set rowcount:
causes Adaptive Server to stop processing the query (select, insert, update, or delete) after the specified number of rows are affected. The number can be a numeric literal with no decimal point or a local variable of type integer. To turn this option off, use:
set rowcount 0

The code above deletes all your useless records, not in a single run, but in "batches" of 20.000 records.
I have to say it is a smart choice
Reply With Quote
  #7 (permalink)  
Old 06-11-08, 04:33
mike_bike_kite mike_bike_kite is offline
Registered User
 
Join Date: Jun 2007
Location: London
Posts: 944
Quote:
I don't get what your code does. Could you please explain a little more?
As explained above, set rowcount x just limits the number of rows affected by any sql. The little program just loops round and round deleting any data before your cut off date but limiting itself to only deleting 20k rows at a time. This means there will still be data before this date in the table when it loop round again. It will then wait for a minute to allow any other programs to do their stuff. It also shows a rough rowcount to show the progress. It will stop when there aren't any records left to delete before your cut off date.

Mike

PS1 You can change the 20k value and the wait value to make things faster or slower.
PS2 You should dump the database before doing any major change like this.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On