tags:

views:

1448

answers:

6

What's difference between inner join and outer join (left join,right join), and which has the best performance of them?

Thanks!

+16  A: 
CMS
Part 2 of the question was about performance which is not necessarily directly related to the join method, did you have any further comment on that?
Greg Domjan
Be sure to read all the comments in the article because its presentation of joins as set operations is highly criticable! For instance, a left join is NOT an intersection, since it creates new tuples.
bortzmeyer
The diagram would be better if it also showed FULL OUTER JOIN.Since the different kinds of joins yield different results, the question on performance is somewhat irrelevant. You should choose the one that produces the required results. Performance depends on the internal of the DBMS.
Walter Mitty
A FULL OUTER JOIN would just be the entirety of both circles, so does it really need to be diagrammed?
R. Bemrose
A: 

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.

le dorfier
Apparently he does.
bart
+1  A: 

A LEFT JOIN B is the same as B RIGHT JOIN A. Some RDBMS don't have RIGHT JOIN, so you have to rewrite your RIGHT JOIN logic to LEFT JOIN logic

A 1 2 3 B 2 3 4 3

SELECT A.I FROM INNER JOIN B ON B.I = A.I;

output: 2 3, 3

SELECT A.I AS X, B.I AS Y FROM A LEFT JOIN B ON B.I = A.I;

read above code as A on LEFT, JOINs B

output:

X Y
1 NULL
2 2
3 3
3 3

SELECT A.I AS X, B.I AS Y FROM B RIGHT JOIN A ON A.I = B.I;

Read the above code as B on RIGHT, JOINs A. Which is just the same as A is on LEFT

Whatever is on left, is always evaluated, always have an output. You can imagine A LEFT JOIN B, B RIGHT JOIN A as:

        var result = new Dictionary<int, int?>();

        var A = new int[] { 1, 2, 3 };
        var B = new int[] { 2, 3, 4, 3 };

        foreach (int aElem in A)
        {


            bool hasMatch = false;
            foreach (int bElem in B)
            {
                if (bElem == aElem)
                {
                    result.Add(aElem, bElem);
                    hasMatch = true;
                }
            }

            if (!hasMatch)
                result.Add(aElem, null);
        }



        foreach(int X in result.Keys)
        {
            MessageBox.Show(string.Format("X {0} Y {1}", X, result[X].HasValue ? result[X].Value.ToString() : "NULL"  ));
        }
Michael Buen
+3  A: 

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).

HLGEM
A: 

Left, right, inner and outer don't effect 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.

Keith
A: 

Try this link: Understanding SQL Joins