Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LEFT INNER JOIN vs. LEFT OUTER JOIN - Why does the OUTER take longer?

Tags:

sql

sql-server

We have the query below. Using a LEFT OUTER join takes 9 seconds to execute. Changing the LEFT OUTER to an LEFT INNER reduces the execution time to 2 seconds, and the same number of rows are returned. Since the same number of rows from the dbo.Accepts table are being processed, regardless of the join type, why would the outer take 3x longer?

SELECT CONVERT(varchar, a.ReadTime, 101) as ReadDate,        a.SubID,        a.PlantID,        a.Unit as UnitID,        a.SubAssembly,        m.Lot   FROM dbo.Accepts a WITH (NOLOCK) LEFT OUTER Join dbo.Marker m WITH (NOLOCK) ON m.SubID = a.SubID WHERE a.LastModifiedTime BETWEEN @LastModifiedTimeStart AND @LastModifiedTimeEnd    AND a.SubAssembly = '400' 
like image 262
Randy Minder Avatar asked Mar 05 '10 19:03

Randy Minder


People also ask

Which is faster Left join or left outer join?

There is not a "better" or a "worse" join type. They have different meaning and they must be used depending on it. In your case, you probably do not have employees with no work_log (no rows in that table), so LEFT JOIN and JOIN will be equivalent in results.

Is left outer join faster than inner join?

If you dont include the items of the left joined table, in the select statement, the left join will be faster than the same query with inner join. If you do include the left joined table in the select statement, the inner join with the same query was equal or faster than the left join.

Is inner or outer join faster?

Returns only the rows that have matching values in both the tables. Includes the matching rows as well as some of the non-matching rows between the two tables. In case there are a large number of rows in the tables and there is an index to use, INNER JOIN is generally faster than OUTER JOIN.

WHY IS LEFT join slower than inner join?

The LEFT JOIN query is slower than the INNER JOIN query because it's doing more work. From the EXPLAIN output, it looks like MySQL is doing nested loop join.


1 Answers

The fact that the same number of rows is returned is an after fact, the query optimizer cannot know in advance that every row in Accepts has a matching row in Marker, can it?

If you join two tables A and B, say A has 1 million rows and B has 1 row. If you say A LEFT INNER JOIN B it means only rows that match both A and B can result, so the query plan is free to scan B first, then use an index to do a range scan in A, and perhaps return 10 rows. But if you say A LEFT OUTER JOIN B then at least all rows in A have to be returned, so the plan must scan everything in A no matter what it finds in B. By using an OUTER join you are eliminating one possible optimization.

If you do know that every row in Accepts will have a match in Marker, then why not declare a foreign key to enforce this? The optimizer will see the constraint, and if is trusted, will take it into account in the plan.

like image 71
Remus Rusanu Avatar answered Oct 05 '22 03:10

Remus Rusanu