0

In theory, why would inner join work remarkably faster then left outer join given the fact that both queries return same result set. I had a query which would take long time to describe, but this is what I saw changing single join: left outer join - 6 sec, inner join - 0 sec (the rest of the query is the same). Result set: the same

4
  • 9
    Did you compare the execution plans? If you ask a vague question, I hope you don't expect very specific results. Commented Apr 16, 2012 at 14:25
  • The first thing you should do if you get expected results is to ensure your statistics (and indexes) are up to date. Commented Apr 16, 2012 at 14:34
  • @Aaron Bertrand Yes, sorry can't give details. Execution plans look different, but I don't read them well. Anyway I just thought maybe there are general reasons for this...
    – ren
    Commented Apr 16, 2012 at 15:05
  • Not that I know of. Unfortunately if you can't read the execution plans and we can't see them either, there's not much anyone on StackOverflow is going to be able to do to help you. Commented Apr 16, 2012 at 15:16

2 Answers 2

1

Actually depending on the data, left outer join and inner join would not return the same results..most likely left outer join will have more result and again depends on the data..

1
  • 1
    I'm not sure why this is an answer and not a comment. While it's true that a left join may return more results in cases where the right table has no matching rows, the OP clearly indicated that in his case, the two do produce identical rows. This may be due to a malformed query (e.g. join/filter criteria not in the right spot) but it may be that there are no rows in the left table without a match in the right table. Commented Apr 16, 2012 at 14:43
0

I'd be worried if I changed a left join to an inner join and the results were not different. I would suspect that you have a condition on the left side of the table in the where clause effectively (and probably incorrectly) turning it into an inner join.

Something like:

select * 
from table1 t1
left join table2 t2 on t1.myid = t2.myid
where t2.somefield = 'something'

Which is not the same thing as

select * 
from table1 t1
left join table2 t2 
on t1.myid = t2.myid and t2.somefield = 'something'

So first I would be worried that my query was incorrect to begin with, then I would worry about performance. An inner join is NOT a performance enhancement for a Left Join, they mean two different things and should return different results unless you have a table where there will always be a match for every record. In this case you change to an inner join because the other is incorrect not to improve performance.

My best guess as to the reason the left join takes longer is that it is joining to many more rows that then get filtered out by the where clause. But that is just a wild guess. To know you need to look at the Execution plans.

1
  • In general I agree, but to be fair, I've built queries with LEFT OUTER JOIN even though today the data could easily fit an INNER JOIN - the results today are not necessarily what the results will always be, and there may be non-matching rows introduced later (or that were there in the past). So I don't tend to sound the alarm bells too loudly if an inner and outer join happen to produce the same results today. The fact that the execution plans are different leads me to believe there is more variance between the two queries than just the join keywords. Commented Apr 16, 2012 at 15:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.