sql - Optimize complex MySQL selects for reporting -


i'm building application in which,

a) each librarian can create campaign
b) actions carried out part of campaign tracked in campaign_actions, actions being page loads

in order report on number of actions made in each campaign, i wrote sql query (for mysql) following database structure, intention of tracking number of actions undertaken librarian each campaign:

 librarians id | status  campaigns id | librarian_id  campaign_actions id | campaign_id | name 

the problems having are:

a) have specify fields want count in correlated subselects
b) query quite expensive result

my question is, since there multiple actions campaign, how can count number of actions per campaign in more efficient manner?

less complex queries amount returning result set so:

   librarians.id   |  librarians.status  |  campaign_actions.name        1                    3                     pagex        1                    3                     pagey        1                    3                     pagez        1                    3                     pagea        1                    3                     pageb        2                    3                     pagex     

which means i'd have parse result set in application code row row, more expensive.

i appreciate thoughts may have on problem.

breaking task smaller tasks (views):

--- campaings per librarian create view count_librarian_campaigns ( select lib.id lib_id           , count(c.id)                    num_campaigns      librarians lib      left join campaigns c        on c.librarian_id = lib.id      group lib.id    )  --- campaign actions per campaign create view count_campaign_actions ( select c.id   c_id           , count(ca.campaign_id)                    num_actions      campaigns c      left join campaign_actions ca        on ca.campaign_id = c.id      group c.id    ) 

so, have queries this:

select lib.id lib_id      , countlibc.num_campaigns      , c.id   c_id       , countca.num_actions librarians lib join count_librarian_campaigns countlibc   on countlibc.lib_id = lib.id left join campaigns c   on c.librarian_id = lib.id join count_campaign_actions countca   on countca.c_id = c.id 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -