mysql group by using filesort优化

Posted 星朝

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql group by using filesort优化相关的知识,希望对你有一定的参考价值。

  1. 原join 连接语句

    SELECT 
    SUM(video_flowers.number) AS num,
    video_flowers.flower_id,
    flowers.title,
    flowers.image
    FROM
    `video_flowers`
    JOIN
    `flowers` ON `video_flowers`.`flower_id` = `flowers`.`id`
    JOIN
    `video_posts` ON `video_flowers`.`video_post_id` = `video_posts`.`id`
    WHERE
    `video_posts`.`user_id` = 36
    GROUP BY `video_flowers`.`flower_id`

可以优化成

SELECT 
vf.num, flowers.title, flowers.image
FROM
`flowers`

join
(SELECT
SUM(video_flowers.number) AS num, video_flowers.flower_id, video_flowers.video_post_id FROM
video_flowers
GROUP BY `video_flowers`.`flower_id`) AS vf ON `vf`.`flower_id` = `flowers`.`id`
join `video_posts` on `video_posts`.`id` = vf.`video_post_id`
where video_posts.user_id = 36;

这样就没有using filesort 和using temporary


























以上是关于mysql group by using filesort优化的主要内容,如果未能解决你的问题,请参考以下文章

在 MySql Godaddy VPS Cpanel 中使用 sql_mode=only_full_group_by 获取 MYSQL 错误

mysql8 的 group by错误

mysql 可以group by 两个字段吗

mysql group by 问题

mysql报错:Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggre

mysql group by问题