No difference in plan and cost for IN and EXISTS [message #392997] |
Thu, 19 March 2009 23:27 |
mbmalasenthil
Messages: 27 Registered: July 2008
|
Junior Member |
|
|
Hi,
I have a query as shown below:
select SUM(DECODE(a.year, 2003, a.month, 0)),
a.FID,
a.OID,
a.GID,
a.CID,
a.PRODUCT_ID,
a.TID,
a.CURR
from table1 a
where a.CID = 500
and a.OID IN
(SELECT leaf_node
FROM table2
WHERE table2.id = 101069
and (table2.node02 = 7009 or
table2.node01 = -99000 or
table2.node01 = 7011))
and a.year IN (2003)
group by a.FID,
a.OID,
a.GID,
a.CID,
a.PRODUCT_ID,
a.TID,
a.CURR
I replaced the IN clause with EXISTS and saw the execution plan. I dont see any change in the plan for both IN and EXISTS and also no change in cost/time. What does it mean? I read in many places that using EXISTS is a better style of programming that IN. Please suggest.
Thank you,
Senthil
|
|
|
|
|
|
|
Re: No difference in plan and cost for IN and EXISTS [message #393247 is a reply to message #393004] |
Fri, 20 March 2009 16:32 |
rleishman
Messages: 3728 Registered: October 2005 Location: Melbourne, Australia
|
Senior Member |
|
|
Oracle's optimizer can rewrite IN queries so that they execute as EXISTS and vice-versa.
What's happening here is that Oracle thinks one of them is better. When you try the other one, it rewrites the query internally.
If you suppress the REWRITE capability, you will be able to try out the other plan and see if it is faster. Just use the NO_REWRITE hint.
SELECT /*+NO_REWRITE*/ ...
Ross Leishman
|
|
|
|