Solved

correlated subquery vs simple joins

Posted on 2010-11-10
8
1,020 Views
Last Modified: 2013-12-18
If for a query, we can use both correlated subquery and joins,
which ones are better, when both produce the same result.?
0
Comment
Question by:sakthikumar
8 Comments
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 34109104
It depends on how you actually write the query with correlated subquery and with joins.

I think Joins are the best if you have proper indexes and good data designs which link tables properly. At time, the correlated subqueries are internally changed to make use of join type queries by SQL engine.

Can you post both versions of the queries please ?
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 34109109
0
 
LVL 28

Accepted Solution

by:
Naveen Kumar earned 250 total points
ID: 34109139
Just read this important link as it explain with examples on what kind of differences are there and how it works.

http://www.oracle-database-tips.com/oracle_subquery.html
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 13

Expert Comment

by:riazpk
ID: 34110363
Like everything, it depends. Test, Test, Test. This is what i can say.

Also bear in mind that
(1) sub-query might return different result set than join and you might have to use DISTINCT in order to get equivalent results for both cases.
(2) sub-query MIGHT NOT be equivalent of an equi-join so you might be forced to use outer join which in turn, might force optimizer to eliminate some of access paths.

Sub-query might be useful when you want to get first row as soon as possible. And sometimes, i have used sub-query (along with ROWNUM>0) to avoid merging with the main query and being executed separately.

In conclusion,  nothing is superior over other always. If it would have been the case, Oracle would have only the superior one.
0
 
LVL 3

Expert Comment

by:mpaladugu
ID: 34110853
   The advantage of using joins is, yon can select any column from both the tables
    in you select statement.
    where as in a correlated sub-query you will not be able select columns from a table used inside a  
    sub-query
 
0
 

Author Comment

by:sakthikumar
ID: 34120471
sdstuber:

It looks like duplicate, But I need to know which one to use, if both options are available.
0
 
LVL 73

Assisted Solution

by:sdstuber
sdstuber earned 250 total points
ID: 34120539
in general you can guess before hand which one that will be based on the amount of reuse you'll get from the scalar query.
If the query  results will be used a lot, then you'll get the advantage of caching, if they won't, then you won't and a regular sub query might be better.

best option is to test both, use the one that is faster and scales better.

it's typically a trivial effort to try both ways
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now