日期:2014-05-18  浏览次数:20481 次

高手帮忙优化这条SQL语句,难!
K2.dbo._worklist数据量是20万条,DocumentReceive 10万条,其他表均在200以下
SQL语句如下:
SELECT a.procID,b.ProcName,b.ProcSetID,FROM (SELECT DISTINCT procID FROM K2.dbo._ProcInst WHERE ID IN(SELECT DISTINCT DocumentReceive.ProcInstID 
FROM K2.dbo._worklist,DocumentReceive 
WHERE K2.dbo._worklist.[User]='chenxh14' AND DocumentReceive.Exigence='平件' AND K2.dbo._worklist.Status IN(0,1) AND K2.dbo._worklist.ProcInstID 
NOT IN(SELECT ProcInstID FROM DeletedPendingWorkListItem WHERE Status=0 UNION SELECT ProcInstID FROM DeletedPendingWorkListItem WHERE Status=2)AND K2.dbo._worklist.ProcInstID=DocumentReceive.ProcInstID))a,Process b WHERE a.procID=b.procID ORDER BY b.ProcSetID ASC
在查询分析器里面运行了3分钟都没有出结果,但是我把
SELECT DISTINCT procID FROM K2.dbo._ProcInst WHERE ID IN(SELECT DISTINCT DocumentReceive.ProcInstID 
FROM K2.dbo._worklist,DocumentReceive 
WHERE K2.dbo._worklist.[User]='chenxh14' AND DocumentReceive.Exigence='平件' AND K2.dbo._worklist.Status IN(0,1) AND K2.dbo._worklist.ProcInstID 
NOT IN(SELECT ProcInstID FROM DeletedPendingWorkListItem WHERE Status=0 UNION SELECT ProcInstID FROM DeletedPendingWorkListItem WHERE Status=2)AND K2.dbo._worklist.ProcInstID=DocumentReceive.ProcInstID)
单独拿出来只需要三秒钟(结果只有4行),为什么 SELECT a.procID,b.ProcName,b.ProcSetID,FROM a,Process b WHERE a.procID=b.procID ORDER BY b.ProcSetID ASC这句要执行那么长时间呢?
高手出来帮下忙啊!!!!!!


------解决方案--------------------
in 的效率本来就低,用eists 解决
------解决方案--------------------
sql语句的优化其实就是那么几种方法,临时表、效率低的关键字、用连接代替子查询。
------解决方案--------------------
in 和 eists 根本就没有区别
虽然有些书说有区别
------解决方案--------------------
in 和 exists 根本就没有区别 
虽然有些书说有区别