5

Why the outer joins are in general slower than inner joins? I mean, independently from the database vendor. I suppose it's a matter of implementation or the access plan used, but I wasn't able to convince a colleague of mine who thinks performance should be the same.

Thanks in advance Lluis

6 Answers 6

7

An inner join will eliminate rows while an outer join doesn't. This results in a lot more rows to process.

Take a look at this article that visually describes joins. Notice how much smaller the result set is for the inner join vs the left and full outer join. These pictures don't represent every scenario but they give you an idea of what is going on.

Sign up to request clarification or add additional context in comments.

Comments

3

I had a problem with performance in my website and found a query taking 7 seconds to complete.

All the columns in this query were numbers and were indexed(SQL Server 2008 R2).

This full outer join included 3 tables with 150, 350 and 270 thousand records each.

I simply replaced the FULL OUTER JOIN for LEFT JOIN and execution time reduced to 0 seconds (miliseconds).

Before implementing the full outer join I had only tested it with a few records. However, I have learned that if tables have thousands or millions of records, the performance of a full outer join will not be good.

Comments

1

In general, it is because the db engine has to perform much more comparison operations to narrow down the result set.

Comments

1

Test it with real data. It may be that unless the outer join introduces additional rows, the performance is the same.

Comments

1

Some times left out join perform faster than inner joins. this will depends on the 2 results sets which supposed to be joined

Comments

0

Outer join will normally give you MANY more results (A*B instead of WHERE A=B). That'll take more time.

4 Comments

An outer join will not normally give you A multiplied by B as the number of results as it isn't a union. It will give you the number of records in A or B as a result, depending on which is the primary select of the two. Outer join only means that you want all records on the primary selected table and null values in the joined table's columns when there is no corresponding record in the joined table. A*B would only occur if you can do a "full" outer join: a left and right outer join combined.
@Marjan, what if it's a FULL OUTER JOIN?
@Abe: I just realised that and edited my comment... (Can't delete them...)
Not to pick nits but A*B (the cross product) is only normally the output of CROSS JOIN. If your join condition was 1 = 1 it would become a cross product, but that's true for INNER JOIN as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.