php - Effectively Firing a multiple query -
i practicing drawing graphs on various statistics purpose of data analysis. not able figure out efficient way fire multiple mysql query @ back-end.
i trying draw period vs no of visitors graph. please note: period here refers week,month,3 months,6 months,1 year,2 years. period selected user select box. example: when user selects 3 week, need construct no of visitors per 3 week graph.
my database contains 2 column: each of site hit, records:
(1) timestamp and
(2)user id.
if fire query multiple times each select option, performance quite poor.so, how efficiently?
upd: when user select stats per 3 month:
then firing mysql query as:
select count(*) stats_tab timestamp between jan , mar; select count(*) stats_tab timestamp between apr , jun; select count(*) stats_tab timestamp between jul , sep; ............
each count returned each of query y-axis value graph
when user select stats per year: firing mysql query as:
select count(*) stats_tab timestamp between 2009 , 2010; select count(*) stats_tab timestamp between 2010 , 2011; select count(*) stats_tab timestamp between 2011 , 2012; ............
don't hit database multiple queries. values 1 query appropriately applying group by
, where
select year(timestamp) year, count(*) total stats_tab timestamp between now() - interval 3 year , now() group year(timestamp); select month(timestamp) month, count(*) total stats_tab timestamp between now() - interval 6 month , now() group month(timestamp); select day(timestamp) day, count(*) total stats_tab timestamp between now() - interval 7 day , now() group day(timestamp);
sample output:
| year | total | ---------------- | 2011 | 2 | | 2012 | 1 | | 2013 | 9 | | month | total | ----------------- | 2 | 1 | | 3 | 2 | | 5 | 1 | | day | total | --------------- | 20 | 1 | | 21 | 1 | | 22 | 1 |
here sqlfiddle demo
Comments
Post a Comment