0

我遇到了一个奇怪的问题,在我们将数据库从 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 中对这个查询做了一个解释,我看到了这个:

用 MySQL 5.1.73 解释 放大版:http: //i.stack.imgur.com/c4ko0.jpg

当我在 5.6.23 中解释时,我看到了这个:

用 MySQL 5.6.23 解释 放大版:http: //i.stack.imgur.com/CgBtA.jpg

我可以看到,在这两种情况下,都对显示表进行了全面扫描(类型 ALL),但是还有其他我没有看到的导致 5.6 大幅减速的东西吗?

谢谢是

4

2 回答 2

0

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

show_rollups:索引(设备ID,包组ID,视频计数)

你有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).

于 2015-04-02T01:30:31.853 回答
0

很抱歉,我无法辨认我显示屏上的 EXPLAIN 屏幕抓取。

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

于 2015-04-02T00:35:43.760 回答