使用Explain调试慢速mySQL查询

时间:2016-05-10 17:01:44

标签: mysql explain mysql-slow-query-log

在我们的系统中发现了一个效率低下的查询。 content包含幻灯片的版本,这应该通过id选择幻灯片的最高版本。

SELECT `content`.*
FROM (`content`)
JOIN (
SELECT max(version) as `version` from `content`
WHERE `slide_id` = '16901'
group by `slide_id`
) c ON `c`.`version` = `content`.`version`;

EXPLAIN

+----+-------------+------------------+------------+--------+--------------------------------------------------------------------------------+------------------------------------+---------+-------+------+----------+--------------------------+
| id | select_type | table            | partitions | type   | possible_keys                                                                  | key                                | key_len | ref   | rows | filtered | Extra                    |
+----+-------------+------------------+------------+--------+--------------------------------------------------------------------------------+------------------------------------+---------+-------+------+----------+--------------------------+
|  1 | PRIMARY     | <derived2>       | NULL       | system | NULL                                                                           | NULL                               | NULL    | NULL  |    1 |   100.00 | NULL                     |
|  1 | PRIMARY     | content          | NULL       | ref    | PRIMARY,version                                                                | PRIMARY                            | 8       | const | 9703 |   100.00 | NULL                     |
|  2 | DERIVED     | content          | NULL       | ref    | PRIMARY,fk_content_slides_idx,thumbnail_asset_id,version,slide_id                      | fk_content_slides_idx              | 8       | const |    1 |   100.00 | Using where; Using index |
+----+-------------+------------------+------------+--------+--------------------------------------------------------------------------------+------------------------------------+---------+-------+------+----------+--------------------------+

一个很大的问题是它返回系统中的几乎所有幻灯片,因为外部查询不会按幻灯片ID过滤。添加后我得到......

SELECT `content`.* 
FROM (`content`) 
JOIN ( 
SELECT max(version) as `version` from `content`  
WHERE `slide_id` = '16901' group by `slide_id` 
) c ON `c`.`version` = `content`.`version` 
WHERE `slide_id` = '16901';

EXPLAIN

+----+-------------+------------------+------------+--------+--------------------------------------------------------------------------------+------------------------------------+---------+-------------+------+----------+--------------------------+
| id | select_type | table            | partitions | type   | possible_keys                                                                  | key                                | key_len | ref         | rows | filtered | Extra                    |
+----+-------------+------------------+------------+--------+--------------------------------------------------------------------------------+------------------------------------+---------+-------------+------+----------+--------------------------+
|  1 | PRIMARY     | <derived2>       | NULL       | system | NULL                                                                           | NULL                               | NULL    | NULL        |    1 |   100.00 | NULL                     |
|  1 | PRIMARY     | content          | NULL       | const  | PRIMARY,fk_content_slides_idx,version,slide_id                                 | PRIMARY                            | 16      | const,const |    1 |   100.00 | NULL                     |
|  2 | DERIVED     | content          | NULL       | ref    | PRIMARY,fk_content_slides_idx,thumbnail_asset_id,version,slide_id              | fk_content_slides_idx              | 8       | const       |    1 |   100.00 | Using where; Using index |
+----+-------------+------------------+------------+--------+--------------------------------------------------------------------------------+------------------------------------+---------+-------------+------+----------+--------------------------+

这样可以将行数减少到一个,但不会真正加快速度。

版本上有索引,slide_id和版本AND slide_id上​​的唯一键。

我还能做些什么来加快速度吗?

使用TOP LIMIT 1而不是Max?

2 个答案:

答案 0 :(得分:0)

只是一个建议我认为你应该通过slide_id避免使用该组,因为你只能通过一个slide_id过滤(16901)

SELECT `content`.* 
FROM (`content`) 
JOIN ( 
SELECT max(version) as `version` from `content`  
   WHERE `slide_id` = '16901' 
) c ON `c`.`version` = `content`.`version` 
WHERE `slide_id` = '16901';

答案 1 :(得分:0)

MySQL似乎采用索引(版本,slide_id)来加入表。你应该用

获得更好的结果
SELECT `content`.* 
FROM `content`
FORCE INDEX FOR JOIN (fk_content_slides_idx) 
join (    
    SELECT `slide_id`, max(version) as `version` from `content`  
    WHERE `slide_id` = '16901' group by `slide_id` 
) c ON `c`.`slide_id` = `content`.`slide_id` and `c`.`version` = `content`.`version` 

你需要一个将slide_id作为第一列的索引,我只是猜测是fk_content_slides_idx,如果没有,则需要另一个。

部分FORCE INDEX FOR JOIN (fk_content_slides_idx)只是为了强制执行它,你应该尝试,如果mysql自己接受而不强制(它应该)。

你可能会得到一个稍微好一点的索引(slide_id,版本)的结果,它取决于数据量(例如每个id的版本数量),如果你看到差异(但你不应该垃圾邮件索引,和你已经在这张桌子上有很多东西了,但是你可以试试它。)