相同的MySQL查询在5.6中比在5.1中运行慢得多

时间:2015-04-02 00:24:20

标签: mysql sql performance mysql-5.6 mysql-5.1

我遇到一个奇怪的问题,在我们将数据库从MySQL 5.1.73升级到5.6.23之后,我们所拥有的这个特定MySQL查询的运行速度将快50倍。

这是SQL查询:

SELECT `companies`.*
FROM   `companies` 
   LEFT OUTER JOIN `company_texts` 
                ON `company_texts`.`company_id` = `companies`.`id` 
                   AND `company_texts`.`language` = 'en' 
                   AND `company_texts`.`region` = 'US' 
   INNER JOIN show_texts 
           ON show_texts.company_id = companies.id 
              AND `show_texts`.`is_deleted` = 0 
              AND `show_texts`.`language` = 'en' 
              AND `show_texts`.`region` = 'US' 
   INNER JOIN show_region_counts 
           ON show_region_counts.show_id = show_texts.show_id 
              AND show_region_counts.region = 'US' 
    WHERE  ( ( `companies`.`id` NOT IN ( '77', '26' ) ) 
     AND ( `company_texts`.is_deleted = 0 ) 
     AND `companies`.id IN (
        SELECT DISTINCT show_texts.company_id AS 
                                id 
                FROM   shows 
                       INNER JOIN `show_rollups` 
                               ON 
                                `show_rollups`.`show_id` = `shows`.`id` 
                                    AND ( `show_rollups`.`device_id` = 3 ) 
                                    AND ( `show_rollups`.`package_group_id` = 2 ) 
                                    AND ( `show_rollups`.`videos_count` > 0 ) 
                                LEFT OUTER JOIN `show_texts` ON 
                                    `show_texts`.`show_id` = `shows`.`id` 
                                        AND 
                                    `show_texts`.`is_deleted` = 0 
                                       AND 
                                    `show_texts`.`language` = 'en' 
                                       AND 
                                    `show_texts`.`region` = 'US'
                                       AND
                                    shows.is_browseable = 1 
                                    AND 
                                    show_texts.show_id IS NOT NULL 
                                    AND ( 
                                        `show_rollups`.`episodes_count` > 0 
                                        OR `show_rollups`.`clips_count` > 0 
                                        OR `show_rollups`.`games_count` > 0 
                                        )   


     ) ) 
    GROUP  BY companies.id 
    ORDER  BY Sum(show_region_counts.view_count) DESC 
    LIMIT  30 offset 30; 

现在问题是当我在升级之前在MySQL 5.1.73中运行此查询时,查询只需要大约1.5秒,但在升级到5.6.23之后,现在可能需要1分钟。

所以我在5.1.73中对这个查询进行了解析,我看到了这个:

EXPLAIN with MySQL 5.1.73 放大版本:http://i.stack.imgur.com/c4ko0.jpg

当我在5.6.23中进行EXPLAIN时,我看到了这个:

EXPLAIN with MySQL 5.6.23 放大版本:http://i.stack.imgur.com/CgBtA.jpg

我可以看到,在这两种情况下,显示表都有一个完整的扫描(类型ALL),但还有其他我没有看到的导致5.6的大幅减速吗?

由于 IS

2 个答案:

答案 0 :(得分:0)

对不起,但我无法在显示屏上显示EXPLAIN屏幕。

没有它,我想知道表的模式是否发生了变化,或者数据库引擎是否发生了变化。特别是,companies.id似乎被用作主键。

答案 1 :(得分:0)

请提供SHOW CREATE TABLE。没有它,我猜你错过了这个理想的指数:

show_rollups:INDEX(device_id,package_group_id,videos_count)

你有LEFT OUTER JOIN show_texts ... ON ... show_texts.show_id IS NOT NULL。这可能错误有两个原因:(a)如果您不是在寻找LEFT,请不要使用NULL,以及(b)NULL测试应该在缺少的WHERE子句中,而不是在ON子句中。

摆脱IN ( SELECT ... ) 可以在两台机器上帮助它:

SELECT  c.*
    FROM  
      ( SELECT  DISTINCT st.company_id AS id
            FROM  shows
            INNER JOIN  `show_rollups` AS sr ON sr.`show_id` = `shows`.`id`
              AND  ( sr.`device_id` = 3 )
              AND  ( sr.`package_group_id` = 2 )
              AND  ( sr.`videos_count` > 0 )
            LEFT OUTER JOIN  `show_texts` AS st ON st.`show_id` = `shows`.`id`
              AND  st.`is_deleted` = 0
              AND  st.`language` = 'en'
              AND  st.`region` = 'US'
              AND  shows.is_browseable = 1
              AND  st.show_id IS NOT NULL
              AND  ( sr.`episodes_count` > 0
                      OR  sr.`clips_count` > 0
                      OR  sr.`games_count` > 0 ) 
      ) AS x
    JOIN  `companies` AS c ON x.id = c.id
    LEFT OUTER JOIN  `company_texts` AS ct ON ct.`company_id` = c.`id`
          AND  ct.`language` = 'en'
          AND  ct.`region` = 'US'
    INNER JOIN  show_texts AS st ON st.company_id = c.id
          AND  st.`is_deleted` = 0
          AND  st.`language` = 'en'
          AND  st.`region` = 'US'
    INNER JOIN  show_region_counts AS src ON src.show_id = st.show_id
          AND  src.region = 'US'
    WHERE  ( c.`id` NOT IN ( '77', '26' ) )
      AND  ( ct.is_deleted = 0 )
    GROUP BY  c.id
    ORDER BY  Sum(src.view_count) DESC
    LIMIT  30 offset 30; 

JOIN可能会混淆Sum(src.view_count)中的计算。