SQL優化方案

1:創建中間表,將步驟分解。優化

2:避免全字段查詢,只查須要的字段it

3:限定條件查詢,避免先關聯後寫條件,select

 

--優化交貨數據  --建臨時表T_JHinfo  select A.VBELN,C.VBELN AS JH_VBELN,C.ERDAT AS JH_ERDAT,C.WADAT_IST   from (          select VBELN,case when AUART='ZA17' then BSTNK else  VBELN end as VBELNNO          from VBAK          where ERDAT=CONVERT(varchar(10),GETDATE()-1,112)          )A left join (select VBELN,VGBEL from LIPS) AS B ON A.VBELNNO=B.VGBEL             LEFT jOIN (select VBELN,ERDAT,WADAT_IST FROM LIKP) AS C ON B.VBELN=C.VBELN數據

select A.ERDAT AS 對接日期, A.ERZET AS 對接時間,A.ORDERID AS 訂單號,DDLX AS 訂單類型,A.KUNNR AS 客戶編號, B.POSNR1 AS 訂單行項目,B.MATNR AS 物料編號,KWMENGE AS 數量,SLPRC AS 單價,BUKRS AS 銷售組織,B.VKBUR AS 銷售辦公室, B.VBELN as SAP訂單號,POSNR AS SAP訂單行項目,ZMENG AS SAP訂單受理數量,LIFSK AS SAP訂單交貨凍結, C.ERDAT AS SAP訂單建立日期, F.JH_VBELN AS SAP交貨單號,F.JH_ERDAT AS SAP交貨單建立日期,F.WADAT_IST AS SAP交貨單過賬日期, B.REASON as 拒絕緣由,case when  A.DDLX='08' AND B.VBELN='' AND B.REASON='' then '待審覈'         when A.DDLX='08' AND LIFSK='ZE'  then '已審覈,待付款'         when  A.DDLX='08' AND LIFSK='ZF'  then '已付款,待發貨'         when  A.DDLX='08' AND F.WADAT_IST<>'' then '已發貨'         when  A.DDLX='08' AND  B.REASON<>'' then '已拒絕'         when  A.DDLX<>'08' AND B.VBELN='' AND B.REASON='' then '待受理'         when  A.DDLX<>'08' AND B.VBELN<>'' AND F.WADAT_IST='' then '已受理待發貨'         when  A.DDLX<>'08' AND F.WADAT_IST<>'' then '已發貨'         end as 訂單狀態 from (select ERDAT,ERZET,ORDERID,DDLX,KUNNR,BUKRS   from ZTSD200 where ERDAT=CONVERT(Varchar(10),GETDATE()-1,112) ) AS  A left join (select ORDERID,POSNR1,MATNR,KWMENGE,SLPRC,VKBUR,VBELN,REASON                       from  ZTSD200_item                       where ORDERID IN (select ORDERID from ZTSD200 where ERDAT=CONVERT(Varchar(10),GETDATE()-1,112) )        ) AS  B ON A.ORDERID=B.ORDERID                left join (select VBELN,LIFSK,ERDAT from VBAK where ERDAT=CONVERT(Varchar(10),GETDATE()-1,112)) C ON B.VBELN=C.VBELN                LEFT JOIN (select VBELN,POSNR,ZMENG from VBAP where ABGRU not in ('X2','X6') AND ERDAT=CONVERT(Varchar(10),GETDATE()-1,112)) D ON B.VBELN=D.VBELN       LEFT jOIN T_JHinfo AS F ON C.VBELN=F.VBELN項目

相關文章
相關標籤/搜索