使用子查询,IF和联接优化MySQL查询

时间:2013-04-17 16:00:43

标签: mysql

我有一个复杂的SQL Statemant,需要每次调用(它用于日历视图) 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

也许你给我一些推荐。

1 个答案:

答案 0 :(得分: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。