【发布时间】:2020-01-28 13:36:37
【问题描述】:
在 MySQL 中,您将如何重新编写一个 SELECT 查询,其中 JOIN SELECT 子查询执行几乎相同的操作。
就我而言,我遇到了性能问题,因为在子查询中,我需要为每个子查询加入相同的表,这是一项繁重的操作。
考虑下面的(丑陋的)例子:
set @businessdate = '2020-01-24';
Select Daily.*, Monthly.Price, Yearly.Price FROM
# DAILY
(Select @businessdate as BusinessDate, t1.Owner , SUM(t.Price) AS DailyPrice
FROM Table t
INNER JOIN Table2 t2 on ...
INNER JOIN Table1 t1 on ...
where t2.date = @businessdate
GROUP BY t1.Owner) Daily
INNER JOIN
# MONTHLY
(Select SUM(t.Price) AS Price, t1.Owner
FROM Table t
INNER JOIN Table2 t2 on ...
INNER JOIN Table1 t1 on ...
where MONTH(t2.date) = MONTH(@businessdate) and YEAR(t2.date) = YEAR(@businessdate)
GROUP BY t1.Owner) Monthly
INNER JOIN
#YEARLY
(Select SUM(t.Price) AS Price, t1.Owner
FROM Table t
INNER JOIN Table2 t2 on ...
INNER JOIN Table1 t1 on ...
where YEAR(t2.date) = YEAR(@businessdate)
GROUP BY t1.Owner) Yearly
原因是我需要 where 子句中的日期(来自 t2),以及来自 t1 的组中的一个字段。我还需要显示 t1 中的一些字段。
我只需要SUM(Price)(每月和每年的价格......)所以对我来说,即使它工作正常(非常慢......)子查询中的连接听起来也有点过头了(非常慢......)
【问题讨论】: