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

帮忙优化一条MSSQL查询语句
请帮忙优化一条MSSQL查询语句,查询返回的记录行有94236行,用时5秒!语句如下:

--序列号跟踪报表
Select ts.FSerialNum As FSerialNum,tk.FName As FStockName, t.FNumber as FItemnumber,t.FName As FItemName,ta.FName As FAuxPropName,t.FModel,  
'预入库' As FStatus,tk.FNumber As FStockNumber, '仓库调拨' As FLasttranType --,1 As FSumSort,1 AS FOrder
From ICSerial ts
Inner Join ICStockBill tb On tb.FInterID=ts.FInterID
Inner Join ICSerial_F1 ts1 On ts1.FSerialClassID=ts.FSerialClassID And ts1.FSerialID=ts.FSerialID
Inner Join t_ICItem t On t.FItemID=ts.FItemID
Left Join t_AuxItem ta On ta.FItemID=ts.FAuxPropID
Left Join t_Stock tk On tk.FItemID=ts.FStockID
Left Join t_StockPlace tp On tp.FSPID=ts.FStockPlace 
Left Join t_StockPlace tpo On tpo.FSPID=ts.FOutStockPlace
 Where ts.FSerialClassID=1AND ts.FTrantypeID = 41 AND ts.FValid=0 
UNION ALL 
Select ts.FSerialNum As FSerialNum,tk.FName As FStockName, t.FNumber as FItemnumber,
  t.FName As FItemName,ta.FName As FAuxPropName,t.FModel,
  Case When ts.FTranTypeID In (1,2,5,10,40) And tb.FRob=1 And ts.FValid=0 Then '预入库' 
  When ts.FTranTypeID In (1,2,5,10,40) And tb.FRob=1 And ts.FValid=1 Then '在库' 
  When ts.FTranTypeID In (1,2,5,10,40) And tb.FRob=-1 And ts.FValid=0 Then '预退库' 
  When ts.FTranTypeID In (1,2,5,10,40) And tb.FRob=-1 And ts.FValid=1 Then '退库' 
  When ts.FTranTypeID In (21,24,28,29,43) And tb.FRob=-1 And ts.FValid=0 Then '预入库' 
  When ts.FTranTypeID In (21,24,28,29,43) And tb.FRob=-1 And ts.FValid=1 Then '在库' 
  When ts.FTranTypeID In (21,24,28,29,43) And tb.FRob=1 And ts.FValid=0 Then '预出库' 
  When ts.FTranTypeID In (21,24,28,29,43) And tb.FRob=1 And ts.FValid=1 Then '出库' 
  When ts.FTranTypeID =41 And ts.FValid=1 Then '在库' 
  When ts.FTranTypeID =41 And ts.FValid=0 Then '预出库' End As FStatus,
  tk.FNumber As FStockNumber,  
  Case When ts.FTranTypeID=1 Then '外购入库'
  When ts.FTranTypeID=2 Then '产品入库'
  When ts.FTranTypeID=5 Then '委外加工入库'
  When ts.FTranTypeID=10 Then '其他入库'
  When ts.FTranTypeID=21 Then '销售出库'
  When ts.FTranTypeID=24 Then '生产领料'
  When ts.FTranTypeID=28 Then '委外加工发出'
  When ts.FTranTypeID=29 Then '其他出库'
  When ts.FTranTypeID=40 Then '盘盈入库'
  When ts.FTranTypeID=41 Then '仓库调拨'
  When ts.FTranTypeID=43 Then '盘亏毁损'
  When ts.FTranTypeID=-1 Then '初始化' End As FLasttranType --,1 As FSumSort,0 AS FOrder
From ICSerial ts
Inner Join ICStockBill tb On tb.FInterID=ts.FInterID
Inner Join ICSerial_F1 ts1 On ts1.FSerialClassID=ts.FSerialClassID And ts1.FSerialID=ts.FSerialID
Inner Join t_ICItem t On t.FItemID=ts.FItemID
Left Join t_AuxItem ta On ta.FItemID=ts.FAuxPropID
Left Join t_Stock tk On tk.FItemID=(case when ts.FTranTypeID=41 and ts.FValid=0 Then ts.foutstockid else ts.FStockID end)
Left Join t_StockPlace tp On tp.FSPID=(case when ts.FTranTypeID=41 and ts.FValid=0 Then ts.FoutStockPlace else ts.FStockPlace end )
Left Join t_StockPlace tpo On tpo.FSPID=ts.FOutStockPlace
Where ts.FSerialClassID=1
Union All
Select ts.FSerialNum As FSerialNum,tk.FName As FStockName, t.FNumber as FItemnumber,
  t.FName As FItemName,ta.FName As FAuxPropName,t.FModel,Case ts.FValid When 1 Then '在库' Else '预入库' End As FStatus,tk.FNumber As FStockNumber,  
  '初始化' As FLasttranType --,1 As FSumSort,0 AS FOrder
From ICSerial ts
Inner Join IcInvInitial tb On tb.FSNListID=ts.FSNListID
Inner Join ICSerial_F1 ts1 On ts1.FSerialClassID=