MySQL 为什么 STRAIGHT_JOIN 对这个查询有如此大的改进,它写在 SELECT 关键字之后是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5818837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does STRAIGHT_JOIN so drastically improve this query, and what does it mean when it is written after the SELECT keyword?
提问by zod
I have the following MySql query:
我有以下 MySql 查询:
select t1.*
from Table1 t1
inner join Table2 t2
on t1.CommonID = t2.CommonID
where t1.FilterID = 1
It takes about 30 seconds to run, which was strange, because if I comment out the join or the where clause it takes less than a second: i.e.
运行大约需要 30 秒,这很奇怪,因为如果我注释掉 join 或 where 子句,它需要不到一秒钟的时间:即
select t1.*
from Table1 t1
where t1.FilterID = 1
or
或者
select t1.*
from Table1 t1
inner join Table2 t2
on t1.CommonID = t2.CommonID
each take less than a second.
每个都需要不到一秒钟。
Then there is the STRAIGHT_JOIN keyword, which I can find one reference of, here: http://dev.mysql.com/doc/refman/5.0/en/join.html
然后是 STRAIGHT_JOIN 关键字,我可以在这里找到一个参考:http: //dev.mysql.com/doc/refman/5.0/en/join.html
STRAIGHT_JOIN is similar to JOIN, except that the left table is always read before the right table. This can be used for those (few) cases for which the join optimizer puts the tables in the wrong order.
STRAIGHT_JOIN 与 JOIN 类似,只是左表总是在右表之前被读取。这可用于连接优化器将表以错误顺序放置的那些(少数)情况。
What? I can write:
什么?我可以写:
select t1.*
from Table1 t1
STRAIGHT_JOIN Table2 t2
on t1.CommonID = t2.CommonID
where t1.FilterID = 1
and the query executes in less than a second.
查询在不到一秒的时间内执行。
Even stranger, I can write:
即使是陌生人,我也可以写:
select STRAIGHT_JOIN t1.*
from Table1 t1
inner join Table2 t2
on t1.CommonID = t2.CommonID
where t1.FilterID = 1
and it takes less than a second, and this syntax does not appear to even be legal.
它只需要不到一秒钟的时间,而且这种语法似乎也不合法。
I would guess the second example means that a STRAIGHT_JOIN will be used whenever an INNER JOIN is written, but I can't find any documentation about it.
我猜第二个例子意味着每当编写 INNER JOIN 时都会使用 STRAIGHT_JOIN ,但我找不到任何关于它的文档。
What is going on here, and how can the “join optimizer” result in such relatively poor performance? Should I always use STRAIGHT_JOIN? How can I tell when to use it or not?
这是怎么回事,“连接优化器”怎么会导致如此相对较差的性能?我应该总是使用 STRAIGHT_JOIN 吗?我怎么知道什么时候使用它?
Table1 and Table2 both have integer primary keys; FilterID is a foreign key to another table; the CommonID columns are both foreign keys to a third table. They both have indexes on them. The database engine is InnoDB.
Table1 和 Table2 都有整数主键;FilterID 是另一个表的外键;CommonID 列都是第三个表的外键。它们都有索引。数据库引擎是 InnoDB。
Thanks
谢谢
采纳答案by Quassnoi
What is going on here, and how can the “join optimizer” result in such relatively poor performance?
这是怎么回事,“连接优化器”怎么会导致如此相对较差的性能?
STRAIGHT_JOIN
forces the join order of the tables, so table1
is scanned in the outer loop and table2
in the inner loop.
STRAIGHT_JOIN
强制表的连接顺序,因此table1
在外循环和table2
内循环中进行扫描。
The optimizer is not perfect (though stil quite decent), and the most probable cause is the outdated statistics.
优化器并不完美(虽然仍然相当不错),最可能的原因是过时的统计数据。
Should I always use
STRAIGHT_JOIN
我应该总是使用
STRAIGHT_JOIN
No, only when the optimizer is wrong. This may be if your data distribution is severely skewed or cannot be calculated properly (say, for spatial or fulltext indexes).
不,只有当优化器错误时。这可能是因为您的数据分布严重倾斜或无法正确计算(例如,对于空间或全文索引)。
How can I tell when to use it or not?
我怎么知道什么时候使用它?
You should collect the statistics, build the plans for both ways and understand what do these plans mean.
您应该收集统计数据,为两种方式制定计划并了解这些计划的含义。
If you see that:
如果你看到:
The automatically generated plan is not optimal and cannot be improved by the standard ways,
The
STRAIGHT_JOIN
version is better, you understand it always will and understand whyit always will
自动生成的计划不是最优的,无法通过标准方式进行改进,
该
STRAIGHT_JOIN
版本更好,你了解它总是会和理解,为什么它总是会
, then use STRAIGHT_JOIN
.
,然后使用STRAIGHT_JOIN
。