日期:2014-05-17  浏览次数:20539 次

表中存有80万条数据 只查25条记录 查询分析器中用时13秒
下面的sql语句,表中最多存有80万条数据 只查25条记录 查询分析器中用时居然达到13秒


哪位大神 给我分析一下原因呢

select ROW_NUMBER() Over (order by mes_wi.AutoID desc) as rownum,
mes_wi.AutoID,b_pd.CustomerID,c.CustomerName,cwo.ProductID,b_pd.ProductName,cwo.FabDevice,cwo.Org,
cwo.WaferLot,cwo.TargetDevice,cwo.IsTax,cwo.IsReturn,mes_wo.Plan_StartTime,cwo.WONumber,cpo.Cust_PO, 
mes_wi.IsArrived,mes_wi.ArrivedTime,mes_wi.CartonNo,mes_wi.IsIQC,mes_wi.IQCTime,
mes_wi.IsWIP,mes_wi.WIPTime,mes_wi.IsOutStock,mes_wi.OutStockTime,cwo.TradeType,
mes_wi.IsFGIN,mes_wi.FGINTime,mes_wi.IsScrap,mes_wi.ScrapTime,mes_wo_wi.WorkorderID,
mes_wi.IsPKG,mes_wi.PKGTime,mes_wi.IsShipping,mes_wi.ShippingTime,mes_wi.DieQty,
mes_wi.WaferNo,mes_wo.WlcspLot,mes_wi.Yield,mes_wi.LMContext,b_pd1.ProductName as WO_ProductName,mes_wi.GoodDie,mes_wi.BadDie, 
Datediff(Day,(select dateadd(Hour,8,mes_wop.OutTime) as OutTime from MES_WorkorderProcess mes_wop where mes_wop.WorkorderID=mes_wo_wi.WorkorderID and mes_wop.Sequence=1 and mes_wop.Deleted=0),mes_wi.FGINTime) as ProcessCT 
from MES_WaferInfo mes_wi
left outer join Customer_WO cwo on cwo.AutoID=mes_wi.CustomerWOID 
left outer join Customer_PO cpo on cpo.AutoID=cwo.CustomerPOID 
left outer join Base_Product b_pd on b_pd.AutoID=cwo.ProductID 
left outer join Customer c on c.AutoID=b_pd.CustomerID 
left outer join MES_Workorder_WaferInfo mes_wo_wi on mes_wo_wi.WaferInfoID=mes_wi.AutoID and mes_wo_wi.Deleted<>1 
left outer join MES_WorkOrder mes_wo on mes_wo.AutoID=mes_wo_wi.WorkorderID 
left outer join Base_Product b_pd1 on b_pd1.AutoID=mes_wo.ProductID 
where mes_wi.Deleted=0 and cwo.Deleted=0 and cwo.WaferLot='H7F256'


------解决方案--------------------
把表结构和索引贴出来,最好把执行计划弄出来,估计你没有用好索引,
------解决方案--------------------
帮你整理一下格式:
SQL code
SELECT  ROW_NUMBER() OVER ( ORDER BY mes_wi.AutoID DESC ) AS rownum ,
        mes_wi.AutoID ,
        b_pd.CustomerID ,
        c.CustomerName ,
        cwo.ProductID ,
        b_pd.ProductName ,
        cwo.FabDevice ,
        cwo.Org ,
        cwo.WaferLot ,
        cwo.TargetDevice ,
        cwo.IsTax ,
        cwo.IsReturn ,
        mes_wo.Plan_StartTime ,
        cwo.WONumber ,
        cpo.Cust_PO ,
        mes_wi.IsArrived ,
        mes_wi.ArrivedTime ,
        mes_wi.CartonNo ,
        mes_wi.IsIQC ,
        mes_wi.IQCTime ,
        mes_wi.IsWIP ,
        mes_wi.WIPTime ,
        mes_wi.IsOutStock ,
        mes_wi.OutStockTime ,
        cwo.TradeType ,
        mes_wi.IsFGIN ,
        mes_wi.FGINTime ,
        mes_wi.IsScrap ,
        mes_wi.ScrapTime ,
        mes_wo_wi.WorkorderID ,
        mes_wi.IsPKG ,
        mes_wi.PKGTime ,
        mes_wi.IsShipping ,
        mes_wi.ShippingTime ,
        mes_wi.DieQty ,
        mes_wi.WaferNo ,
        mes_wo.WlcspLot ,
        mes_wi.Yield ,
        mes_wi.LMContext ,
        b_pd1.ProductName AS WO_ProductName ,
        mes_wi.GoodDie ,
        mes_wi.BadDie ,
        DATEDIFF(Day, ( SELECT  DATEADD(Hour, 8, mes_wop.OutTime) AS OutTime
                        FROM    MES_WorkorderProcess mes_wop
                        WHERE   mes_wop.WorkorderID = mes_wo_wi.WorkorderID
                                AND mes_wop.Sequence = 1
                                AND mes_wop.Deleted = 0
                      ), mes_wi.FGINTime) AS ProcessCT
FROM    MES_WaferInfo mes_wi
        LEFT OUTER JOIN Customer_WO cwo ON cwo.AutoID = mes_wi.CustomerWOID
        LEFT OUTER JOIN Customer_PO cpo ON cpo.AutoID = cwo.CustomerPOID
        LEFT OUTER JOIN Base_Product b_pd ON b_pd.AutoID = cwo.ProductID
        LEFT OUTER JOIN Customer c ON c.AutoID = b_pd.CustomerID
        LEFT OUTER JOIN MES_Workorder_WaferInfo mes_wo_wi ON mes_wo_wi.WaferInfoID = mes_wi.AutoID