添加子查询时mysql查询速度很慢

时间:2014-03-19 04:30:08

标签: mysql

我有以下查询,只有800条记录需要5分钟才能运行,请帮助一下

SELECT 

vtiger_salesorder.salesorderid,vtiger_salesorder.salesorder_no,vtiger_salesorder.sostatus,
(SELECT se.s_date
FROM
softMax_events as se 
INNER JOIN vtiger_salesorder as bm ON bm.salesorderid = se.orderNum
where (bm.sostatus = 'Order' AND se.orderNum = vtiger_salesorder.salesorderid) AND se.appointTyp='60'
group by bm.salesorderid  Limit 0,1) As sdate

FROM
  vtiger_salesorder
Inner Join vtiger_crmentity ON vtiger_salesorder.salesorderid = vtiger_crmentity.crmid
WHERE (vtiger_salesorder.sostatus = 'Order')
and ( vtiger_crmentity.deleted<>'1')

1 个答案:

答案 0 :(得分:0)

尝试此查询,希望这对您有帮助,

SELECT vtiger_salesorder.salesorderid,vtiger_salesorder.salesorder_no,vtiger_salesorder.sostatus,se.s_date
    FROM  vtiger_salesorder 
    Inner Join vtiger_crmentity ON vtiger_salesorder.salesorderid = vtiger_crmentity.crmid
    INNER JOIN softMax_events se ON se.orderNum = salesorderid 
    WHERE (vtiger_salesorder.sostatus = 'Order') AND/OR
    se.orderNum = vtiger_salesorder.salesorderid AND se.appointTyp='60'
    and ( vtiger_crmentity.deleted<>'1')

修改 我注意到vtiger_salesorder and softMax_events之间存在某种关系,你可以对这两个表使用连接,这样你可以删除那个内部查询,我试过它你可以测试它。经过一点修改后,这将对您有所帮助。