gpt4 book ai didi

mysql - 在 left join vs. 中使用 Inner Join 的性能多个左连接

转载 作者:可可西里 更新时间:2023-11-01 08:55:03 24 4
gpt4 key购买 nike

我有一个关于 MySQL 性能的问题

查询 1:

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

查询2:

select departments.*, booth_feature.some_feature
from departments
left join (booth, booth_feature) on ( booth.dept_id = departments.dept_id and booth.booth_id=booth_feature.booth_id)

假设:部门可以有多个展位

1 个展位 => 1 个展位特色

部门 table 和摊位 table 都是大 table 。

使用 explain,第一个查询似乎更好(它在 booth_feature 之前检查 booth),尽管左连接通常比内连接更昂贵。那正确吗?

最佳答案

您基本上是在第二个查询中在 booth 和 booth_feature 之间进行完全交叉连接,如果这些表变大,这可能会非常昂贵。并且优化器将无法智能地将“部门”与交叉产品结合起来,因为它需要先计算完整的交叉产品,然后才能将其与部门结合起来。由于缺少连接子句(使其成为完整的交叉产品)。

在第一个查询中,优化器可以使用来自部门和摊位的索引,然后使用来自该结果(基于两个索引)的引用(MYSQL EXPLAIN 输出中的 REF 类型)到另一个索引(booth_features)中

关于mysql - 在 left join vs. 中使用 Inner Join 的性能多个左连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6300550/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com