我有一个表,我最近将几个列的类型从 varchar 更改为 enum(见下文)。我的应用程序在这两个列上针对此表进行查询,一旦进行类型更改,我发现此查询的性能严重下降(我已包含下面的查询以及解释计划结果)。到目前为止,我一直无法在这里找到罪魁祸首,并希望有人遇到这个问题并可以提供建议。
desc order_transmission_history;
+--------------------------+--------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------------+--------------+------+-----+---------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| transmission_id | varchar(255) | YES | | NULL | |
| transmitter_type | varchar(10) | YES | MUL | NULL | |
| initial_attempt_date | timestamp | NO | MUL | CURRENT_TIMESTAMP | |
| most_recent_attempt_date | timestamp | NO | | 0000-00-00 00:00:00 | |
| most_recent_status | varchar(16) | YES | | NULL | |
+--------------------------+--------------+------+-----+---------------------+----------------+
指数为:KEY transmission_history_transmitter_status_date
( transmitter_type
, most_recent_status
, initial_attempt_date
)
explain SELECT * FROM order_transmission_history where transmitter_type = 'FAX_1' AND transmission_id = '' AND (most_recent_status is null or (most_recent_status not in ('SENT', 'ERROR')));
+----+-------------+----------------------+-------+-------------------------------------------------------------------------------------------+----------------------------------------------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------------+-------+-------------------------------------------------------------------------------------------+----------------------------------------------+---------+------+------+-------------+
| 1 | SIMPLE | transmission_history | range | transmission_history_transmitter_status_date | transmission_history_transmitter_status_date | 32 | NULL | 350 | Using where |
+----+-------------+----------------------+-------+-------------------------------------------------------------------------------------------+----------------------------------------------+---------+------+------+-------------+
现在,使用更改的数据类型:
+--------------------------------------+----------------------------------------------------------------------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------------------------+----------------------------------------------------------------------------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| transmission_id | varchar(255) | YES | | NULL | |
| initial_attempt_date | timestamp | NO | MUL | CURRENT_TIMESTAMP | |
| most_recent_attempt_date | timestamp | YES | | NULL | |
| transmitter_type | enum('FAX_1','FAX_2','FAX_3','EMAIL') | YES | MUL | NULL | |
| most_recent_status | enum('NONE','PENDING','TRANSIENT_ERROR','ERROR','SENDING','SENT','SYSTEM_ERROR') | YES | | NULL | |
+--------------------------------------+----------------------------------------------------------------------------------+------+-----+-------------------+----------------+
explain SELECT * FROM order_transmission_history where transmitter_type = 'FAX_1' AND transmission_id = '' AND (most_recent_status is null or (most_recent_status not in ('SENT', 'ERROR')));
+----+-------------+----------------------------+------+----------------------------------------------+----------------------------------------------+---------+-------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+----------------------------+------+----------------------------------------------+----------------------------------------------+---------+-------+--------+-------------+
| 1 | SIMPLE | order_transmission_history | ref | transmission_history_transmitter_status_date | transmission_history_transmitter_status_date | 2 | const | 394992 | Using where |
+----+-------------+----------------------------+------+----------------------------------------------+----------------------------------------------+---------+-------+--------+-------------+