Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

best way to obtain records that are NOT in another table

userLynxFeb 3 2011 — edited Aug 15 2011
I have two rather large tables in oracle. An Account table that has millions of rows. Each account may be enrolled into a particular program and therefore can also be in an Enrollment table, also with millions of rows. I'm trying to find the most optimal way to find any accounts in ACCOUNT that are NOT in the Enrollment table.

I was doing something like this:

select /*+ index(ACCOUNT idx_acct_no) */
a.acct_no
from ACCOUNT a
where a.acct_no not in (Select e.acct_no from ENROLLMENT e);


This takes a VERY long time to execute, even though I am using the index.

I even tried to use the PK on the ACCOUNT table, as it is also a FK on the ENROLLMENT table as such:

select a.acct_no
from ACCOUNT a
where a.id not in (Select e.id from ENROLLMENT e);

this too takes too long to get back (if at all).

Is there a better way to do this selection please?
This post has been answered by LKBrwn_DBA on Feb 3 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 12 2011
Added on Feb 3 2011
12 comments
17,347 views