MySQL Select不使用不同Where值的索引

时间:2013-10-28 14:34:57

标签: mysql database

我有一个连接查询,有时可以完美运行 我从表EventKeyReference获取eventKey,label和dataUnit,从EventData获取valueParam,从表EventHeader获取timestamp。 在我的where子句中,我有Unit u.id = 3.这很好用。但是,当我尝试搜索单位u.id = 1时,查询有时可能需要20秒。

搜索id = 3

SELECT ek.eventKey, ek.label, ek.dataUnit, ed.valueParam, eh.timestamp
FROM EventKeyReference AS ek 
INNER JOIN UnitReference_EventKeyReference AS urek ON urek.eventKeyReferences_id = ek.id 
INNER JOIN UnitReference AS ur ON ur.id = urek.UnitReference_id
INNER JOIN Unit AS un ON un.id = ur.unit_id INNER JOIN User AS u ON u.id = ur.user_id
INNER JOIN EventData AS ed ON ek.eventKey = ed.keyParam
INNER JOIN EventHeader AS eh ON eh.id = ed.eventHeader_id
WHERE ek.eventKey = 'C004' AND u.id = 3 AND un.serialNumber=221148096
ORDER BY eh.timestamp LIMIT 1;

给出这个:

+----------+----------+----------+------------+---------------------+
| eventKey | label    | dataUnit | valueParam | timestamp           |
+----------+----------+----------+------------+---------------------+
| C004     | Utgang 2 | on/off   | 0          | 2000-01-01 00:01:04 |
+----------+----------+----------+------------+---------------------+
1 row in set (0.00 sec)

搜索id = 1可以得到:

+----------+-------------+----------+------------+---------------------+
| eventKey | label       | dataUnit | valueParam | timestamp           |
+----------+-------------+----------+------------+---------------------+
| C004     | DigitalOut1 | ON/OFF   | 0          | 2000-01-01 00:01:04 |
+----------+-------------+----------+------------+---------------------+
1 row in set (16.04 sec)

如您所见,查询需要16秒才能执行。

在检查EXPLAIN SELECT时,我得到了这个。

    mysql> EXPLAIN SELECT ek.eventKey, ek.label, ek.dataUnit, ed.valueParam,    eh.timestamp   FROM EventKeyReference AS ek INNER JOIN UnitReference_EventKeyReference AS urek ON urek.eventKeyReferences_id = ek.id INNER JOIN UnitReference AS ur ON ur.id = urek.UnitReference_id  INNER JOIN Unit AS un ON un.id = ur.unit_id INNER JOIN User AS u ON u.id = ur.user_id INNER JOIN EventData AS ed ON ek.eventKey = ed.keyParam INNER JOIN EventHeader AS eh ON eh.id = ed.eventHeader_id   WHERE ek.eventKey = 'C004' AND u.id = 1 AND un.serialNumber=221148096 ORDER BY eh.timestamp DESC LIMIT 1;
+----+-------------+-------+--------+-----------------------------------------------+--------------------+---------+------------------------------------+--------+----------------------------------------------+
| id | select_type | table | type   | possible_keys                                 | key                | key_len | ref                                | rows   | Extra                                        |
+----+-------------+-------+--------+-----------------------------------------------+--------------------+---------+------------------------------------+--------+----------------------------------------------+
|  1 | SIMPLE      | u     | const  | PRIMARY                                       | PRIMARY            | 8       | const                              |      1 | Using index; Using temporary; Using filesort |
|  1 | SIMPLE      | eh    | index  | PRIMARY                                       | eh_timestamp       | 9       | NULL                               | 169161 | Using index                                  |
|  1 | SIMPLE      | ed    | ref    | FK794A194417A622D6                            | FK794A194417A622D6 | 9       | fltcoor.eh.id                      |      5 | Using where                                  |
|  1 | SIMPLE      | ur    | range  | PRIMARY,FK83382A0747140EFE,FK83382A073EC5085E | FK83382A0747140EFE | 9       | NULL                               |      3 | Using where; Using join buffer               |
|  1 | SIMPLE      | urek  | ref    | PRIMARY,FKAC98AB0E2DFCBD8F,FKAC98AB0E564AEB96 | FKAC98AB0E564AEB96 | 8       | fltcoor.ur.id                      |     11 | Using index                                  |
|  1 | SIMPLE      | un    | eq_ref | PRIMARY,serialNumber                          | PRIMARY            | 8       | fltcoor.ur.unit_id                 |      1 | Using where                                  |
|  1 | SIMPLE      | ek    | eq_ref | PRIMARY                                       | PRIMARY            | 8       | fltcoor.urek.eventKeyReferences_id |      1 | Using where                                  |
+----+-------------+-------+--------+-----------------------------------------------+--------------------+---------+------------------------------------+--------+----------------------------------------------+
7 rows in set (0.00 sec)

