如何减少这个mysql查询执行时间?

时间:2018-03-09 12:46:38

标签: mysql

父表,

的MySQL> desc sattool_testing;

+------------------+--------------+------+-----+---------+----------------+
| Field            | Type         | Null | Key | Default | Extra          |
+------------------+--------------+------+-----+---------+----------------+
| id               | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| circuit_id       | varchar(30)  | NO   |     | NULL    |                |
| coff_id          | varchar(100) | NO   |     | NULL    |                |
| result           | int(11)      | YES  |     | NULL    |                |
| result_details   | text         | NO   |     | NULL    |                |
| details          | longtext     | NO   |     | NULL    |                |
| reverse_response | varchar(100) | NO   |     | NULL    |                |
| start_date       | datetime     | NO   |     | NULL    |                |
| end_date         | datetime     | YES  |     | NULL    |                |
| Modules          | varchar(100) | YES  |     | NULL    |                |
| rehit            | datetime     | YES  |     | NULL    |                |
| isAuto           | tinyint(1)   | NO   |     | 0       |                |
+------------------+--------------+------+-----+---------+----------------+

12行(0.00秒)

第一个儿童表,

的MySQL> desc sattool_desc;

+--------------------+--------------+------+-----+---------+----------------+
| Field              | Type         | Null | Key | Default | Extra          |
+--------------------+--------------+------+-----+---------+----------------+
| sattool_desc_id    | bigint(20)   | NO   | PRI | NULL    | auto_increment |
| ceinterface        | varchar(50)  | YES  |     | NULL    |                |
| cehostname         | varchar(50)  | YES  |     | NULL    |                |
| vprnno             | varchar(50)  | YES  |     | NULL    |                |
| policyname         | varchar(50)  | YES  |     | NULL    |                |
| cosno              | varchar(10)  | YES  |     | NULL    |                |
| peipaddress        | varchar(17)  | YES  |     | NULL    |                |
| router_type        | varchar(5)   | YES  |     | NULL    |                |
| sattool_testing_id | bigint(20)   | YES  |     | NULL    |                |
| epipeid            | varchar(20)  | YES  |     | NULL    |                |
| cerouter           | varchar(10)  | YES  |     | NULL    |                |
| service_type       | varchar(10)  | YES  |     | NULL    |                |
| scope_of_manage    | varchar(50)  | YES  |     | NULL    |                |
| service_name       | varchar(10)  | YES  |     | NULL    |                |
| ce_vrf_name        | varchar(150) | YES  |     | NULL    |                |
+--------------------+--------------+------+-----+---------+----------------+

15行(0.01秒)

第二个子表(包含针对一个父表记录的多个记录), MySQL的> desc sattool_error_log;

+----------------+--------------+------+-----+-------------------+-----------------------------+
| Field          | Type         | Null | Key | Default           | Extra                       |
+----------------+--------------+------+-----+-------------------+-----------------------------+
| id             | bigint(20)   | NO   | PRI | NULL              | auto_increment              |
| service_id     | varchar(30)  | NO   |     | NULL              |                             |
| coff_id        | varchar(50)  | NO   |     | NULL              |                             |
| error_name     | varchar(100) | NO   |     | NULL              |                             |
| error_desc     | text         | NO   |     | NULL              |                             |
| error_occurred | timestamp    | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| error_type     | int(11)      | YES  |     | NULL              |                             |
| error_code     | varchar(10)  | YES  |     | NULL              |                             |
| sattool_id     | bigint(20)   | YES  |     | 0                 |                             |
| module_no      | int(11)      | YES  |     | 0                 |                             |
+----------------+--------------+------+-----+-------------------+-----------------------------+

10行(0.00秒)

SQL查询: -

SELECT GROUP_CONCAT(sattool_error_log.error_desc) AS err_desc, GROUP_CONCAT(sattool_error_log.error_type) AS err_type, sattool_testing.*, sattool_desc.service_type, sattool_desc.service_name, sattool_desc.scope_of_manage 
FROM sattool_testing 
    LEFT JOIN sattool_desc ON sattool_testing.id = sattool_desc.sattool_testing_id 
    LEFT JOIN sattool_error_log ON sattool_testing.id = sattool_error_log.sattool_id 
WHERE sattool_testing.isAuto = 1 GROUP BY sattool_testing.id ORDER BY sattool_testing.id DESC limit 100

当前执行时间为(22.76秒)

解释查询: -

+----+-------------+-------------------+------+---------------+------+---------+------+------+----------------------------------------------+
| id | select_type | table             | type | possible_keys | key  | key_len | ref  | rows | Extra                                        |
+----+-------------+-------------------+------+---------------+------+---------+------+------+----------------------------------------------+
|  1 | SIMPLE      | sattool_testing   | ALL  | NULL          | NULL | NULL    | NULL | 3578 | Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | sattool_desc      | ALL  | NULL          | NULL | NULL    | NULL | 4009 |                                              |
|  1 | SIMPLE      | sattool_error_log | ALL  | NULL          | NULL | NULL    | NULL | 8904 |                                              |
+----+-------------+-------------------+------+---------------+------+---------+------+------+----------------------------------------------+

3行(0.00秒)

我希望将速度提高到上面的查询执行速度。

请给我一些建议。

1 个答案:

答案 0 :(得分:0)

感谢!!!

我已在join / where子句中使用的列添加了索引。

喜欢: -

ALTER TABLE sattool_desc ADD INDEX sattool_testing_id (sattool_testing_id);
ALTER TABLE sattool_error_log ADD INDEX sattool_id (sattool_id);
ALTER TABLE sattool_testing ADD INDEX isAuto (isAuto);

现在执行时间为100行(0.00秒)。

再次感谢。