简体   繁体   English

在解释计划中签入时,MYSQL使用JOIN TYPE'ALL'

[英]MYSQL using JOIN TYPE 'ALL' when checked in explain plan

The query is as below, 查询如下

select *
from lab this_ 
inner join visits v1_ on this_.visit_id=v1_.id

v1_.id is primary key in the query. v1_.id是查询中的主键。

It takes more than 1 minute to complete. 需要超过1分钟才能完成。

Below is the plan. 下面是计划。

id   select_type   table   type  possible_keys  key       
1      SIMPLE       v1_     ALL    <null>      <null>
1      SIMPLE      this_    ALL    <null>      <null>

Not sure why primary key is picked as key. 不确定为什么选择主键作为键。 Also type is ALL. 还要输入ALL。

Mysql may ignore an index during the excution of the query if it belives that the alternative plan is more efficient. 如果MySQL认为替代计划更为有效,则在执行查询期间可能会忽略索引。 A couple of points it considers: 它考虑了几点:

  1. The size of the tables. 表格的大小。 If the visits table is small, then there is not too much point in using the index. 如果访问表很小,则使用索引没有太多意义。

  2. Selectivity. 选择性。 You do join the 2 tables, however there is no filtering and you want all fields from both tables. 您确实联接了两个表,但是没有过滤,并且您希望两个表中的所有字段都可以。 This could mean that mysql has to return most of the records from the visits table anyway and the index covers only the id column. 这可能意味着mysql无论如何都要从visits表返回大多数记录,并且索引仅覆盖id列。 Therefore, mysql would be forced to scan through most of the records of the visits table anyway to return the data, so there is not much more to gain by using the index. 因此,无论如何,MySQL都将被迫遍历visits表的大多数记录以返回数据,因此使用索引没有太多收获。

  3. Index on the fields on the other side of the join. 在联接另一侧的字段上建立索引。 You do not mention if labs.visit_id field is indexed. 您没有提及是否为labs.visit_id字段建立了索引。 If it is not, then again there is less to be gained from using the pk of the visits table. 如果不是,那么再次使用visits表的pk将会获得更少的收益。

The speed of producing the results does not depend on the indexes used, it also depends of the size of the resultset (both record and field count), mysql configuration, and the overall performance of the underlying system. 产生结果的速度不取决于所使用的索引,还取决于结果集的大小(记录和字段计数),mysql配置以及底层系统的整体性能。 Nevertheless, if you believe that mysql should use the pk of the visits table, then use an index hint in the query to emphasise that the index should be used. 不过,如果您认为mysql应该使用visits表的pk,则在查询中使用索引提示来强调应该使用索引。 You can check with explain if mysql was influenced by the index hint. 您可以检查以explain mysql是否受索引提示的影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM