NOT IN Vs. NOT EXISTS [message #338974] |
Wed, 06 August 2008 06:26 |
Olivia
Messages: 519 Registered: June 2008
|
Senior Member |
|
|
I have found the same explain plan using NOT IN and NOT EXISTS.
Want to know which one is feasible?Do we need to avoid NOT IN? why?
Thanks in advance,
Oli
SET AUTOTRACE TRACEONLY
select i.item_cd from item i where i.item_cd NOT IN (SELECT d.item_cd
FROM item_activity d
WHERE i.item_cd = d.item_cd
and d.status_cd = 'CL'
);
112479 rows selected.
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer Mode=CHOOSE (Cost=2770806 Card=691 K Bytes=4 M)
1 0 FILTER
2 1 INDEX FAST FULL SCAN DB0DAFX1.XPKITEM (Cost=3326 Card=691 K Bytes=4 M)
3 1 INDEX RANGE SCAN DB0DAFX1.Xitem_cd_TMSCD (Cost=4 Card=1 Bytes=10)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
397230 consistent gets
0 physical reads
0 redo size
0 PX remote messages sent
0 PX remote messages recv'd
0 buffer is pinned count
0 workarea memory allocated
4 workarea executions - optimal
112479 rows processed
112479 rows selected.
SET AUTOTRACE TRACEONLY
select i.item_cd from item i where NOT EXISTS (SELECT 'X'
FROM item_activity d
WHERE i.item_cd = d.item_cd
and d.status_cd = 'CL'
);
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer Mode=CHOOSE (Cost=2770806 Card=691 K Bytes=4 M)
1 0 FILTER
2 1 INDEX FAST FULL SCAN DB0DAFX1.XPKITEM (Cost=3326 Card=691 K Bytes=4 M)
3 1 INDEX RANGE SCAN DB0DAFX1.Xitem_cd_TMSCD (Cost=4 Card=1 Bytes=10)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
397230 consistent gets
0 physical reads
0 redo size
0 PX remote messages sent
0 PX remote messages recv'd
0 buffer is pinned count
0 workarea memory allocated
4 workarea executions - optimal
112479 rows processed
112479 rows selected.
[Updated on: Wed, 06 August 2008 06:27] Report message to a moderator
|
|
|
|
Re: NOT IN Vs. NOT EXISTS [message #338988 is a reply to message #338985] |
Wed, 06 August 2008 07:00 |
Olivia
Messages: 519 Registered: June 2008
|
Senior Member |
|
|
Michel Cadot wrote on Wed, 06 August 2008 06:56 | Oracle is smart to convert from one to the other one that is more efficient if it is equivalent.
So it is normal you get the same plan.
Regards
Michel
|
That means if the query is using NOT IN and NOT EXISTS also gives
the same performance then there is no need to replace NOT IN by NOT EXISTS?
Regards,
Oli
|
|
|
|
|
Re: NOT IN Vs. NOT EXISTS [message #339091 is a reply to message #338974] |
Wed, 06 August 2008 13:51 |
coleing
Messages: 213 Registered: February 2008
|
Senior Member |
|
|
Also, an outer join is also a good option to not in and not exists. (it can be quicker as well converting FILTER conditions to hash joins). Oracle will sometimes convert "not in" to hash joins automatically, but an outer join would halp oracle make an easier decision.
[Updated on: Wed, 06 August 2008 13:53] Report message to a moderator
|
|
|
|
|
|
Re: NOT IN Vs. NOT EXISTS [message #343124 is a reply to message #343121] |
Tue, 26 August 2008 06:12 |
Olivia
Messages: 519 Registered: June 2008
|
Senior Member |
|
|
Michel Cadot wrote on Tue, 26 August 2008 06:01 | This is why I posted "when they are equivalent" in bold.
Regards
Michel
|
Great! You hardly put a hole in your answer..
Thanks to you but I will give this credit to Kelvin to make me understand for giving his valuable time.
Regards,
Oli
|
|
|