一些背景:
我们仅对MySQL数据库具有SELECT
特权,并且需要计算每个用户在最近的1、2,...,45天内花了多少钱。该查询是LEFT JOIN
的庞大集合,我受命对其进行优化。请注意,为简化代码,我用注释删除了大部分Round
和LEFT JOIN
。
代码:
SELECT m.username AS 'Username',
# Actually we have 45 instead of 2 Round()
Round(y2.spend, 2) AS '2',
Round(y1.spend, 2) AS '1'
FROM fund_m AS fm
LEFT JOIN member AS m
ON fm.user_id = m.id
LEFT JOIN employee AS e
ON m.account_manager = e.id
# Again we have 45 instead of 2 LEFT JOIN following
LEFT JOIN (SELECT fm.user_id AS id,
Sum(fm.amount_changed) *- 1 AS 'spend'
FROM fund_m AS fm
WHERE fm.arrival_date = Subdate(CURRENT_DATE, 1)
AND fm.type = 'impressions'
GROUP BY fm.user_id) AS y1
ON fm.user_id = y1.id
LEFT JOIN (SELECT fm.user_id AS id,
Sum(fm.amount_changed) *- 1 AS 'spend'
FROM fund_m AS fm
WHERE fm.arrival_date = Subdate(CURRENT_DATE, 2)
AND fm.type = 'impressions'
GROUP BY fm.user_id) AS y2
ON fm.user_id = y2.id
WHERE fm.arrival_date BETWEEN Subdate(CURRENT_DATE, 45) AND Subdate(
CURRENT_DATE, 1)
AND fm.type = 'impressions'
GROUP BY m.username;
表fm
的结构如下:
每个用户都有一个user_id
和arrival_date
列,因此代码检查到达日期是否为CURRENT_DATE
前45天,1、2 ... ...,然后是{{1 }} Sum
基于此。该脚本还将汇总显示为列。因此,每个amount_charged
会有45列求和结果。
这是我使用SQL的第一天,经过研究,我得出了结论:
1)它包含45个user_id
和Round()
,并且对LOOP或其他东西大喊。但是,我仅可以在存储过程中使用LEFT JOIN
,而我不能这样做。下一个想法是查看是否可以创建一个临时表来保存1〜45之间的序列。但是,我在SO上搜索了一段时间,无法找到真正有用的帖子。
2)是否可以在不使用LOOP
的情况下生成45列?因为我知道LEFT JOIN
需要大量资源,而45(实际上是47)LEFT JOIN
似乎并不是一个好主意(其中45个是自我LEFT JOIN
)。但是我不知道如何优化它。
已更新
想了一下,对不起,我没有工作区了,所以无法测试。只需阅读有关LEFT JOIN
SELECT
CASE
WHEN
ELSE
的信息。因此,也许我可以使用END
来过滤日期(例如,CASE
CASE
现在和WHEN
之间的差是1,2,...,45天)。但是我仍然需要生成45列...
答案 0 :(得分:1)
您可以做的是首先将使用user_id分组的fund_m表上的MAX使用MAX将它们分组为45列,然后将其加入成员。这样可以避免多次连接到同一张表。
例如:
SELECT m.username, ROUND('spend1',2) '1', ROUND('spend2',2) '2', .... ROUND('spend45',2) '45'
FROM member AS m
LEFT OUTER JOIN
(
SELECT fm.user_id AS id,
SUM(CASE WHEN fm.arrival_date = Subdate(CURRENT_DATE, 1)
THEN fm.amount_changed *- 1 END) AS 'spend1',
SUM(CASE WHEN fm.arrival_date = Subdate(CURRENT_DATE, 2)
THEN fm.amount_changed *- 1 END) AS 'spend2',
....,
SUM(CASE WHEN fm.arrival_date = Subdate(CURRENT_DATE, 44)
THEN fm.amount_changed *- 1 END) AS 'spend44',
SUM(CASE WHEN fm.arrival_date = Subdate(CURRENT_DATE, 45)
THEN fm.amount_changed *- 1 END) AS 'spend45'
FROM fund_m AS fm
WHERE fm.arrival_date BETWEEN Subdate(CURRENT_DATE, 45) AND Subdate(CURRENT_DATE, 1)
AND fm.type = 'impressions'
GROUP BY fm.user_id
) X ON fm.user_id = X.id
LEFT JOIN employee AS e ON m.account_manager = e.id
答案 1 :(得分:1)
这应该提供您想要的结果:
SELECT
m.username,
Round(SUM(IF(SubDate(CURRENT_DATE,1) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `1`,
Round(SUM(IF(SubDate(CURRENT_DATE,2) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `2`,
Round(SUM(IF(SubDate(CURRENT_DATE,3) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `3`,
Round(SUM(IF(SubDate(CURRENT_DATE,4) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `4`,
Round(SUM(IF(SubDate(CURRENT_DATE,5) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `5`,
Round(SUM(IF(SubDate(CURRENT_DATE,6) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `6`,
Round(SUM(IF(SubDate(CURRENT_DATE,7) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `7`,
Round(SUM(IF(SubDate(CURRENT_DATE,8) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `8`,
Round(SUM(IF(SubDate(CURRENT_DATE,9) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `9`,
Round(SUM(IF(SubDate(CURRENT_DATE,10) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `10`,
Round(SUM(IF(SubDate(CURRENT_DATE,11) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `11`,
Round(SUM(IF(SubDate(CURRENT_DATE,12) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `12`,
Round(SUM(IF(SubDate(CURRENT_DATE,13) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `13`,
Round(SUM(IF(SubDate(CURRENT_DATE,14) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `14`,
Round(SUM(IF(SubDate(CURRENT_DATE,15) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `15`,
Round(SUM(IF(SubDate(CURRENT_DATE,16) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `16`,
Round(SUM(IF(SubDate(CURRENT_DATE,17) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `17`,
Round(SUM(IF(SubDate(CURRENT_DATE,18) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `18`,
Round(SUM(IF(SubDate(CURRENT_DATE,19) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `19`,
Round(SUM(IF(SubDate(CURRENT_DATE,20) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `20`,
Round(SUM(IF(SubDate(CURRENT_DATE,21) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `21`,
Round(SUM(IF(SubDate(CURRENT_DATE,22) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `22`,
Round(SUM(IF(SubDate(CURRENT_DATE,23) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `23`,
Round(SUM(IF(SubDate(CURRENT_DATE,24) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `24`,
Round(SUM(IF(SubDate(CURRENT_DATE,25) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `25`,
Round(SUM(IF(SubDate(CURRENT_DATE,26) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `26`,
Round(SUM(IF(SubDate(CURRENT_DATE,27) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `27`,
Round(SUM(IF(SubDate(CURRENT_DATE,28) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `28`,
Round(SUM(IF(SubDate(CURRENT_DATE,29) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `29`,
Round(SUM(IF(SubDate(CURRENT_DATE,30) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `30`,
Round(SUM(IF(SubDate(CURRENT_DATE,31) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `31`,
Round(SUM(IF(SubDate(CURRENT_DATE,32) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `32`,
Round(SUM(IF(SubDate(CURRENT_DATE,33) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `33`,
Round(SUM(IF(SubDate(CURRENT_DATE,34) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `34`,
Round(SUM(IF(SubDate(CURRENT_DATE,35) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `35`,
Round(SUM(IF(SubDate(CURRENT_DATE,36) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `36`,
Round(SUM(IF(SubDate(CURRENT_DATE,37) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `37`,
Round(SUM(IF(SubDate(CURRENT_DATE,38) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `38`,
Round(SUM(IF(SubDate(CURRENT_DATE,39) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `39`,
Round(SUM(IF(SubDate(CURRENT_DATE,40) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `40`,
Round(SUM(IF(SubDate(CURRENT_DATE,41) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `41`,
Round(SUM(IF(SubDate(CURRENT_DATE,42) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `42`,
Round(SUM(IF(SubDate(CURRENT_DATE,43) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `43`,
Round(SUM(IF(SubDate(CURRENT_DATE,44) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `44`,
Round(SUM(IF(SubDate(CURRENT_DATE,45) = fm.arrival_date,fm.amount_changed * -1,0)),2) as `45`
FROM fund_m AS fm
LEFT JOIN member AS m
ON fm.user_id = m.id
LEFT JOIN employee AS e
ON m.account_manager = e.id
WHERE fm.arrival_date BETWEEN SubDate(CURRENT_DATE,45) AND CURRENT_DATE
AND fm.type = 'impressions'
GROUP BY m.username
ORDER BY m.username;
答案 2 :(得分:1)
一个更简化的版本。根据每个用户的合格日期和展示类型记录进行预汇总。
select
m.username AS 'Username',
SUM( if( PQ.Days = 1, PQ.Spend, 0 )) as `1`,
SUM( if( PQ.Days = 2, PQ.Spend, 0 )) as `2`,
SUM( if( PQ.Days = 3, PQ.Spend, 0 )) as `3`,
SUM( if( PQ.Days = 44, PQ.Spend, 0 )) as `44`,
SUM( if( PQ.Days = 45, PQ.Spend, 0 )) as `45`,
from
( select
fm.user_id,
datediff( current_date, fm.arrival_date ) as Days,
Round( Sum(fm.amount_changed) * -1, 2 ) AS 'spend'
from
fund_m AS fm
where
fm.arrival_date >= Date_Sub( Current_Date, interval 45 day )
AND fm.type = 'impressions'
group by
fm.user_id,
datediff( current_date, fm.arrival_date )) PQ
LEFT JOIN member AS m
ON PQ.user_id = m.id
LEFT JOIN employee AS e
ON m.account_manager = e.id
group by
m.username
如果您查看内部PQ(预查询),我将应用所有45天(where子句),并按用户和天数进行分组,那么我得到的IF()计算的天数可以追溯到0-45天。在查询结束时,您将拥有AT MOST,每人每天1条记录,因此外部查询联接可以从联接中获取其余数据。
此处最大的不同是,不是每行都进行45个日期比较,而只是确定当前日期的#天。现在,您有了一个比预聚合小得多的有限集,可以对天进行简单检查以获取当天的总数。
按用户分组,但是您随后要加入客户经理值上的employee表。您是否打算根据帐户MANAGER的名称获得总数?如果不是这样,则除非您有其他一些本示例没有涉及的字段,否则甚至不需要加入到employee表中。