我坚持改进SQL查询的性能

时间:2016-07-22 05:25:05

标签: sql-server-2012

我一直在改进查询的性能。我的查询返回单行中每个产品的所有54周基线数据,如下图所示。

SELECT BusinessPlanMain.Id, BusinessPlanMain.SalesOrg, ProductMain.MetaType, ProductMain.Id [Product ID], BplText.Text, BusinessPlanData.Week1, BusinessPlanData.Week2
                .
                .
                .
 , BusinessPlanData.Week54, SystemPeriod.Year
  FROM BusinessPlanMain INNER JOIN SystemPeriod ON BusinessPlanMain.SystemPeriodPKey = SystemPeriod.PKey INNER JOIN BusinessPlanData ON BusinessPlanMain.PKey = BusinessPlanData.BusinessPlanMainPKey INNER JOIN BusinessPlanDataTemplate ON BusinessPlanDataTemplate.PKey = BusinessPlanData.BusinessPlanDataTemplatePKey INNER JOIN BplText ON BplText.ReferencePKey=BusinessPlanTemplate.PKey INNER JOIN ProductMain ON BusinessPlanData.ProductMainPKey=ProductMain.PKey WHERE SystemPeriod.Year IN ('2016') AND SystemPeriod.PeriodType = 'Year' AND SystemPeriod.SalesOrg = 'OrgID1' AND BusinessPlanMain.SalesOrg='OrgID1' AND BusinessPlanData.OrgID ='OrgID1' AND BusinessPlanTemplate.Sheet='Baseline' and ProductMain.MetaType IN ('PrdGroup','Product')

The Result of the above Query is

我的项目要求创建一个DataView,我必须将所有54周的列数据转换成一个列,如下图所示,我通过使用' Union All'关键字54次以上查询。

我正在寻找更好的解决方案来提高我的查询性能,而忽略了54次联合。在此先感谢,任何更好的解决方案都可以得到赞赏。

SELECT BusinessPlanMain.Id
 , BusinessPlanMain.SalesOrg
 , ProductMain.MetaType 
 , ProductMain.Id [Product ID]
 , BplText.Text
 , BusinessPlanData.Week1
 , SystemPeriod.Year
  FROM BusinessPlanMain INNER JOIN SystemPeriod ON BusinessPlanMain.SystemPeriodPKey = SystemPeriod.PKey INNER JOIN BusinessPlanData ON BusinessPlanMain.PKey = BusinessPlanData.BusinessPlanMainPKey INNER JOIN BusinessPlanDataTemplate ON BusinessPlanDataTemplate.PKey = BusinessPlanData.BusinessPlanDataTemplatePKey INNER JOIN BplText ON BplText.ReferencePKey=BusinessPlanTemplate.PKey INNER JOIN ProductMain ON BusinessPlanData.ProductMainPKey=ProductMain.PKey WHERE  SystemPeriod.Year IN ('2016') AND SystemPeriod.PeriodType = 'Year' AND  SystemPeriod.SalesOrg = 'OrgID1' AND BusinessPlanMain.SalesOrg='OrgID1' AND    BusinessPlanData.OrgID ='OrgID1' AND BusinessPlanTemplate.Sheet='Baseline' and   ProductMain.MetaType IN ('PrdGroup','Product') Union ALL SELECT BusinessPlanMain.Id
 , BusinessPlanMain.SalesOrg
 , ProductMain.MetaType 
 , ProductMain.Id [Product ID]
 , BplText.Text
 , BusinessPlanData.Week2
 , SystemPeriod.Year
  FROM BusinessPlanMain INNER JOIN SystemPeriod ON BusinessPlanMain.SystemPeriodPKey = SystemPeriod.PKey INNER JOIN BusinessPlanData ON BusinessPlanMain.PKey = BusinessPlanData.BusinessPlanMainPKey INNER JOIN BusinessPlanDataTemplate ON BusinessPlanDataTemplate.PKey = BusinessPlanData.BusinessPlanDataTemplatePKey INNER JOIN BplText ON BplText.ReferencePKey=BusinessPlanTemplate.PKey INNER JOIN ProductMain ON BusinessPlanData.ProductMainPKey=ProductMain.PKey WHERE SystemPeriod.Year IN ('2016') AND SystemPeriod.PeriodType = 'Year' AND
SystemPeriod.SalesOrg = 'OrgID1' AND BusinessPlanMain.SalesOrg='OrgID1' AND BusinessPlanData.OrgID ='OrgID1' AND BusinessPlanTemplate.Sheet='Baseline' and ProductMain.MetaType IN ('PrdGroup','Product')
.
.
.

继续54个工会

The Result of the above Query is

enter code here

1 个答案:

答案 0 :(得分:0)

您要使用的概念而不是所有这些UNION子查询称为UNPIVOT。以下是使用原始查询的粗略示例 - 您需要研究该主题并更正以下代码:

WITH OriginalDataSet as (
   [your first query here]
)

SELECT ods.Id
 , ods.SalesOrg
 , ods.MetaType 
 , ods.[Product ID]
 , ods.Text
 , ods.SystemPeriod
 , BusinessPlanWeek
 , BusinessPlanWeekData
FROM OriginalDataSet ods
UNPIVOT (BusinessPlanWeekData FOR BusinessPlanWeek In
([Week 1], [Week 2], [Week 3], [Week 4], [Week 5], ...)) AS unpvt