php - how to aggregate data by age for rows spanning across different timeline -
i have huge table on 10,00,000 rows , growing. data looks this
id topic_id publish_date hits ------------------------------------------------- 1 1 2014-01-01 20 2 1 2014-01-01 10 3 1 2014-01-03 10 4 2 2014-01-10 50 5 2 2014-01-15 100 ... ...
i have table topics id, name, start_date, end_date.
i need retrieve hit data table daywise hits each topic. enable me compare performance on each day
eg:
topic -> 1 2 day 1 x y 2 y 3 z b
so on.
i hope made proper explanation.
what trying is:
select report.topic_id, topic.start_date, sum(if( datediff(report.hits , en.start_date) = 0, hit,0)) d0 ,sum(if( datediff(report.hits , en.start_date) = 1, hit,0)) d1 ,sum(if( datediff(report.hits , en.start_date) = 2, hit,0)) d2 ,sum(if( datediff(report.hits , en.start_date) = 3, hit,0)) d3 ,sum(if( datediff(report.hits , en.start_date) = 4, hit,0)) d4 ,sum(if( datediff(report.hits , en.start_date) = 5, hit,0)) d5 report left join topic en on report.topic_id = en.id topic_id in (1,2,4) group topic_id
it takes 10-12 seconds generate result data. using php & mysql. willing tradeoff taking off db load script calculation, caching results.
is there better way this? looking @ faster way retrieve results.
Comments
Post a Comment