Oracle SQL Help: "not in" clause
Hi,
I need to speed up a query that takes upwards of 2 minutes to execute on a
table that currently has 14000 records, but will soon have 2+ million
records. If it takes 2 minutes to execute on 14000 records, I don't want to
see what it will be like on 2 million records! I think it is taking long
because of a "not in" clause, and I know I should try to use "not exists"
instead, but my first attempts at doing that were incorrect.
Here is the basic table structure:
MY_TABLE_ID ORDER_ITEM_ID RETURN_ID LINKING_ID
1 1
2 1 90
3 1 90
4 1
5 1
6 1
7 1 90 1
The LINKING_ID is a foreign key to MY_TABLE_ID; can be NULL.
Here is my SQL:
select * from MY_TABLE
where ORDER_ITEM_ID = 1
and RETURN_ID is null
and MY_TABLE_ID not in
(select distinct nvl(LINKING_ID,0)
from MY_TABLE where ORDER_ITEM_ID = 1
and RETURN_ID is not null)
I want those records for a particular item where the RETURN_ID is null and
there are no LINKING_ID records pointing to it. For the above sample
records, the result should be records 4,5 and 6.
Any help is appreciated.
Thanks,
Natalie