gpt4 book ai didi

mysql - 使用Explain调试缓慢的mySQL查询

转载 作者:行者123 更新时间:2023-11-29 21:01:57 25 4
gpt4 key购买 nike

在我们的系统中发现了低效的查询。 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`;

解释一下

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

解释一下

+----+-------------+------------------+------------+--------+--------------------------------------------------------------------------------+------------------------------------+---------+-------------+------+----------+--------------------------+
| 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 都有索引,版本和 Slide_id 上有唯一键。

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

使用 TOP LIMIT 1 代替 Max ?

最佳答案

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 的版本数)(但您不应该垃圾邮件索引,并且这张 table 上已经有很多东西了,但你可以尝试一下。)

关于mysql - 使用Explain调试缓慢的mySQL查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37144743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com