这种慢速MySQL查询的最佳指标是什么? (InnoDB的)

时间:2017-07-27 18:52:45

标签: mysql sql performance innodb mysql-slow-query-log

此类MySQL-Query目前非常慢。

加速它的最佳指数是什么? (InnoDB的)

SELECT item_id, 
       Group_concat(storage_nr SEPARATOR ',') AS storage_nr, 
       Group_concat(condition SEPARATOR ',') AS condition, 
       Group_concat(number SEPARATOR ',') AS number, 
       Group_concat(price SEPARATOR ',') AS price, 
       last_calc 
FROM   items
WHERE  number > 0 
       AND bottomlimit IS NOT NULL 
       AND condition IN (1, 2, 3) 
       AND ( price_date IS NULL 
              OR price_date < Date_sub(Now(), INTERVAL 1 hour) ) 
       AND ( NOT ( price = bottomlimit 
                   AND pricebefore = bottomlimit 
                   AND pricebefore2 = bottomlimit ) 
              OR price IS NULL 
              OR pricebefore IS NULL 
              OR pricebefore2 IS NULL 
              OR Date(price_date) <> Curdate() ) 
GROUP  BY item_id 
ORDER  BY last_calc 
LIMIT  20

提前多多感谢!

祝你好运!

3 个答案:

答案 0 :(得分:0)

我倾向于同意戈登的评论,但你可以尝试的一件事是条件聚合。 (这可能是处理更多数据并丢弃不需要的内容比过滤所需内容更快的特殊场景之一,尤其是因为看起来某些OR条件往往会破坏MySQL中的索引使用。) / p>

与此相似的 可能帮助。

SELECT item_id
       , Group_concat(
           IF(condition IN (1, 2, 3) 
               AND ( price_date IS NULL OR price_date < Date_sub(Now(), INTERVAL 1 hour) ) 
               AND ( NOT ( price = bottomlimit AND pricebefore = bottomlimit AND pricebefore2 = bottomlimit ) 
                     OR price IS NULL 
                     OR pricebefore IS NULL 
                     OR pricebefore2 IS NULL 
                     OR Date(price_date) <> Curdate() 
                   ) 
              , storage_nr, NULL)
           SEPARATOR ','
         ) AS storage_nr 
       , [etc...]
FROM   items
WHERE  number > 0 AND bottomlimit IS NOT NULL 
GROUP  BY item_id 
HAVING storage_nr IS NOT NULL
ORDER  BY last_calc 
LIMIT  20

答案 1 :(得分:0)

您只希望使用indexed来在更简单和索引查询的UNION中分解您的复杂查询。 首先,您可以使用FROM [子查询]语法

SELECT item_id, 
       Group_concat(storage_nr SEPARATOR ',') AS storage_nr, 
       Group_concat(condition SEPARATOR ',') AS condition, 
       Group_concat(number SEPARATOR ',') AS number, 
       Group_concat(price SEPARATOR ',') AS price, 
       last_calc 
FROM   

SUBQUERY

AS items

GROUP  BY item_id 
ORDER  BY last_calc 
LIMIT  20

这可能是你的子查询:

SELECT *
FROM items
WHERE number > 0 
AND bottomlimit IS NOT NULL 
AND condition IN (1, 2, 3) 
AND price_date IS NULL 
AND ( NOT ( price = bottomlimit 
                   AND pricebefore = bottomlimit 
                   AND pricebefore2 = bottomlimit ) 
              OR price IS NULL 
              OR pricebefore IS NULL 
              OR pricebefore2 IS NULL 
              OR Date(price_date) <> Curdate() ) 

UNION ALL

SELECT *
FROM items
WHERE number > 0 
AND bottomlimit IS NOT NULL 
AND condition IN (1, 2, 3) 
AND price_date < Date_sub(Now(), INTERVAL 1 hour)
AND ( NOT ( price = bottomlimit 
                   AND pricebefore = bottomlimit 
                   AND pricebefore2 = bottomlimit ) 
              OR price IS NULL 
              OR pricebefore IS NULL 
              OR pricebefore2 IS NULL 
              OR Date(price_date) <> Curdate() ) 

定义以下索引

- condition
- price_date
- bottomLimit

请给我一些结果反馈。感谢。

答案 2 :(得分:0)

感谢您的帮助!

对我来说最好的解决方案是在测试所有内容后将表格转换为MyISAM。

通过此更改,我可以将查询速度提高3到5倍 - 从大约12秒到小于3秒。