繁体   English   中英

为什么这么慢的mysql查询执行?

[英]why so slow this mysql query to execute?

select 
    'Owner' as 'Type',
    count(s.shipwynum) as Total,
    o.ownwynum as WYNum,
    o.ownshortnam as 'Short name',
    o.ownnam as 'Full name',
    cio.weburl as 'Web URL',
    count(if(s.statuscod = 'O',1,null)) as 'number of orders',
    count(if(s.statuscod = 'S',1,null)) as 'number of ships' 
from owner o 
left join ship s on o.ownwynum = s.benownwynum and s.deleted = 'N' and
      s.statuscod in ('O','S') and
      s.benownwynum != '' and s.benownwynum !='0' and s.benownwynum is not null 
left join companyinfo cio on cio.objwynum = s.benownwynum and
      cio.deleted = 'N' and cio.comflag = 'OW' where o.deleted = 'N'     
group by o.ownnam

该查询在9分钟55秒内运行,并检索10,106条记录。

我的问题是为什么要花费大量时间来获取数据,而减少执行时间又有什么问题呢?

但有趣的是,我已经运行了此查询:=

select * from ship 

执行过程需要3分12秒,将获取75,672条记录。

有优化的主意吗?

除非您真的有意避免s.shipwynum的空值,否则请尝试以下操作-替换:

count(s.shipwynum) as Total

count(*) as Total

通常,COUNT(*)的运行速度比COUNT(expression)的运行速度快。


另一个实验,替换为:

count(if(s.statuscod = 'O',1,null)) as 'number of orders'
count(if(s.statuscod = 'S',1,null)) as 'number of ships' 

sum(if(s.statuscod = 'O',1,0)) as 'number of orders'
sum(if(s.statuscod = 'S',1,0)) as 'number of ships' 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM