2016-04-01 6 views
1

Wir haben mehrere MySql-Abfragen, die gut funktionieren, wenn eine kleinere Anzahl von Datensätzen ausgewählt werden, d. H. Begrenzen 0,100.Mysql nimmt lange für größere Grenzen

Die Abfrage dauerte fast 10X bis 50X mehr Zeit, wenn wir den Grenzwert von 100 auf 1000 erhöhen, d. H. 0,1000 begrenzen.

BEARBEITET Wie können wir das optimieren? Explain Plan unten geben,

Explain PLan

QUERY

Select 
    concat(floor(TIMESTAMPDIFF(MINUTE,x.msgCreatedOn, NOW())/60),':',floor(TIMESTAMPDIFF(MINUTE,x.msgCreatedOn, NOW())%60)) as aging, 
    x.`actionStartDate`, 
    concat(floor(TIMESTAMPDIFF(MINUTE,x.`actionStartDate`, NOW())/60),':',floor(TIMESTAMPDIFF(MINUTE,x.`actionStartDate`, NOW())%60)) as workflowAging 
from 
    ( 
    Select 
     * 
    from 
     t_wk_dtls d left 
    join t_s_dtls s on (d.id = s.`work_parentId` or d.`parentId` = s.`work_parentId`) and d.`currentlyActive` = true 
    left join t_w b on b.basketId=d.toBasketId 
    left join t_u u on d.toUserId= u.userId 
    left join t_s_m sch on d.sourceId=sch.schMsgId 
    left join t_l_s_a a on a.orgId='1002' 
      AND ((((a.`type`='TWITTER' and d.channel in (1,34)) 
or (a.`type`='FACEBOOK' and d.channel in (6,7)) 
or (a.`type`='GOOGLEPLUS' and d.channel in (5,25)) 
or (a.`type`='LINKEDIN' and d.channel =30) 
or (a.`type`='GOOGLEPLUS' and d.channel=36) 
or ((a.`type`='YOUTUBE' or a.`type`='GOOGLEPLUS') and d.channel=27) 
or (a.`type`='TUMBLR' and d.channel in (29,31)) 
or (a.`type`='INSTAGRAM' and d.channel=35)) AND d.userChannelId=a.socialId)    OR (a.`type`='BLOG' and d.channel in (9,11,15,21) AND d.msgId=a.socialId)) 
    left join t_l l on l.leadId=a.leadId and l.orgId='1002' 
where 
    d.currentlyActive = true 
and d.dataSource ='SOLR' 
and d.profileId = '148' AND msgCreatedOn BETWEEN '2016-03-14 18:30:00' AND '2016-03-31 18:29:59' 
order by d.msgCreatedOn desc limit 0,100000) as x 
+1

Indizes ........ –

+0

Beachten Sie, dass viel mehr Daten übertragen. – jarlh

+0

könnten Sie die Abfrage zur Frage hinzufügen? Es könnte Cross Join oder etwas anderes sein, das die Abfrage mit höheren Limits verlangsamt. – Jester

Antwort

0

Haben Sie wirklich noch eine Unterabfrage dafür brauchen? Beachten Sie jetzt die "abhängige Unterabfrage" in den EXPLAIN-Ergebnissen. Dies bedeutet im Allgemeinen, dass die Unterabfrage einmal pro zurückgegebener äußerer Zeile ausgeführt wird (siehe point 18.2.1.18.2 in "18.2.1.18 Subquery optimization"). Als Ergebnis erhöht sich Ihre Ausführungszeit exponentiell, wenn mehr Zeilen zurückgegeben werden.

Da Sie nur eine select * from in der Unterabfrage tun, würde nicht etwas wie das gleiche erreichen?

Select 
    concat(floor(TIMESTAMPDIFF(MINUTE,x.msgCreatedOn, NOW())/60),':',floor(TIMESTAMPDIFF(MINUTE,x.msgCreatedOn, NOW())%60)) as aging, 
    x.`actionStartDate`, 
    concat(floor(TIMESTAMPDIFF(MINUTE,x.`actionStartDate`, NOW())/60),':',floor(TIMESTAMPDIFF(MINUTE,x.`actionStartDate`, NOW())%60)) as workflowAging 
from 
    t_wk_dtls d left 
join t_s_dtls s on (d.id = s.`work_parentId` or d.`parentId` = s.`work_parentId`) and d.`currentlyActive` = true 
left join t_w b on b.basketId=d.toBasketId 
left join t_u u on d.toUserId= u.userId 
left join t_s_m sch on d.sourceId=sch.schMsgId 
left join t_l_s_a a on a.orgId='1002' 
     AND ((((a.`type`='TWITTER' and d.channel in (1,34)) 
or (a.`type`='FACEBOOK' and d.channel in (6,7)) 
or (a.`type`='GOOGLEPLUS' and d.channel in (5,25)) 
or (a.`type`='LINKEDIN' and d.channel =30) 
or (a.`type`='GOOGLEPLUS' and d.channel=36) 
or ((a.`type`='YOUTUBE' or a.`type`='GOOGLEPLUS') and d.channel=27) 
or (a.`type`='TUMBLR' and d.channel in (29,31)) 
or (a.`type`='INSTAGRAM' and d.channel=35)) AND d.userChannelId=a.socialId)  OR (a.`type`='BLOG' and d.channel in (9,11,15,21) AND d.msgId=a.socialId)) 
left join t_l l on l.leadId=a.leadId and l.orgId='1002' 
where 
d.currentlyActive = true 
and d.dataSource ='SOLR' 
and d.profileId = '148' AND msgCreatedOn BETWEEN '2016-03-14 18:30:00' AND  '2016-03-31 18:29:59' 
order by d.msgCreatedOn desc limit 0,100000; 

(mit der Ausnahme der Tabellenpräfixe geändert von „x“ zu dem realen in der Spaltenliste am Anfang)