【发布时间】:2018-01-04 09:11:53
【问题描述】:
似乎第二个语句在加入之前首先应用 where 条件,而第一个语句在应用 where 条件之前确实加入,所以第二个语句会更快,因为它会减少加入。但真的是这样吗?是否有参考明确指出在第一个语句中,在所有其他连接操作完成后执行 where 条件?
SELECT * FROM class t1
LEFT JOIN class_students t2 ON t1.id = t2.class_id
LEFT JOIN student t3 ON t2.student_id = t3.id
WHERE t1.id = 1;
或
SELECT * FROM (SELECT * FROM class WHERE id = 1) t1
LEFT JOIN class_students t2 ON t1.id = t2.class_id
LEFT JOIN student t3 ON t2.student_id = t3.id;
【问题讨论】:
-
编译器分解你的 SQL 语句并选择优化器认为最快的。如果您查看执行计划,它实际上可能会显示完全相同的计划。
-
Lfet 不是公认的命令
标签: mysql sql performance join