组合快速MySQL查询的性能降低

时间:2016-01-23 20:57:18

标签: mysql performance

我有2个查询在单独运行时运行得非常快,但是当使用其中一个作为子查询进行组合时,性能会急剧下降。

快速查询我:

SELECT DISTINCT p.products_image, 
                p.products_subimage1, 
                pd.products_name, 
                p.products_quantity, 
                p.products_model, 
                p.products_ordered, 
                p.products_id, 
                p.products_price, 
                p.products_weight, 
                p.products_length, 
                p.products_width, 
                p.products_height, 
                p.products_tax_class_id, 
                p.products_status, 
                IF(s.status, s.specials_new_products_price, NULL)             AS 
                specials_new_products_price, 
                IF(s.status, s.specials_new_products_price, p.products_price) AS 
                final_price 
FROM   products p 
       LEFT JOIN specials s 
              ON p.products_id = s.products_id 
       LEFT JOIN products_to_categories p2c 
              ON p.products_id = p2c.products_id 
       LEFT JOIN products_description pd 
              ON p.products_id = pd.products_id 

       LEFT JOIN products_attributes pa 
              ON p.products_id = pa.products_id
       LEFT JOIN (SELECT `products_id`, 
               `products_stock_attributes`, 
               `products_stock_quantity` 
        FROM   products_stock 
        WHERE  `products_stock_quantity` > 0) t 
       ON t.products_id = p.products_id 
       WHERE t.products_stock_attributes IN ( '1-31', '1-25', '1-18', '1-7' ) 
       AND p.products_status = '1' 
       AND Date_sub(Curdate(), INTERVAL 30 day) >= p.products_date_added 
       AND pa.options_values_id IN ( 31, 25, 18, 7 ) 
GROUP  BY p.products_id 
HAVING Count(DISTINCT pa.options_values_id) = 4 
       AND Count(DISTINCT t.products_stock_attributes) = 4 

快速查询II:

SELECT sul2.* 
FROM   stock_update_log sul2 
INNER JOIN (SELECT products_id, 
     Max(date_time) AS maxDateTime 
FROM   stock_update_log 
WHERE  stock_qty_change > 1 
AND id > 154700 
   AND Date_sub(Curdate(), INTERVAL 360 day)<= From_unixtime(date_time) 
GROUP  BY products_id)gsul 
       ON sul2.products_id = gsul.products_id 
AND sul2.date_time = gsul.maxdatetime

组合查询(非常慢):

SELECT DISTINCT p.products_image, 
                p.products_subimage1, 
                pd.products_name, 
                p.products_quantity, 
                p.products_model, 
                p.products_ordered, 
                p.products_id, 
                p.products_price, 
                sul.date_time, 
                p.products_weight, 
                p.products_length, 
                p.products_width, 
                p.products_height, 
                p.products_tax_class_id, 
                p.products_status, 
                IF(s.status, s.specials_new_products_price, NULL)             AS 
                specials_new_products_price, 
                IF(s.status, s.specials_new_products_price, p.products_price) AS 
                final_price 
FROM   products p 
       LEFT JOIN specials s 
              ON p.products_id = s.products_id 
       LEFT JOIN products_to_categories p2c 
              ON p.products_id = p2c.products_id 
       LEFT JOIN products_description pd 
              ON p.products_id = pd.products_id 
       LEFT JOIN (SELECT sul2.* 
                  FROM   stock_update_log sul2 
                         INNER JOIN (SELECT products_id, 
                                            Max(date_time) AS maxDateTime 
                                     FROM   stock_update_log 
                                     WHERE  stock_qty_change > 1 
                                            AND id > 154700 
                                            AND Date_sub(Curdate(), 
                                                INTERVAL 360 day) 
                                                <= 
                                                From_unixtime(date_time) 
                                     GROUP  BY products_id)gsul 
                                 ON sul2.products_id = gsul.products_id 
                                    AND sul2.date_time = gsul.maxdatetime) sul 
              ON p.products_id = sul.products_id 
       LEFT JOIN products_attributes pa 
              ON p.products_id = pa.products_id
       LEFT JOIN (SELECT `products_id`, 
               `products_stock_attributes`, 
               `products_stock_quantity` 
        FROM   products_stock 
        WHERE  `products_stock_quantity` > 0) t 
       ON t.products_id = p.products_id 
       WHERE t.products_stock_attributes IN ( '1-31', '1-25', '1-18', '1-7' ) 
       AND p.products_status = '1' 
       AND Date_sub(Curdate(), INTERVAL 360 day) <= From_unixtime(sul.date_time) 
       AND Date_sub(Curdate(), INTERVAL 30 day) >= p.products_date_added 
       AND sul.id > 154700 
       AND sul.stock_qty_change > 1 
       AND pa.options_values_id IN ( 31, 25, 18, 7 ) 
GROUP  BY p.products_id 
HAVING Count(DISTINCT pa.options_values_id) = 4 
       AND Count(DISTINCT t.products_stock_attributes) = 4 
ORDER  BY sul.date_time DESC 

组合查询结果的解析: EXPLAIN of combined quries result

我一直在努力弄清楚为什么它以这种方式组合起来变得如此缓慢并试图重写组合查询无济于事,所以我在这里寻求专家的帮助。

导致它变得如此缓慢的原因是什么?我应该怎样做才能使它变得快速?

2 个答案:

答案 0 :(得分:1)

您正在连接7个表,这肯定会非常慢。我建议避免加入超过3张桌子。

你是否尝试过自己做两个查询,然后结合他们的结果?

答案 1 :(得分:1)

查询II感觉它有一百万行;生成的tmp表没有索引;它隐藏在LEFT JOIN后面。仍然优化器似乎足够聪明,从混乱开始,并为联合查询提供7904行。

你能避免LEFT吗?

然后代码进行,但最终必须从表扫描中检查18057行的所有行。优化器再次使用“连接缓冲区”做了一件聪明的事情。不过,7904 * 923 * 18057还有很多行要看。

这似乎是邪恶的部分:

   LEFT JOIN
     ( SELECT `products_id`, 
              `products_stock_attributes`, 
              `products_stock_quantity` 
        FROM    products_stock 
        WHERE  `products_stock_quantity` > 0
    ) t 
   ON t.products_id = p.products_id 

   WHERE t.products_stock_attributes IN ( '1-31', '1-25', '1-18', '1-7' ) 

所有这些似乎都是危险信号:

  • 不必要(?)LEFT;
  • “where where in ......”在子查询之外,当它们可以在里面时;
  • 您可以将HAVING Count(DISTINCT t.products_stock_attributes) = 4折叠到子查询中。