-1

我有一个复杂的 SQL 语句,每次调用都需要(它用于日历视图)0.35sec。有 50 个用户和 2 周的日历项目,需要 245 秒,这太长了。我试图优化查询,但目前我不知道。

我对所有相关列都有索引。

SELECT 
      DISTINCT tax.ta_id, tax.a_id, ax.status, ax.kunden_id, 
      IF(ax.todo_from != '0000-00-00', DATE_FORMAT(ax.todo_from, '%d.%m'), 'k. day_date') todo_from, 
      IF(ax.todo_until != '0000-00-00', DATE_FORMAT(ax.todo_until, '%d.%m'), 'k. day_date') todo_until, 
      IF((SELECT taj.city FROM suborders taj WHERE taj.a_id = tax.a_id AND taj.order_type = 'BRING' ORDER BY pos_id ASC LIMIT 1) != '', CONCAT(IF((SELECT short_name FROM locations WHERE company_id = '100' AND h_id = tax.h_id) != '', (SELECT short_name FROM locations WHERE company_id = '100' AND h_id = tax.h_id),tax.city),'>', CONCAT((SELECT IF((SELECT short_name FROM locations WHERE company_id = '100' AND h_id = taj.h_id) != '', (SELECT short_name FROM locations WHERE company_id = '100' AND h_id = taj.h_id), taj.city) FROM suborders taj WHERE taj.a_id = tax.a_id AND taj.order_type = 'BRING' AND taj.pos_id >= tax.pos_id ORDER BY pos_id ASC LIMIT 1))), IF((SELECT short_name FROM locations WHERE company_id = '100' AND h_id = tax.h_id) != '', (SELECT short_name FROM locations WHERE company_id = '100' AND h_id = tax.h_id),tax.city)) as city, 
  tax.user_id, tax.day_date, tax.pos_gesamt_id, '4' as class_type 
    FROM 
      suborders tax INNER JOIN orders ax ON (ax.a_id = tax.a_id) 
    WHERE 
      tax.order_type = 'TAKE' AND tax.user_id = '140' AND (tax.day_date = '2013-04-16' AND '2013-04-16' = (SELECT taj.day_date FROM suborders taj WHERE taj.a_id = tax.a_id AND taj.user_id = '140' AND taj.day_date = '2013-04-16' AND taj.user_id = '140' AND taj.order_type = 'BRING' ORDER BY pos_gesamt_id ASC)) AND tax.company_id = '100' GROUP BY tax.ta_id

也许你给我一些建议。

4

1 回答 1

1

第一次尝试重写(未经测试,因为我不知道你的表格布局): -

SELECT DISTINCT tax.ta_id, tax.a_id, ax.status, ax.kunden_id, 
      IF(ax.todo_from != '0000-00-00', DATE_FORMAT(ax.todo_from, '%d.%m'), 'k. day_date') todo_from, 
      IF(ax.todo_until != '0000-00-00', DATE_FORMAT(ax.todo_until, '%d.%m'), 'k. day_date') todo_until, 
      IF((SELECT taj.city FROM suborders taj WHERE taj.a_id = tax.a_id AND taj.order_type = 'BRING' ORDER BY pos_id ASC LIMIT 1) != '', 
        CONCAT(
            IF(Sub2.short_name != '', Sub2.short_name,tax.city),
            '>', 
            CONCAT((
                SELECT IF(  Sub3.short_name != '', 
                            Sub3.short_name, 
                            taj.city) 
                FROM suborders taj 
                LEFT OUTER JOIN (SELECT h_id, short_name FROM locations WHERE company_id = '100') Sub3 ON Sub3.h_id = taj.h_id
                WHERE taj.a_id = tax.a_id 
                AND taj.order_type = 'BRING' 
                AND taj.pos_id >= tax.pos_id 
                ORDER BY pos_id 
                ASC LIMIT 1))), 
            IF(Sub2.short_name != '', Sub2.short_name, tax.city)) as city, 
  tax.user_id, tax.day_date, tax.pos_gesamt_id, '4' as class_type 
FROM suborders tax 
INNER JOIN orders ax ON (ax.a_id = tax.a_id) 
INNER JOIN (
    SELECT DISTINCT a_id 
    FROM suborders taj 
    WHERE taj.user_id = '140' 
    AND taj.day_date = '2013-04-16' 
    AND taj.user_id = '140' 
    AND taj.order_type = 'BRING' 
) Sub4 ON Sub4.a_id = tax.a_id 
LEFT OUTER JOIN (SELECT h_id, short_name FROM locations WHERE company_id = '100') Sub2 ON Sub2.h_id = tax.h_id
 WHERE tax.order_type = 'TAKE' 
 AND tax.user_id = '140' 
 AND tax.day_date = '2013-04-16' 
 AND tax.company_id = '100' 
 GROUP BY tax.ta_id

在这个阶段,我不确定我是否可以更进一步,因为这真的让我很困惑,事情是如何组合在一起的。

问题是你有很多相关的子查询,而且速度很慢(即,一个依赖于查询外部字段的子查询——因此 MySQL 必须为每一行执行该子查询)。我已经删除了一些我可以解决的问题,并将它们替换为子查询的连接(使用这些 MySQL 可以执行一次子查询,然后将结果连接到主查询)

但是,当您在查询中关联了子查询,并且在不同的表和子查询之间重用了别名时,这是非常令人困惑的。

如果我能弄清楚它的用途,那么查询的性能可以大大提高!

此外,您还在主查询上使用了 GROUP BY,而没有任何聚合列。这有时会代替 DISTINCT,但您也有 DISTINCT。

于 2013-04-17T16:53:41.997 回答