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

帮我看看这个SQL如何优化?
SELECT   ERP_ProductionBatch.BatchID,   BatchNumber,   ERP_ProductionBatch.Description,   ERP_ProductionBatch.StatusID,   StatusName,ERP_ProductionBatch.CreateDate,  
dbo.fn_GetUserNameFromLoginID(ERP_ProductionBatch.CreateUser,2)   CreateUser
,DryBoardBudgetID,StatisticsID    
FROM   ERP_ProductionBatch   inner   join     StatusMC   on   StatusMC.StatusID   =   ERP_ProductionBatch.StatusID   AND  
StatusMC.CultureID   =   2  
left   join   ERP_DryBoardBudget   on   ERP_DryBoardBudget.ProductionBatchID=   ERP_ProductionBatch.BatchID  
left   join   ERP_MaterialStatistics   on   ERP_MaterialStatistics.BatchID=ERP_ProductionBatch.BatchID
  where     ERP_ProductionBatch.StatusID   not   in(7805,   7806)  
And   ERP_ProductionBatch.BatchID   in   (select   BatchID   from   ERP_MaterialStatistics)  
And   ERP_ProductionBatch.BatchID   not   in
  (select   ProductionBatchID   from   ERP_MaterialRequestForm   mr,ERP_MaterialRequestProductionBatch   mp,vObjectStateMC    
where   MaterialRequestTypeID=2   and   mr.MaterialRequestFormID=mp.MaterialRequestFormID   and   vObjectStateMC.ObjectID=mr.MaterialRequestFormID   and   ClassID=81     and   vObjectStateMC.NodeLevel   =   1  
and   StateNodeID   <> 8109   and   vObjectStateMC.CultureID=2)

------解决方案--------------------
SELECT ERP_ProductionBatch.BatchID,
BatchNumber,
ERP_ProductionBatch.Description,
ERP_ProductionBatch.StatusID,
StatusName,
ERP_ProductionBatch.CreateDate,
dbo.fn_GetUserNameFromLoginID(ERP_ProductionBatch.CreateUser,2) CreateUser,
DryBoardBudgetID,StatisticsID
FROM ERP_ProductionBatch
inner join StatusMC on StatusMC.StatusID = ERP_ProductionBatch.StatusID AND StatusMC.CultureID = 2
left join ERP_DryBoardBudget on ERP_DryBoardBudget.ProductionBatchID= ERP_ProductionBatch.BatchID
inner join ERP_MaterialStatistics on ERP_MaterialStatistics.BatchID=ERP_ProductionBatch.BatchID
where ERP_ProductionBatch.StatusID not in(7805, 7806)
--上面改为用内连接就不用这个条件了
--And ERP_ProductionBatch.BatchID in (select BatchID from ERP_MaterialStatistics)
And ERP_ProductionBatch.BatchID not in
(select ProductionBatchID from ERP_MaterialRequestForm mr,ERP_MaterialRequestProductionBatch mp,vObjectStateMC
where MaterialRequestTypeID=2 and mr.MaterialRequestFormID=mp.MaterialRequestFormID and vObjectStateMC.ObjectID=mr.MaterialRequestFormID and ClassID=81 and vObjectStateMC.NodeLevel = 1
and StateNodeID <> 8109 and vObjectStateMC.CultureID=2)