我的分析团队使用如下查询:
SELECT
SUM(ul.upload_bandwidth) + SUM(dl.download_bandwidth) AS total
FROM
upload_facts ul, download_facts dl
WHERE
ul.date BETWEEN '2011-09-01' AND '2011-09-30' AND
dl.date BETWEEN '2011-09-01' AND '2011-09-30';
由于正在发生的“加入”,这需要很长时间,超过12000秒。
从两个表中分别进行求和只需几秒钟。我正在考虑每天将一笔金额放入一张单独的表中以加快速度。但是,我认为应该比这更容易。
我想消除连接并使用子查询来执行此操作,但是我不是100%确定如何。我尝试了以下方法,但它没有用。
SELECT
(select upload_bandwidth from upload_facts where date
BETWEEN '2011-09-01' AND '2011-09-30')
+
(select download_bandwidth from downloaded_facts where date
BETWEEN '2011-09-01' AND '2011-09-30');
答案 0 :(得分:6)
SELECT
(SELECT SUM(`upload_bandwidth`) FROM `upload_facts` WHERE `date`
BETWEEN '2011-09-01' AND '2011-09-30')
+
(SELECT SUM(`download_bandwidth`) FROM `download_facts` WHERE `date`
BETWEEN '2011-09-01' AND '2011-09-30')
AS `total`
应该这样做。