subquery - Better performance in MySQL subqueries for timeline graph -


i have query subqueries timeline widget of participants, leads , customers. example 15k rows in table 2k in date range (january 1st january 28th) takes 40 seconds!

select created_at date,  (     select count(id)     participant     created_at <= date ) participants, (     select count(distinct id)     participant     participant_type = "lead"     , created_at <= date ) leads, (     select count(distinct id)     participant     participant_type = "customer"     , created_at <= date ) customer participant created_at >= '2016-01-01 00:00:00' , created_at <= '2016-01-28 23:59:59' group date(date) 

how can improve performance?

the table fields declared follows:

id => primary_key, int 10, auto increment participant_type => enum "lead,customer", nullable, ut8_unicode_ci created_at => timestamp, default '0000-00-00 00:00:00' 

possibly try using conditions within counts (or sums) values want, having cross joined things:-

select a.created_at date,          sum(if(b.created_at <= a.created_at, 1, 0)) participants,         count(distinct if(b.participant_type = "lead" , b.created_at <= a.created_at, b.id, null)) leads,         count(distinct if(b.participant_type = "customer" , b.created_at <= a.created_at, b.id, null)) customer participant cross join participant b a.created_at >= '2016-01-01 00:00:00' , a.created_at <= '2016-01-28 23:59:59' group date(date) 

or maybe move date check join

select a.created_at date,          count(b.id) participants,         count(distinct if(b.participant_type = "lead", b.id, null)) leads,         count(distinct if(b.participant_type = "customer", b.id, null)) customer participant left outer join participant b on b.created_at <= a.created_at a.created_at >= '2016-01-01 00:00:00' , a.created_at <= '2016-01-28 23:59:59' group date(date) 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -