If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Deleting all records in many tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-23-06, 04:11
VoodooEx VoodooEx is offline
Registered User
 
Join Date: May 2005
Posts: 4
Deleting all records in many tables

Hi everyone,

This is my first time posting. I am quite new to DB2. My company is using DB2/UDB v8.1.6 on a Win2k3 Box to run J.D. Edwards application. I am the unofficial DB2 administrator in the company.

I have a question which I hope someone can help me with. It's regarding deleting a whole bunch of tables in a script files.

To help me speed up the deletion, I know I have to issue a command:

ALTER TABLE table1 ACTIVATE NOT LOGGED INITIALLY WITH EMPTY TABLE;

to reduce the logging overhead.

My question is that, in my script. If I have 100 tables, do I need to run the ALTER TABLE command for the 100 tables 100 times?

E.g. Script1.sql:

ALTER TABLE table1 ACTIVATE NOT LOGGED INITIALLY WITH EMPTY TABLE;
DELETE FROM table1;
ALTER TABLE table2 ACTIVATE NOT LOGGED INITIALLY WITH EMPTY TABLE;
DELETE FROM table2;
ALTER TABLE table3 ACTIVATE NOT LOGGED INITIALLY WITH EMPTY TABLE;
DELETE FROM table3;
.
.
.
ALTER TABLE table100 ACTIVATE NOT LOGGED INITIALLY WITH EMPTY TABLE;
DELETE FROM table100;

COMMIT;

Many thanks if anyone could provide some insights.
Reply With Quote
  #2 (permalink)  
Old 05-23-06, 04:21
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,279
Issue 100 IMPORT REPLACE or LOAD REPLACE statements with an empty data file. The filled tables will all be replaced with the data of the empty file, resulting in empty tables.

db2 import from emptyfile.del of del replace into table1
db2 import from emptyfile.del of del replace into table2
....
db2 import from emptyfile.del of del replace into table100
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages
Reply With Quote
  #3 (permalink)  
Old 05-23-06, 04:25
VoodooEx VoodooEx is offline
Registered User
 
Join Date: May 2005
Posts: 4
Hi Wim,

Thanks for the suggestion.

I am just wondering whether this will take long to run? Reason I'm asking is because some of the tables I'm trying to delete contains millions of records.
Reply With Quote
  #4 (permalink)  
Old 05-23-06, 06:53
JAYANTA_DATTA JAYANTA_DATTA is offline
Registered User
 
Join Date: Oct 2004
Location: DELHI INDIA
Posts: 336
The best way to delete such huge table is to IMPORT blank files into those tables as Wim suggested.
We have tried this for tables with more than 50 Million records and it got deleted in few seconds.

Create a blank file blank.csv and run the following:

db2 "import from blank.csv of del replace into tb_tabl1"

Thanks,
Jay
Reply With Quote
  #5 (permalink)  
Old 05-23-06, 11:16
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Well, actually the option you specify, WITH EMPTY TABLE, should delete all data in the table, so you don't need to do the DELETE part.
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

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