我们有几个MySql查询在选择较少数量的记录时表现良好,即限制为0,100。
当我们将限制从100增加到1000,即限制为0,1000时,查询时间增加了近10倍到50倍。
EDITED 我们怎样才能优化这个?解释计划如下,
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
答案 0 :(得分:0)
你真的需要一个子查询吗?就像现在一样,注意"依赖子查询"在解释结果中 - 它通常意味着子查询每返回一个外行执行一次(参见point 18.2.1.18.2 in "18.2.1.18 Subquery optimization")。因此,随着返回更多行,您的执行时间会呈指数级增长。
既然您只是在子查询中执行select * from
,那么这样的事情会不会这样做?
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;
(除了表格前缀从" x"更改为开头列列表中的真实前缀)