Home » SQL & PL/SQL » SQL & PL/SQL » Alternative to NOT IN (Oracle 11g)
Alternative to NOT IN [message #595777] Fri, 13 September 2013 09:03 Go to next message
developer12
Messages: 88
Registered: July 2013
Member
Hi,
Can anyone suggest an alternative to NOT IN Oracle?
I have Table A which has 12 million records and I want to match them with records in Table B by joining on a column- ID.
If the record in Table B is missing, I want to report the column ID from Table A for which there is no record in Table B.
Now, a NOT IN clause is consuming huge overhead for the query to run, so is there any other alternatives to that??
Re: Alternative to NOT IN [message #595779 is a reply to message #595777] Fri, 13 September 2013 09:04 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
MINUS
Re: Alternative to NOT IN [message #595781 is a reply to message #595779] Fri, 13 September 2013 09:11 Go to previous messageGo to next message
developer12
Messages: 88
Registered: July 2013
Member
Thanks BlackSwan
Re: Alternative to NOT IN [message #595782 is a reply to message #595779] Fri, 13 September 2013 09:12 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
Couldn't you just outer join it with xxx is null (assuming there's an available column with NOT NULL in play)?
Re: Alternative to NOT IN [message #595784 is a reply to message #595782] Fri, 13 September 2013 09:19 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
You could, but minus should be quicker (depending on the results expected)
Re: Alternative to NOT IN [message #595796 is a reply to message #595784] Fri, 13 September 2013 13:29 Go to previous messageGo to next message
Littlefoot
Messages: 21807
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
The most obvious alternative to NOT IN seems to be OUT.
Re: Alternative to NOT IN [message #595797 is a reply to message #595796] Fri, 13 September 2013 13:32 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
I guess it depends on how you read 'alternative'. It could also be ... IN
Re: Alternative to NOT IN [message #595799 is a reply to message #595797] Fri, 13 September 2013 13:42 Go to previous messageGo to next message
Littlefoot
Messages: 21807
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
/forum/fa/1964/0/
Re: Alternative to NOT IN [message #596071 is a reply to message #595799] Tue, 17 September 2013 15:14 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
select a.id
from tableb b, tablea a
where a.id = b.id(+)
where b.id is null;
Re: Alternative to NOT IN [message #596072 is a reply to message #596071] Tue, 17 September 2013 15:37 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Incorrect syntax Smile
Re: Alternative to NOT IN [message #596093 is a reply to message #596072] Wed, 18 September 2013 00:23 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
SQL> create table tablea (id int);

Table created.

SQL> create table tableb (id int);

Table created.

SQL> select a.id
  2  from tableb b, tablea a
  3  where a.id = b.id(+)
  4    and b.id is null;

no rows selected
Re: Alternative to NOT IN [message #596112 is a reply to message #596093] Wed, 18 September 2013 02:05 Go to previous messageGo to next message
msol25
Messages: 396
Registered: June 2011
Senior Member
Hi friends,

Only use of MINUS can be the best solution
icon6.gif  Re: Alternative to NOT IN [message #596113 is a reply to message #596112] Wed, 18 September 2013 02:13 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
But MINUS cannot be (efficiently) used when you want columns in one table that are not in the other one.
For instance, with the following tables:
create table tablea (id int, val int);
create table tableb (id int);

I want rows from tablea with id not in tableb.
MINUS can only return you ID, and VAL if you use it as a subquery:
select * from tablea
where id in (select id from tablea minus select id from tableb);

There OUTER JOIN is a direct alternative to NOT IN:
select a.* 
from tablea a, tableb b
where a.id = b.id(+)
  and b.id is null;


[Edit: english]

[Updated on: Wed, 18 September 2013 05:08]

Report message to a moderator

Re: Alternative to NOT IN [message #596120 is a reply to message #596112] Wed, 18 September 2013 02:55 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
msol25 wrote on Wed, 18 September 2013 08:05
Hi friends,

Only use of MINUS can be the best solution

Please define what you mean by 'best'
Re: Alternative to NOT IN [message #596225 is a reply to message #596120] Thu, 19 September 2013 03:04 Go to previous message
rleishman
Messages: 3728
Registered: October 2005
Location: Melbourne, Australia
Senior Member
As Michel points out, MINUS has its problems if you want some other values.
Similarly, outer joins may have performance problems if there are typically many TABLEB rows for one TABLEA row, because you bear the cost of joining all rows before filtering.

NOT IN can be made to run fast. Typically when NOT IN is slow, it is because the optimizer cannot rewrite it as an ANTI-JOIN. This is caused by comparison columns in the outer query or the sub-query being nullable. If you make them non-nullable, ANTI-JOIN comes into play and you get same or better performance as the outer join.

SELECT *
FROM TABLEA
WHERE ID IS NOT NULL
AND ID NOT IN (
     SELECT ID
     FROM   TABLEB
     WHERE  ID IS NOT NULL)


Ross Leishman
Previous Topic: Index and null values (merged 2)
Next Topic: Limitations for With data clause
Goto Forum:
  


Current Time: Fri Apr 19 19:40:45 CDT 2024