使用 Explain 调试慢 mySQL 查询

Debugging Slow mySQL query with Explain

在我们的系统中发现了一个低效的查询。 content 包含幻灯片的版本,这应该是 select 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`;

解释

+----+-------------+------------------+------------+--------+--------------------------------------------------------------------------------+------------------------------------+---------+-------+------+----------+--------------------------+
| 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 |
+----+-------------+------------------+------------+--------+--------------------------------------------------------------------------------+------------------------------------+---------+-------+------+----------+--------------------------+

一个大问题是它 returns 系统中几乎所有的幻灯片,因为外部查询不按幻灯片 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';

解释

+----+-------------+------------------+------------+--------+--------------------------------------------------------------------------------+------------------------------------+---------+-------------+------+----------+--------------------------+
| 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 insetead of Max ?

只是一个建议,我认为您应该避免按 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';

MySQL 似乎需要一个索引(版本,slide_id)来加入 table。

你应该会得到更好的结果
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,版本)可能会得到更好的结果,如果您看到差异(但您不应该是垃圾索引,你已经有了很多关于这个的东西 table,但你可以试试看。)