簡體   English   中英

SQL Query的性能問題

[英]Performance issue with SQL Query

我從兩個表中提取數據 - 預測和訂單來計算銷售預測准確性。

我正在采取的步驟:

  1. 識別產品 - 區域 - 需求月份的所有詳盡組合,然后兩個數據集......讓我們稱之為(1)

  2. 識別預測數據中的不同預測快照...讓我們稱之為(2)

  3. 執行(1)和(2)的交叉連接......讓我們稱之為(3)

  4. 對於訂單和預測,在(3)的行上執行“SUMIF()”等價物。 例如,如果我將預測與2月份的實際訂單進行比較,

Jan“INDPOR”預測--->部分產品/地區 - 2月交付組合:2月預測(1月生成)與1月1日之后預訂的訂單以及2月交付時間表

2月“INDPOR”預測--->對於同一產品/地區 - 2月交貨日期組合:2月預測(2月生成)與1月27日之后訂購的訂單* 2月交貨時間表

注1:為同一月生成多個預測

注2:使用的財政日歷定義; 這就是為什么2月27日開始的原因

輸出正確生成。 但是,它很慢(1小時+)。 請幫助我對其進行微調並使其更快,因為我還需要將它用於更大的數據集。

其他詳情:

  1. 我在我的桌面本地在SQL Server 2014上運行它。使用SQL數據導入向導將其從當前的Excel文件中導入SQL
  2. 輸入預測數據:ForecastAggTable
  3. 輸入訂單數據:OrderAggTable

輸入和輸出文件

碼:

Select * 
from
    (

            Select *,

            (Select isnull(sum([Forecast Qty]),0) from ForecastAggTable t2 where t2.LOB=D.LOB and
                    t2.[Demand Month]=D.[Demand Month] and t2.Class=D.Class 
                    and t2.[Item Type]=D.[Item Type] and t2.[LoB Region]=D.[LoB Region] and
                    t2.[Key Account]=D.[Key Account] and t2.Country=D.Country 
                    and t2.[Master Customer]=D.[Master Customer] and t2.[INDPOR Version]=D.[INDPOR Version])[Forecast Qty],

            (
                    Select isnull(sum([Order Qty]),0) from OrderAggTable t1 where t1.LOB=D.LOB and
                    t1.[SAD Month]=D.[Demand Month] and t1.Class=D.Class 
                    and t1.[Item Type]=D.[Item Type] and t1.[LoB Region]=D.[LoB Region] and
                    t1.[Key Account]=D.[Key Account] and t1.Country=D.Country 
                    and t1.[Master Customer]=D.[Master Customer] and t1.[Book Date]>=D.[INDPOR Timestamp]
            )[SAD-OrderQty],

            (
                    Select isnull(sum([Order Revenue]),0) from OrderAggTable t1 where t1.LOB=D.LOB and
                    t1.[SAD Month]=D.[Demand Month] and t1.Class=D.Class 
                    and t1.[Item Type]=D.[Item Type] and t1.[LoB Region]=D.[LoB Region] and
                    t1.[Key Account]=D.[Key Account] and t1.Country=D.Country 
                    and t1.[Master Customer]=D.[Master Customer] and t1.[Book Date]>=D.[INDPOR Timestamp]
            )[SAD-OrderRevenue],


            (
                    Select isnull(sum([Order Qty]),0) from OrderAggTable t1 where t1.LOB=D.LOB and
                    t1.[RDD Month]=D.[Demand Month] and t1.Class=D.Class 
                    and t1.[Item Type]=D.[Item Type] and t1.[LoB Region]=D.[LoB Region] and
                    t1.[Key Account]=D.[Key Account] and t1.Country=D.Country 
                    and t1.[Master Customer]=D.[Master Customer] and t1.[Book Date]>=D.[INDPOR Timestamp]
            )[RDD-OrderQty],

            (
                    Select isnull(sum([Order Revenue]),0) from OrderAggTable t1 where t1.LOB=D.LOB and
                    t1.[RDD Month]=D.[Demand Month] and t1.Class=D.Class 
                    and t1.[Item Type]=D.[Item Type] and t1.[LoB Region]=D.[LoB Region] and
                    t1.[Key Account]=D.[Key Account] and t1.Country=D.Country 
                    and t1.[Master Customer]=D.[Master Customer] and t1.[Book Date]>=D.[INDPOR Timestamp]
            )[RDD-OrderRevenue]


            from
            (
            Select distinct LOB,[INDPOR Version],[INDPOR Timestamp],[Demand Month],
            [Demand Quarter],[Min Date],Class,[Item Type],[Offer PF],
            [LoB Region],[Key Account],Country,[Master Customer]

            from

            (

                            Select V.LOB,V.[SAD Month][Demand Month],V.[SAD Quarter][Demand Quarter],V.[SAD Min Date][Min Date],V.Class,
                            [Item Type],[Offer PF],[LoB Region],[Key Account],Country,[Master Customer]
                            from OrderAggTable V

                            union

                            (
                            Select Z.LOB,Z.[RDD Month][Demand Month],Z.[RDD Quarter][Demand Quarter],Z.[RDD Min Date][Min Date],Z.Class,
                            [Item Type],[Offer PF],[LoB Region],[Key Account],Country,[Master Customer]
                            from OrderAggTable Z
                            )

                            union

                            (
                            Select LOB,[Demand Month],[Demand Quarter],[Min Date],Class[Class],[Item Type],[Offer PF],[LoB Region],
                            [Key Account],Country,[Master Customer] from ForecastAggTable
                            )
            )A

            cross join

            (
            select distinct [INDPOR Version],[INDPOR Timestamp] 
            from ForecastAggTable
            )B
            )D
            where [Min Date]>=[INDPOR Timestamp]
            )E
            where ([SAD-OrderQty] +  [RDD-OrderQty] + [Forecast Qty]<>0)

