views:

55

answers:

5

Hi!

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

A: 

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

Tobiasopdenbrouw
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 Venema
@Marjan, what if it's a `FULL OUTER JOIN`?
Abe Miessler
@Abe: I just realised that and edited my comment... (Can't delete them...)
Marjan Venema
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.
Matt
A: 

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

Thomas Weller
+1  A: 

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.

Abe Miessler
+1  A: 

This images helps explain all

Mowgli
I understand the concepts well, but my question is about performance.
Lluis Martinez
A: 

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

Beth