Question

Consider this table:

Table: ORDER [Columns: ID (PK), Order_Num, etc...]
Child Tables: ORDER_ITEMS, ORDER_TRANSACTIONS, ORDER_PAYMENTS, ORDER_TRANSPORT, and many more etc.

All the Child Tables have Foreign Keys pointing to ORDER Table Primary Key. And there is huge amount of data.

Now, I have to Purge some Orders. Although I delete records in correct Order i.e Child Table first and then Order Table - It Takes a Very Very Huge time(15 minutes) for this particular Query to run (for a single Order ID):

DELETE FROM ORDER WHERE ID='whatever'

The DBA Says this is probably because of Child Table dependecies - (FK check), and the child tables contains huge records themselves- hence the time taken (even though all dependencies will be ultimately satisfied - as child records were deleted explicitly upfront).

What are my options:

  1. Can I disable FK relationships in Child Tables before executing DELETE on main ORDER Table? How can I easily do this.
  2. Any other way to tell Oracle to not check for dependencies?
    Or any other ideas?
Was it helpful?

Solution

This is most likely because you have unindexed foreign key.

You have deleted the children first, but some other transactions may have inserted data in the mean time, so Oracle has to check again when you delete from a parent table.

If a foreign key is not paired with an index, Oracle has to get a lock on the whole child table to make sure that no other concurrent uncommited transaction has inserted a potentially orphan child.

This lock can take a lot of time to acquire, even on small busy tables.

Adding indexes solves this problem.

OTHER TIPS

How to make index unusable and then rebuilt in index

This link should help you as far as I know making indexes unusable, stops oracle to check constraints. So it may help you. Make your indexes unusable, delete records, then rebuilt indexes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top