sql语句查询慢(50分)

  • 主题发起人 主题发起人 bijiaofan
  • 开始时间 开始时间
B

bijiaofan

Unregistered / Unconfirmed
GUEST, unregistred user!
请各位大虾帮忙看一下这条语句怎这么慢,该加的索引也都加了,查询出来要两到三分钟:<br>SELECT a.rmano,a.PN, a.SN, c.Model AS model,e.maxreturncount,a.returndays, <br>f.test_station,f.test_time,f.repair_time,f.repairer,f.error_code,f.error_desc, <br>g.reason_code,g.reason_desc,f.component,f.location,f.duty FROM RMARecv_d AS a <br>INNER JOIN (SELECT sn,MAX(returncount) AS maxreturncount FROM dbo.RMARecv_d GROUP BY sn ) e <br>ON a.sn=e.sn AND a.returncount=e.maxreturncount <br>INNER JOIN bd_itmstr AS c ON a.PN = c.PN <br>INNER JOIN (SELECT red.partpn AS component ,red.flow_order,red.partloc AS location,red.duty,red.create_t AS create_t,<br>red.sn,red.rmano, (CASE red.pre_station WHEn '3' THEN 'MF/T1' WHen '11' THEN 'MF/T2' WHen '18' THEN 'MF/T3' WHen '10' <br>THEN 'RunIn1' WHen '15' THEN 'RunIn2' WHen '13' THEN 'OBE' WHen '12' THEN 'COSCHECK' WHen '21' THEN 'T00' END ) AS test_station, <br>red.pre_t AS test_time,red.probcd AS error_code,pro.descript AS error_desc,red.start_t AS repair_time,red.rpengfullnm AS repairer <br>FROM RepaLog_d red LEFT OUTER JOIN bd_probcd pro ON red.ProbCd COLLATE Chinese_PRC_CI_AS_WS = pro.ProbCd ) f <br>ON a.sn=f.sn AND a.RMANo=f.rmano INNER JOIN (SELECT rem.flow_order,rem.sn,rem.RMANo,rs.reason AS reason_code,<br>rs.descript AS reason_desc FROM dbo.RepaLog_m rem LEFT OUTER JOIN dbo.bd_reason rs ON <br>rem.result = rs.result AND rem.reason=rs.reason) g ON a.sn=g.sn AND a.RMANo=g.rmano AND g.flow_order=f.flow_order <br>where (f.error_code='4A03') AND (f.repair_time BETWEEN '2008/10/4 8:00:00' AND '2008/11/4 17:00:00') ORDER BY f.repair_time
 
表关联太多了,估计没办法提高速度。想办法减少关联。
 
把INNER JOIN bd_itmstr AS c ON a.PN = c.PN这个关联与INNER JOIN (SELECT sn,MAX(returncount) AS maxreturncount FROM dbo.RMARecv_d GROUP BY sn ) e <br>ON a.sn=e.sn AND a.returncount=e.maxreturncount 换一个位置试试。
 
已经搞定,送分吧,多加几个索引就OK了
 
晕,这么乱怎么看.
 
多人接受答案了。
 
后退
顶部