ctyankeeinok
MIS
I want to know how to most efficiently do the following query:
I need to remove entries in a table if there are no matching entries in two other tables, as well as a recursive look at another column in the same table. What I currently have is this (simplified):
delete from table_a
where table_a.key in
(
select ta.key
from table_a ta,
ref_table rt
where ta.key2 = rt.key2
and rt.end_date < sysdate
and not exists (select tb.key
from table_b tb
where tb.key = ta.key)
and not exists (select tc.key
from table_c tc
where tc.key = ta.key)
and not exists (select ta1.key
from table_a ta1
where ta1.col_fk = ta.key)
)
As you can see I am referencing table_a three times (which I am sure does not help things). This query takes over 2 minutes on about 20K rows in table_a and I have tried various incarnations to no avail. What is the most efficient way to accomplish this objective? I have tried not exists, not in, and 0 = select count.... with no improved performance. Is there a better way to do this?
I need to remove entries in a table if there are no matching entries in two other tables, as well as a recursive look at another column in the same table. What I currently have is this (simplified):
delete from table_a
where table_a.key in
(
select ta.key
from table_a ta,
ref_table rt
where ta.key2 = rt.key2
and rt.end_date < sysdate
and not exists (select tb.key
from table_b tb
where tb.key = ta.key)
and not exists (select tc.key
from table_c tc
where tc.key = ta.key)
and not exists (select ta1.key
from table_a ta1
where ta1.col_fk = ta.key)
)
As you can see I am referencing table_a three times (which I am sure does not help things). This query takes over 2 minutes on about 20K rows in table_a and I have tried various incarnations to no avail. What is the most efficient way to accomplish this objective? I have tried not exists, not in, and 0 = select count.... with no improved performance. Is there a better way to do this?