Peformance of using Inner Join within leftjoin v.s. multiple leftjoins
I have a question about MySQL performance
Query1:
select departments.*, booth_feature.some_feature
from departments
left join booth on booth.dept_id = departments.dept_id
left join booth_feature on booth.booth_id=booth_feature.booth_id
Query2:
select departments.*, booth_feature.some_feature
from departments
left join (booth, booth_feature) on ( booth.dept_id = departme开发者_StackOverflow社区nts.dept_id and booth.booth_id=booth_feature.booth_id)
Assuming: department can have multiple booths
1 booth => 1 booth feature
both department and booth tables are big tables.
Using explain, the first query seems to be better (it checks on booth before booth_feature), despite the fact that left join is generally more expensive than inner join. Is that right?
You are basically doing a full cross join between booth and booth_feature in the second query, if these tables grow large, this have the potential of being quite costy. And the optimizer wont have a way of intelligently joining 'departments' with the cross product since it needs to calculate the full cross product before its able to join it with departments. Due to the lack of a join clause (making it a full cross product).
in the first query, the optimizer can use indexes from departments and booths, and then use a reference (REF type in the MYSQL EXPLAIN output) from that result (based on both indexes) into another index (of booth_features)
精彩评论