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 > Best way to do a huge delete on a table with RI

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-26-06, 12:44
JamesAvery22 JamesAvery22 is offline
Registered User
 
Join Date: Jan 2004
Posts: 70
Best way to do a huge delete on a table with RI

Is there any way I can speed this up?

Code:
while ((select count(*) from myschema.mytable where var = invar ) > 0 ) do
	delete
		from myschema.mytable
			where id_col in
			(select id_col
				from myschema.mytable
				where var = invar
				fetch first 250000 rows only
			);
	commit;
end while;

And what is the proper way to do this?

Code:
while ((select count(*) from myschema.mytable1 where var = invar ) > 0 ) do
	delete
		from myschema.mytable1
			where id_col in
			(select id_col
				from myschema.mytable2
				where var = invar
				fetch first 250000 rows only
			);
	commit;
end while;
I know this won't work if the total number of rows found by "select id_col from myschema.mytable2 where var = invar" is more than 250000 because the deletes are on mytable1 and the rows after 250000 will never be returned.
Reply With Quote
  #2 (permalink)  
Old 04-26-06, 12:46
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Export what you want to keep, drop the table, load the data back.
Reply With Quote
  #3 (permalink)  
Old 04-26-06, 12:49
JamesAvery22 JamesAvery22 is offline
Registered User
 
Join Date: Jan 2004
Posts: 70
Quote:
Originally Posted by n_i
Export what you want to keep, drop the table, load the data back.
Can't. Table is too large so we don't have any other places to put the data. Not sure if we are allowed to do drops either.
Reply With Quote
  #4 (permalink)  
Old 04-28-06, 02:48
przytula_guy przytula_guy is offline
Registered User
 
Join Date: Apr 2006
Location: Belgium
Posts: 1,159
delete

alter table ... notlogged initially and in the same luw - the delete..
__________________
Best Regards, Guy Przytula
Database Software Consultant
DB2 UDB LUW Certified V7-V8-V9-V9.7 DB Admin - Dprop..
Information Server Datastage Certified
http://www.infocura.be
Reply With Quote
  #5 (permalink)  
Old 05-02-06, 11:03
JamesAvery22 JamesAvery22 is offline
Registered User
 
Join Date: Jan 2004
Posts: 70
Quote:
Originally Posted by przytula_guy
alter table ... notlogged initially and in the same luw - the delete..
Im sorry but I have no idea what you just said


I know something is wrong with this but I forgot what...


Code:
while ((select count(*) from myschema.mytable1 where var = invar ) > 0 ) do
	delete
		from myschema.mytable1
			where id_col in
			(select id_col
				from myschema.mytable2
				where var = invar
				fetch first 250000 rows only
			);
	commit;
end while;
In a stored procedure can I put a parameter in for the commit count?
Reply With Quote
  #6 (permalink)  
Old 05-02-06, 12:54
przytula_guy przytula_guy is offline
Registered User
 
Join Date: Apr 2006
Location: Belgium
Posts: 1,159
delete

there is a command : alter table xxx notlogged initially..
followed by the delete (in the same luw)
this will NOT LOG the delete statement and not use log space.
It has to be executed in the same luw.
__________________
Best Regards, Guy Przytula
Database Software Consultant
DB2 UDB LUW Certified V7-V8-V9-V9.7 DB Admin - Dprop..
Information Server Datastage Certified
http://www.infocura.be
Reply With Quote
  #7 (permalink)  
Old 05-02-06, 12:55
JamesAvery22 JamesAvery22 is offline
Registered User
 
Join Date: Jan 2004
Posts: 70
Post

Code:
while ((select count(*) from myschema.mytable1 where var = invar ) > 0 ) do
	delete
		from myschema.mytable1
			where id_col in
			(select id_col
				from myschema.mytable1
				where id_col in
				(select id_col
					from myschema.mytable2
					where var = invar
				)
			) fetch first 250000 rows only
	commit;
end while;
Pretty sure the answer is this. still testing...
Reply With Quote
  #8 (permalink)  
Old 05-08-06, 03:50
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Quote:
Originally Posted by przytula_guy
there is a command : alter table xxx notlogged initially..
followed by the delete (in the same luw)
this will NOT LOG the delete statement and not use log space.
It has to be executed in the same luw.
1. Is loggin automaticaly turned on when commit is executed?
2. Is this setting only valid for current transaction or for all transactions executing at the time "alter table ... not logged ..." is executed?
3. How to put the table into normal state?
4. Can any other transactions run during luw of "alter table..." statement?

Thanks,
Grofaty

Last edited by grofaty; 05-08-06 at 04:00.
Reply With Quote
  #9 (permalink)  
Old 05-08-06, 06:49
przytula_guy przytula_guy is offline
Registered User
 
Join Date: Apr 2006
Location: Belgium
Posts: 1,159
alter

alter table .. is only valid for the current logical unit of work = luw
same as transaction : all statements are grouped in a transaction and committed at end or rollbacked
whenever commit is executed - the table returns into normal state = logging
only valid for statements in this luw=transaction
no alter table or any other command needed to reset this state, just commit or rollback
__________________
Best Regards, Guy Przytula
Database Software Consultant
DB2 UDB LUW Certified V7-V8-V9-V9.7 DB Admin - Dprop..
Information Server Datastage Certified
http://www.infocura.be
Reply With Quote
  #10 (permalink)  
Old 05-08-06, 08:16
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
If commit is executed everything works fine, but if rollback is executed then
'select * from table' returs error:
Code:
SQL1477N  Table "db2admin.table" cannot be accessed.  SQLSTATE=55019
The SQL help states:
Code:
SQL1477N Table "<table-name>" cannot be accessed.

Explanation:

An attempt was made to access a table that is not accessible.
The table may not be accessible because of one of the following
reasons:

o   The table had NOT LOGGED INITIALLY activated when the unit of
    work was rolled back.

o   The table is a partitioned declared temporary table and one
    or more partitions failed since the temporary table was
    declared (all declared temporary tables have the schema name
    SESSION).

o   ROLLFORWARD encountered the activation of NOT LOGGED
    INITIALLY on this table or a NONRECOVERABLE load on this
    table.



 Access to this table is not allowed because its integrity cannot
be guaranteed.

User Response:

 One of the following actions can be taken.

o   If the table had NOT LOGGED INITIALLY activated, drop the
    table.  If this table is required, re-create it.

o   If the table is a declared temporary table, drop the table.
    If this table is required, declare it again.

o   Otherwise, restore from a tablespace or database backup.  The
    backup image must have been taken subsequent to the commit
    point following the completion of the non-recoverable
    operation (NOT LOGGED INITIALLY operation, or NONRECOVERABLE
    load).



 sqlcode :  -1477

 sqlstate :  55019
So if rollback is executed the "drop table" and "create table" is required?

Thanks,
Grofaty
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