繁体   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