GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Interview Questions  >  Oracle  >  PL/SQL
Go To First  |  Previous Question  |  Next Question 
 PL/SQL  |  Question 185 of 237    Print  
Compare EXISTS and IN Usage with advantages and disadvantages.

  
Total Answers and Comments: 9 Last Update: July 14, 2008     Asked by: Sachin Gadakri 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: be_viral
 
We should use Exists or In based on the no of records inner and outer query is expected to return.Using Exists : Here the outer query is executed first and then inner query is excecuted for each value of outer query. So use this when outer query contains less records.Using IN : Here the inner query is executed first and then outer query is excecuted for each value of inner query. So use this when inner query contains less records.

Above answer was rated as good by the following members:
ratna82
June 12, 2007 16:43:26   #1  
krishna sai        

RE: Compare EXISTS and IN Usage with advantages and di...
exist is faster than IN Command

exist do full table scan...so it is faster than IN

 
Is this answer useful? Yes | No
July 25, 2007 13:39:47   #2  
Ranjith A Ramachandran        

RE: Compare EXISTS and IN Usage with advantages and di...
Use Exists whenever possible. EXISTS only checks the existence of records (True/False), and in the case of IN each and every records will be checked.  performace wise EXISTS is better.
 
Is this answer useful? Yes | No
November 26, 2007 01:08:53   #3  
Donram        

RE: Compare EXISTS and IN Usage with advantages and di...
If your inner query returns a few rows to the outer query, use IN otherwise use EXISTS .

In -- Full table Scan
Exists -- Existance of rows

 
Is this answer useful? Yes | No
February 12, 2008 07:37:25   #4  
mohit_aren Member Since: February 2008   Contribution: 2    

RE: Compare EXISTS and IN Usage with advantages and disadvantages.
IN is used in normal SELECT queries:

e.g.
SELECT * FROM EMP WHERE EMPID IN (12,13,15)

We cannot use EXIST to fetch the rows with 12,13,15 EMP IDs

EXIST only tells if a SELECT query will fetch any row - true, no rows - false

e.g.
IF EXISTS(SELECT * FROM EMP WHERE EMPID IN (12,13,15))
     DELETE FROM EMP WHERE EMPID IN (12,13,15)
this will fire the DELETE statement only once as next time there will be no rows with these EMPIDs

thus there is basic difference in both

 
Is this answer useful? Yes | No
April 30, 2008 12:17:29   #5  
jryan999 Member Since: April 2008   Contribution: 15    

RE: Compare EXISTS and IN Usage with advantages and disadvantages.
Firstly, in common with Tom Kyte, I could scream hearing comments that IN is "better" or faster than exists.  Nonsense.

There are situations where IN is much faster (and vice versa).

eg.  select  *
       from    MASSIVE_TABLE T
       where T.indexed_field in (select result
                                                from   VERY_SMALL_RESULT_SET)

Using IN, if resulting in the index on MASSIVE_TABLE being used be MUCH faster than an exists.

For a full comparison see Tom Kytes explaination at:-
http://asktom.oracle.com/pls/asktom/f?p=100:11:2957834641539512::::P11_QUESTION_ID:953229842074

From memory, here are some differences:-

1.  IN a subquery - must retrieve the entire query set before evaluating the outer query

2.  EXISTS on the other hand will stop executing a sub-query when the first entry is found.  This may in some cases favour EXISTS over IN

3.  "Where EXISTS (select null from..." clearly cannot use any index on outer query table whereas IN can.  (See explaination above)

In short, there are cases where IN is better than EXISTS (it can mean an index on the outer query may be used).  It depends upon the relative size of the "inner" and "outer" tables, and indexes in place.


 
Is this answer useful? Yes | No
May 03, 2008 05:39:47   #6  
krishnaindia2007 Member Since: September 2007   Contribution: 870    

RE: Compare EXISTS and IN Usage with advantages and disadvantages.

IN or EXISTS which gives better performance depends upon the situation.

 

While using IN , optimizer runs subquery first and then joins it with main dataset. Table in the subqery is small and table in the main query is large then IN usaully makes sense.

 

Using EXISTS, optimizer runs main query first and then applies dataset to the subquery. If the main table size is small and subquery table size is large then EXISTS usually makes sense.


 
Is this answer useful? Yes | No
May 08, 2008 07:26:59   #7  
krishnaindia2007 Member Since: September 2007   Contribution: 870    

RE: Compare EXISTS and IN Usage with advantages and disadvantages.

IN or EXISTS which gives better performance depends upon the situation.


Select * from T1 where x in ( select y from T2 )

is typically processed as:

select * 
from t1, ( select distinct y from t2 ) t2
where t1.x = t2.y;

The subquery is evaluated, distinct'ed, indexed (or hashed or sorted) and then joined to
the original table -- typically.


While using IN , optimizer runs subquery first and then joins it with main dataset. Table in the subqery is small and table in the main query is large then IN usaully makes sense.


select * from t1 where exists ( select null from t2 where y = x )

That is processed more like:


   for x in ( select * from t1 )
   loop
      if ( exists ( select null from t2 where y = x.x )
      then
         OUTPUT THE RECORD
      end if
   end loop

It always results in a full scan of T1 
 

Using EXISTS, optimizer runs main query first and then applies dataset to the subquery. If the main table size is small and subquery table size is large then EXISTS usually makes sense.


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
May 08, 2008 07:29:48   #8  
krishnaindia2007 Member Since: September 2007   Contribution: 870    

RE: Compare EXISTS and IN Usage with advantages and disadvantages.
IN or EXISTS which gives better performance depends upon the situation.Select * from T1 where x in ( select y from T2 )is typically processed as:select * from t1, ( select distinct y from t2 ) t2 where t1.x = t2.y;The subquery is evaluated, distinct'ed, indexed (or hashed or sorted) and then joined to the original table -- typically.While using IN , optimizer runs subquery first and then joins it with main dataset. Table in the subqery is small and table in the main query is large then IN usaully makes sense.select * from t1 where exists ( select null from t2 where y = x )That is processed more like: for x in ( select * from t1 ) loop if ( exists ( select null from t2 where y = x.x ) then OUTPUT THE RECORD end if end loopIt always results in a full scan of T1 Using EXISTS, optimizer runs main query first and then applies dataset to the subquery. If the main table size is small and subquery table size is large then EXISTS usually makes sense.
 
Is this answer useful? Yes | No
July 14, 2008 12:41:44   #9  
be_viral Member Since: July 2008   Contribution: 2    

RE: Compare EXISTS and IN Usage with advantages and disadvantages.
We should use Exists or In based on the no of records inner and outer query is expected to return.Using Exists : Here the outer query is executed first and then inner query is excecuted for each value of outer query. So use this when outer query contains less records.Using IN : Here the inner query is executed first and then outer query is excecuted for each value of inner query. So use this when inner query contains less records.
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    


 
Go To Top


 Sponsored Links

 




About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape