简体   繁体   English

MySQL未按预期使用索引

[英]MySQL doesn't use index as expected

EXPLAIN on this query EXPLAIN在此查询

select v.type,sum(c.rank)
from
  (select distinct power,color,type from vehicle) v
  join configuration c using (power,color)
group by v.type

gives

+----+-------------+---------------+------------+-------+---------------+-------------+---------+-----------------------------------------+---------+----------+---------------------------------+
| id | select_type |     table     | partitions | type  | possible_keys |     key     | key_len |                   ref                   |  rows   | filtered |              Extra              |
+----+-------------+---------------+------------+-------+---------------+-------------+---------+-----------------------------------------+---------+----------+---------------------------------+
|  1 | PRIMARY     | configuration | NULL       | ALL   | veh           | NULL        | NULL    | NULL                                    |   76658 |   100.00 | Using temporary; Using filesort |
|  1 | PRIMARY     | <derived2>    | NULL       | ref   | <auto_key0>   | <auto_key0> | 6       | configuration.power,configuration.color |      65 |   100.00 | NULL                            |
|  2 | DERIVED     | vehicle       | NULL       | index | cov           | cov         | 20      | NULL                                    | 5058658 |   100.00 | Using index                     |
+----+-------------+---------------+------------+-------+---------------+-------------+---------+-----------------------------------------+---------+----------+---------------------------------+

The index on configuration (power,color) is not used even if I set force index 即使设置了force index ,也不会使用配置索引(电源,颜色)

If I use a table instead of a subquery 如果我使用表而不是子查询

create table tmp select distinct power,color,type from vehicle

then Explain on the 'same' query 然后Explain “相同”查询

select v.type,sum(c.rank)
from
  tmp v 
  join configuration c using (power,color)
group by type

becomes 变成

+----+-------------+---------------+------------+------+---------------+------+---------+---------------------+---------+----------+---------------------------------+
| id | select_type |     table     | partitions | type | possible_keys | key  | key_len |         ref         |  rows   | filtered |              Extra              |
+----+-------------+---------------+------------+------+---------------+------+---------+---------------------+---------+----------+---------------------------------+
|  1 | SIMPLE      | tmp           | NULL       | ALL  | NULL          | NULL | NULL    | NULL                | 1016144 |   100.00 | Using temporary; Using filesort |
|  1 | SIMPLE      | configuration | NULL       | ref  | veh           | veh  | 6       | tmp.power,tmp.color |       2 |   100.00 | NULL                            |
+----+-------------+---------------+------------+------+---------------+------+---------+---------------------+---------+----------+---------------------------------+

and this is 4 times faster 这快了四倍

How can I avoid using a hard table ? 如何避免使用硬桌子?

In the first case the optimizer thinks it is better to do it the other way around, by using the auto generated key in the derived table. 在第一种情况下,优化器认为最好通过使用派生表中的自动生成的键来做到这一点。

In the second case there is no key in the temp table, so the best plan is to go for tmp first. 在第二种情况下,临时表中没有键,因此最好的计划是先进行tmp。

You should be able to force the table order by using STRAIGHT_JOIN instead of JOIN . 您应该能够通过使用STRAIGHT_JOIN而不是JOIN来强制执行表顺序。

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

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