views:

1285

answers:

3

Situation:

  • table TBL has ~10k entries for deletion
  • table TBL has 14 child tables with delete rule "no action"
  • I want to delete 10k entries and referenced entries in child tables

Procedure:

  • delete records in child tables
  • disable constraints (if constraints are not disabled deletion in next step takes forever)
  • delete records in TBL table
  • enable constraints

Is there more elegant way for doing this?

[edit] Problem is that third step takes too long because it is checking 14 big tables for existence of non existent records. My procedure has good execution time but I think that there is more elegant way.

A: 

Couldn't you change the constraint so that on a delete in the parent table, records in the child table are also deleted?

matt b
One of the child tables is very big and partitioned. On that table I do some kind of "controlled" deletion :)I made some problems to my DB admin when I tried to cascade deletion.
Chobicus
On delete cascade is so very dangerous. Sometimes you WANT to know that there are children and have the delete FAIL, not just purge copious quantities of data to avoid an error.
@Mark Brady, I understand, just wanted to check to make sure the simple answer wouldn't work for his situation :)
matt b
+1  A: 

Can you create a trigger?

Sergio
+2  A: 

It sounds like you need to index your foreign keys on your child tables. Every time you delete a parent record without an index on the child table it must do a full table scan of the child to check if the foreign key constraint was broken.

With the index, it is at worst case an index range scan.

Edit: More info, and a script to determine if this is your problem can be found here. http://asktom.oracle.com/tkyte/unindex/index.html

Daniel Emge
It's a good hint and AskTom link (I voted up) but child tables are indexed on this FK.
Chobicus
My main concern with your approach would be if another session inserted bad data into the child tables while the constraints were down. If that is not possible, then you should be ok.
Daniel Emge