如何簡化和減少表的傳遞。

在這個例子中,我對預測表進行了兩次掃描,一次針對distinct,一次針對union,一次掃描orders表。

with cte as
(
 select distinct [INDPOR Version],[INDPOR Timestamp] 
            from ForecastAggTable
)
,cte2 as
(
Select 
    V.LOB
    ,iif(DUP=0,V.[SAD Month]    ,V.[RDD Month]      )   [Demand Month]
    ,iif(DUP=0,V.[SAD Quarter]  ,V.[RDD Quarter]    )   [Demand Quarter]
    ,iif(DUP=0,V.[SAD Min Date] ,V.[RDD Min Date]   )   [Min Date]
    ,V.[Book Date]
    ,V.Class
    ,V.[Item Type]
    ,V.[Offer PF]
    ,V.[LoB Region]
    ,V.[Key Account]
    ,V.Country
    ,V.[Master Customer]
    ,null [INDPOR Version]
    ,null [Forecast Qty]
    ,iif(DUP=0,v.[Order Qty]    ,null   )       [SAD-OrderQty]
    ,iif(DUP=0,V.[Order Revenue]    ,null   )   [SAD-OrderRevenue]
    ,iif(DUP=1,V.[Order Qty]    ,null   )       [RDD-OrderQty]
    ,iif(DUP=1,V.[Order Revenue]    ,null   )   [RDD-OrderRevenue]
from OrderAggTable V
cross join (select dup from (values (0),(1))a(dup)) a
union all
Select 
    LOB
    ,[Demand Month]
    ,[Demand Quarter]
    ,[Min Date]
    ,[Min Date]
    ,Class
    ,[Item Type]
    ,[Offer PF]
    ,[LoB Region]
    ,[Key Account]
    ,Country
    ,[Master Customer] 
    ,[INDPOR Version]
    ,[Forecast Qty] 
    ,null[SAD-OrderQty]
    ,null[SAD-OrderRevenue]
    ,null[RDD-OrderQty]
    ,null[RDD-OrderRevenue]
    from ForecastAggTable
)
select
    cte2.LOB
    ,cte.[INDPOR Version]
    ,cte.[INDPOR Timestamp]
    ,cte2.[Demand Month]
    ,cte2.[Demand Quarter]
    ,cte2.[Min Date]
    ,cte2.Class
    ,cte2.[Item Type]
    ,cte2.[Offer PF]
    ,cte2.[LoB Region]
    ,cte2.[Key Account]
    ,cte2.Country
    ,cte2.[Master Customer]
    ,isnull(sum(cte2.[Forecast Qty]    ),0) [Forecast Qty]     
    ,isnull(sum(cte2.[SAD-OrderQty]    ),0) [SAD-OrderQty]
    ,isnull(sum(cte2.[SAD-OrderRevenue])    ,0) [SAD-OrderRevenue]
    ,isnull(sum(cte2.[RDD-OrderQty]    ),0) [RDD-OrderQty]     
    ,isnull(sum(cte2.[RDD-OrderRevenue])    ,0) [RDD-OrderRevenue]
from cte2
inner join cte
on cte2.[Book Date]>=cte.[INDPOR Timestamp]
where isnull(cte2.[INDPOR Version],cte.[INDPOR Version])=cte.[INDPOR Version]
group by    
     cte2.LOB
    ,cte2.[Demand Month]
    ,cte2.[Demand Quarter]
    ,cte2.[Min Date]
    ,cte2.Class
    ,cte2.[Item Type]
    ,cte2.[Offer PF]
    ,cte2.[LoB Region]
    ,cte2.[Key Account]
    ,cte2.Country
    ,cte2.[Master Customer]
    ,cte.[INDPOR Version]
    ,cte.[INDPOR Timestamp]
having 
    isnull(sum(cte2.[Forecast Qty]     ),0) + 
    isnull(sum(cte2.[SAD-OrderQty]     ),0) +
    isnull(sum(cte2.[RDD-OrderQty]     ),0) 
    !=0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM