我有一个完美的连接查询,有时我从表 EventKeyReference 中获取 eventKey、标签和 dataUnit,从 EventData 中获取 valueParam,从表 EventHeader 中获取时间戳。在我的 where 子句中,我有 Unit u.id = 3。这很好用。但是,当我尝试搜索 Unit 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 专家还很远。