MySQL将多个子查询优化为嵌套内连接?

时间:2013-07-10 07:38:04

标签: mysql sql subquery query-optimization

我有3张桌子;合同,经销商和用户。

用户有许多经销商和经销商有很多合同,但合同并不直接与用户相关联。

我正在尝试构建一份报告,让我每月按用户分组过去12个月完成的合同。

到目前为止,我已经构建了一个多子查询,速度很慢:SQL Fiddle

SELECT *,
  ( SELECT count(*) FROM contracts
     WHERE
       dealer_id IN
         ( SELECT id FROM dealers WHERE user_id = User.id )
       AND status = 'Paid'
       AND completion_date BETWEEN
         '2012-08-01 00:00:00' AND '2012-08-31 23:59:59'
  ) AS Aug_2012,

  ( SELECT count(*) FROM contracts
     WHERE
       dealer_id IN
         ( SELECT id FROM dealers WHERE user_id = User.id )
       AND status = 'Paid'
       AND completion_date BETWEEN
         '2012-09-01 00:00:00' AND '2012-09-30 23:59:59'
  ) AS Sep_2012
FROM users AS User
  WHERE
    id IN( SELECT user_id FROM dealers WHERE active = 1 AND user_id IS NOT NULL GROUP BY user_id )
    AND id != 1
  ORDER BY User.name ASC

而不是每月选择的子查询,我想使用这样的东西:

COUNT(*) as last_12_months,
SUM(case when MONTH(completion_date) = 8 then 1 else 0 end) as Aug_2012,
SUM(case when MONTH(completion_date) = 9 then 1 else 0 end) as Sep_2012,
etc.

由于我要返回多个列,我必须对其进行重组,但我不确定如何。如果我使用INNER JOIN,我可以加入哪个条款?

以下是基于米哈伊尔答案的最终查询:

SELECT
User.*,
SUM(case when MONTH(completion_date) = 8 then 1 else 0 end) AS Aug_2012,
SUM(case when MONTH(completion_date) = 9 then 1 else 0 end) AS Sep_2012,
SUM(case when MONTH(completion_date) = 10 then 1 else 0 end) AS Oct_2012,
SUM(case when MONTH(completion_date) = 11 then 1 else 0 end) AS Nov_2012,
SUM(case when MONTH(completion_date) = 12 then 1 else 0 end) AS Dec_2012,
SUM(case when MONTH(completion_date) = 1 then 1 else 0 end) AS Jan_2013,
SUM(case when MONTH(completion_date) = 2 then 1 else 0 end) AS Feb_2013,
SUM(case when MONTH(completion_date) = 3 then 1 else 0 end) AS Mar_2013,
SUM(case when MONTH(completion_date) = 4 then 1 else 0 end) AS Apr_2013,
SUM(case when MONTH(completion_date) = 5 then 1 else 0 end) AS May_2013,
SUM(case when MONTH(completion_date) = 6 then 1 else 0 end) AS Jun_2013,
SUM(case when MONTH(completion_date) = 7 then 1 else 0 end) AS Jul_2013,
SUM(case when completion_date BETWEEN '2012-08-01 00:00:00' AND '2013-07-31 23:59:59' then 1 else 0 end) as last_12_months
FROM users AS User
LEFT OUTER JOIN 
( 
    SELECT id, user_id FROM dealers 
    WHERE active = 1 AND user_id IS NOT NULL
) AS Dealer ON User.id = Dealer.user_id
LEFT OUTER JOIN
(
    SELECT completion_date, status, dealer_id FROM contracts
    WHERE completion_date BETWEEN '2012-08-01 00:00:00' AND '2013-07-31 23:59:59' AND status = 'Paid' AND cancelled = 0
) AS Contract on Dealer.id = Contract.dealer_id
WHERE
    User.id IN 
    (
        SELECT user_id FROM dealers
        WHERE active = 1 AND user_id IS NOT NULL
        GROUP BY user_id
    )
GROUP BY
    User.id order by User.name asc

这大约快4倍。

1 个答案:

答案 0 :(得分:2)

试试这个:

select
    User.id, User.name,
    sum(case when MONTH(completion_date) = 8 and Year(completion_date)=2012 then 1 else 0 end) as Aug_2012,
    sum(case when MONTH(completion_date) = 9 and Year(completion_date)=2012 then 1 else 0 end) as Sep_2012,
    sum(case when MONTH(completion_date) = 10 and Year(completion_date)=2012 then 1 else 0 end) as Oct_2012,
    sum(case when MONTH(completion_date) = 11 and Year(completion_date)=2012 then 1 else 0 end) as Nov_2012,
    sum(case when MONTH(completion_date) = 12 and Year(completion_date)=2012 then 1 else 0 end) as Dec_2012,
    sum(case when MONTH(completion_date) = 1 and Year(completion_date)=2013 then 1 else 0 end) as Jan_2012,
    sum(case when MONTH(completion_date) = 2 and Year(completion_date)=2013 then 1 else 0 end) as Feb_2012,
    sum(case when MONTH(completion_date) = 3 and Year(completion_date)=2013 then 1 else 0 end) as Mar_2012,
    sum(case when MONTH(completion_date) = 4 and Year(completion_date)=2013 then 1 else 0 end) as Apr_2012,
    sum(case when MONTH(completion_date) = 5 and Year(completion_date)=2013 then 1 else 0 end) as May_2012,
    sum(case when MONTH(completion_date) = 6 and Year(completion_date)=2013 then 1 else 0 end) as Jun_2012,
    sum(case when MONTH(completion_date) = 7 and Year(completion_date)=2013 then 1 else 0 end) as Jul_2012
from users AS User
left outer join dealers on
    User.id=dealers.user_id
left outer join contracts on
    dealers.id=contracts.dealer_id
group by
    User.id,
    contracts.status
having
    contracts.status='Paid'
order by
    User.name asc;