mysql> EXPLAIN SELECT ek.eventKey, ek.label, ek.dataUnit, ed.valueParam, eh.timestamp   FROM EventKeyReference AS ek INNER JOIN UnitReference_EventKeyReference AS urek ON urek.eventKeyReferences_id = ek.id INNER JOIN UnitReference AS ur ON ur.id = urek.UnitReference_id  INNER JOIN Unit AS un ON un.id = ur.unit_id INNER JOIN User AS u ON u.id = ur.user_id INNER JOIN EventData AS ed ON ek.eventKey = ed.keyParam INNER JOIN EventHeader AS eh ON eh.id = ed.eventHeader_id   WHERE ek.eventKey = 'C004' AND u.id = 3 AND un.serialNumber=221148096 ORDER BY eh.timestamp DESC LIMIT 1;
+----+-------------+-------+--------+-----------------------------------------------+--------------------+---------+------------------------------------+------+-------------+
| id | select_type | table | type   | possible_keys                                 | key                | key_len | ref                                | rows | Extra       |
+----+-------------+-------+--------+-----------------------------------------------+--------------------+---------+------------------------------------+------+-------------+
|  1 | SIMPLE      | u     | const  | PRIMARY                                       | PRIMARY            | 8       | const                              |    1 | Using index |
|  1 | SIMPLE      | eh    | index  | PRIMARY                                       | eh_timestamp       | 9       | NULL                               |    1 | Using index |
|  1 | SIMPLE      | ur    | ref    | PRIMARY,FK83382A0747140EFE,FK83382A073EC5085E | FK83382A0747140EFE | 9       | const                              |    2 | Using where |
|  1 | SIMPLE      | un    | eq_ref | PRIMARY,serialNumber                          | PRIMARY            | 8       | fltcoor.ur.unit_id                 |    1 | Using where |
|  1 | SIMPLE      | ed    | ref    | FK794A194417A622D6                            | FK794A194417A622D6 | 9       | fltcoor.eh.id                      |    5 | Using where |
|  1 | SIMPLE      | urek  | ref    | PRIMARY,FKAC98AB0E2DFCBD8F,FKAC98AB0E564AEB96 | FKAC98AB0E564AEB96 | 8       | fltcoor.ur.id                      |   11 | Using index |
|  1 | SIMPLE      | ek    | eq_ref | PRIMARY                                       | PRIMARY            | 8       | fltcoor.urek.eventKeyReferences_id |    1 | Using where |
+----+-------------+-------+--------+-----------------------------------------------+--------------------+---------+------------------------------------+------+-------------+
7 rows in set (0.00 sec)

出于某种原因,在id = 1的查询中,MYSQL会经过大约16.000行。

这一直困扰着我,因为我无法找到为什么会这样。如果有更多信息我很乐意提供,因为我自己远离Mysql专家。

2 个答案:

答案 0 :(得分:2)

正如Jakob所说,如果你有160000个用户1的条目,那可能是你的窒息点。我已经改写了希望有所帮助。我改变了顺序,希望将最小的粒状块放在查询的主要位置并从中链接起来。我怀疑给定序列号的非常少的记录,因此将其放入WHERE子句中,并且辅助添加JOIN条件(通过实现的ON / AND子句)应该有所帮助。

SELECT 
      ek.eventKey, 
      ek.label, 
      ek.dataUnit, 
      ed.valueParam, 
      eh.timestamp
   FROM 
      Unit AS un 
         INNER JOIN UnitReference AS ur 
            ON un.id = ur.unit_id
            AND ur.user_id = 3      <==  ADDED USER ID CRITERIA HERE
            INNER JOIN User AS u 
               ON ur.user_id = u.id
            INNER JOIN UnitReference_EventKeyReference AS urek 
               ON ur.id = urek.UnitReference_id
               INNER JOIN EventKeyReference AS ek 
                  ON urek.eventKeyReferences_id = ek.id
                  AND ek.eventKey = 'C004'   <== ADDED EVENT KEY CRITERIA HERE
               INNER JOIN EventData AS ed 
                  ON ek.eventKey = ed.keyParam
                  INNER JOIN EventHeader AS eh 
                     ON ed.eventHeader_id = eh.id
   WHERE 
      un.serialNumber=221148096
   ORDER BY 
      eh.timestamp LIMIT 1;

如果以上方法不起作用,我会尝试的唯一其他项目,因为它是MySQL,我会再添加一个关键字......

SELECT STRAIGHT_JOIN [查询的其余部分]

告诉MySQL按照你说的顺序查询,而不是试着以不同的顺序为你考虑。

答案 1 :(得分:1)

您的查询实际上正在执行您要求它执行的操作。我怀疑你实际上有160000+行,u.id = 1,而你只有一行u.id = 3.看看你的解释,你可以看到你的查询确实在两个查询中使用了索引。但在一种情况下,你循环超过160000+行,因为这是u.id = 1的数量。我敢打赌,如果你删除你的限制1条款,你会得到很多很慢的查询结果。