尝试按市场计算一年中每天输入系统的月度收入。当前查询有效但它在我的BI工具的mysql实例中保持超时(设置为15分钟)。我的BI工具也可能不允许使用mysql变量,如果他们这样做,则必须是有条件的。理想情况下,我想添加更多条件。
/* Current query with subquery, this works syntactically, but is inefficient*/
SELECT
d.`Event Date`,
d.`market`,
(SELECT SUM(s.`Revenue`)
FROM time_from_start s
WHERE s.`created` <= d.`Event Date`
AND s.`Month/Year` = d.`Month/Year`
AND s.`market` = d.`market`) as 'Revenue to Date'
FROM time_from_start d
GROUP BY d.`Event Date`,d.market
答案 0 :(得分:0)
尝试使用一个避免使用自联接来避免使用相关子查询的查询。
此查询应提供与原始查询相同的结果:
SELECT d1.`Event Date`, d1.market, SUM(d2.Revenue) AS Revenue_to_date
FROM time_from_start d1
LEFT JOIN time_from_start d2
ON d2.market = d1.market
AND d2.`Month/Year` = d1.`Month/Year`
AND d2.created <= d1.`Event Date`
GROUP BY d1.`Event Date`, d1.market
另外,请确保查询中使用的列有索引。