Select from multiple tables and sum vs join and sum
05:43 09 Jul 2013

I am doing internship in an advertising company, I already implemented a tool to gather all the necessary data form Facebook and import them on a database.

Now I am trying to manipulate that data, first by making some test cases and getting some results. The tables grow by 35k rows per day so after a month of using the tool I noticed that the query I use to get the sum of certain adcreatives clicks is starting to slow down.

I am asking if the query I use can speed up if I use it with a join and how.

Here is the query I have for the sum of clicks per adcreative (with adgroup_id, campaign_id as connect to the other tables):

SELECT t1.adgroup_id, t1.campaign_id, t1.creative_ids, SUM( t2.clicks ) AS clicks
FROM adgroups t1, adgroup_stats t2
WHERE t1.adgroup_id = t2.adgroup_id
GROUP BY t1.creative_ids
ORDER BY clicks DESC 

Currently the query takes 3 secs to complete on a dedicated server, I guess after 6 months it will be at more than 60 secs or so as the tables grow.

here is the explain of the query ( although this is the first time I actually use it and not so sure what it means)

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  t2  ALL PRIMARY NULL    NULL    NULL    671549  Using temporary; Using filesort
1   SIMPLE  t1  ref PRIMARY PRIMARY 8   fbads.t2.adgroup_id 358 Using index
sql mysql