I have two tables. One is a click-log, that records the exact time we received a click. The 2nd is a dollars / day table that records the amount we earned each day with a particular site.
I need to calculate the daily CPC for each site. The query bellow is working, but takes almost a minute.
Here’s the result of explain:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE click_log ALL url_id,click_time NULL NULL NULL 1404209 Using where; Using temporary; Using filesort
1 SIMPLE daily_dollars ref url_id,date date 3 func 6 Using where
And the query:
SELECT date( click_log.click_time ) AS DAY,
click_log.shorturl AS site,
daily_dollars.money AS earned,
count( click_log.click_id ) AS clicks,
daily_dollars.money / count( click_log.click_id ) AS CPC
FROM `yourls_log` AS click_log, yourls_url_money AS daily_dollars
WHERE click_log.click_time >= "2011-07-01"
AND click_log.url_id = daily_dollars.url_id
AND date( click_log.click_time ) = daily_dollars.date
GROUP BY DAY , click_log.shorturl
Anything I can do to speed this up?
Table Structure:
yourls_log
----------
click_id
click_time
shorturl
url_id
yourls_url_money
----------------
id
url_id
date
money
Not sure why, but this is much faster (1.5 seconds):