What's difference between inner join and outer join (left join,right join), and which has the best performance of them?
Thanks!
What's difference between inner join and outer join (left join,right join), and which has the best performance of them?
Thanks!
Check this article:
A Visual Explanation of SQL Joins
Inner Join:
Left Outer Join:
Right Outer Join:
Performance comparisions between types of joins are irrelevant as they give differnt results sets. Even if an inner join is faster you wouldn't use it if you needed the results of a left join (which includes even the records which don't match the second table in the join).
A
LEFT JOIN B
is the same asB RIGHT JOIN A
. Some RDBMS don't haveRIGHT JOIN
, so you have to rewrite yourRIGHT JOIN
logic toLEFT JOIN
logicRead the above code as
B on RIGHT, JOINs A
. Which is just the same as A is on LEFTWhatever is on left, is always evaluated, always have an output. You can imagine A LEFT JOIN B, B RIGHT JOIN A as:
Left, right, inner and outer don't affect performance, and they have been well explained here already.
However there are hints you can add to joins that do effect performance: hash, loop and merge.
Normally the query planner decides which of these to do, but occasionally you can improve performance by overriding it.
A
loop
join
goes through every row in the second table for each row in the first. This is good if you have one very big table and one much smaller.A
merge
join
goes through both tables together in order. It can be very quick if both tables are already ordered by the field that you're joining on.A
hash
join
uses lots of temporary tables to group the output as it sorts through the joined data.Some specialist DBs also supports other types, such as bitmap joins.
Hopefully you understand the pictures. Performance-wise, they are equivalent - no difference.
EDIT: Oops. Guess you didn't care about that part of the answer.