Why does STRAIGHT_JOIN so drastically improve this query, and what does it mean when it is written after the SELECT keyword?

后端 未结 1 1857
半阙折子戏
半阙折子戏 2020-12-07 18:49

I have the following MySql query:

select t1.*
from Table1 t1
inner join Table2 t2
on t1.CommonID = t2.CommonID
where t1.FilterID = 1

It tak

1条回答
  •  感动是毒
    2020-12-07 19:04

    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.

    The optimizer is not perfect (though stil quite decent), and the most probable cause is the outdated statistics.

    Should I always use 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:

    1. The automatically generated plan is not optimal and cannot be improved by the standard ways,

    2. The STRAIGHT_JOIN version is better, you understand it always will and understand why it always will

    , then use STRAIGHT_JOIN.

    0 讨论(0)
提交回复
热议问题