I have a query on a large table(over 2 million rows) which takes ~10 seconds to complete. Is there any way to optimize it? The query is as below:
SELECT
DATE_FORMAT(date0, '%Y-%m' ) AS Yr_Mo,
DATE_FORMAT(date0, '%p' ) AS AM_PM,
province AS Province,
SUM( IF( top_ads + left_ads =0, 1, 0 ) ) AS pagesWithRightAdsOnly,
AVG( top_ads ) AS top_ads,
AVG( left_ads ) AS left_ads,
AVG( right_ads ) AS right_ads
FROM ad_counts
GROUP BY Yr_Mo, AM_PM, Province
The table 'ad_counts':
date0 (timestamp)
province(varchar)
keyword_id
number_ads(int)
top_ads (int)
left_ads (int)
right_ads (int)
Index on date0, but date0 is not unique.
Any thoughts?
Edit: EXPLAIN:
id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE baidu_pro_ad_counts ALL NULL NULL NULL NULL 2160752 Using temporary; Using filesort