关于MYSQL查询的INDEX

时间:2018-11-01 10:37:05

标签: mysql

我有以下MYSQL查询:

SELECT statusdate,consigneenamee,weight,productcode,
  pieces,statustime,statuscode,statusdesc,generatingiata,
  shipmentdate,shipmenttime,consigneeairportcode,signatory 
FROM notes 
where (shipperref='180908184' OR shipperref='180908184' 
       OR shipperref='180908184' OR shipperref='180908184 ' 
       OR shipperref like 'P_L_%180908184')  
order by edicheckpointdate asc, edicheckpointtime asc;

我添加了索引以使用MYSQL Workbench加快该查询的速度,但是在执行EXPLAIN命令时,它仍然不显示键,并显示为NULL:

+----+-------------+---------------+------+---------------+------+---------+------+---------+-----------------------------+
| id | select_type | table         | type | possible_keys | key  | key_len | ref  | rows    | Extra                       |
+----+-------------+---------------+------+---------------+------+---------+------+---------+-----------------------------+
| 1  | SIMPLE      | dhltracking_2 | ALL  | index2        | NULL | NULL    | NULL | 3920874 | Using where; Using filesort |
+----+-------------+---------------+------+---------------+------+---------+------+---------+-----------------------------+

发生这种情况的原因以及如何加快此查询的速度?

我的索引:

Index added on table

1 个答案:

答案 0 :(得分:1)

您的查询中有LIKE条语句,我认为您的索引跨越表行的20%至30%(或更多)。因此,出于性能原因,MySQL可以忽略它。 我的建议:

  1. 按照@Solarflare的建议添加FORCE INDEX
  2. 使用FULLTEXT索引(也适用于CHAR和VARCHAR)并使用MATCH ... AGAINST搜索(https://dev.mysql.com/doc/refman/8.0/en/fulltext-boolean.html