My analytics team uses a query like this:
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';
This is taking a very long time, over 12000 seconds, due to the “join” that is occurring.
Doing the sum from either table separately takes just a few seconds. I am considering putting a sum per day into a separate table to speed this up. However, I believe that it should be easier than that.
I want to eliminate the join and use subqueries to do this, however I’m not 100% sure how. I tried the following, but it did not work.
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');
That should do it.