我有这个查询,它在加入查看视图后永远运行。在加入之前,需要2分钟。加入的查询本身需要1分钟。他们一起走了20多分钟。
我的问题是:
感谢。这是查询:
DECLARE @year INT=2017;
WITH data AS (
SELECT M.Brand, M.SubBrand, M.Detail, S.Units, S.OB, [PRODUCTION_DATE] = RIGHT(S.DateId,2)
, [PRODUCTION_YEAR] = LEFT(S.DateId,4), [PRODUCTION_MONTH] = SUBSTRING(CONVERT(VARCHAR(10), S.DateId),5,2) FROM [fact].[Sales] V
LEFT JOIN [dim].[Material] M ON M.MaterialId = S.MaterialId
INNER JOIN (SELECT [MaterialId]
,[ROF]
,[OB]
FROM [Objectives].[load].[FactData_KPI_Sales_OB_ROF]
WHERE ROF > 0) RFOB ON RFOB.MaterialId = S.MaterialId
WHERE LEFT(S.DateId,4) > @year
)
SELECT Brand, SubBrand, Detail, [PRODUCTION_MONTH], [PRODUCTION_YEAR], OB, [01], [02], [03], [04], [05], [06], [07], [08], [09], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31]
FROM data
Pivot (
SUM(Units)
FOR [PRODUCTION_DATE] In
([01], [02], [03], [04], [05], [06], [07], [08], [09], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])
) AS piv
答案 0 :(得分:0)
您是否尝试使用临时表而不是公用表?
DECLARE @year INT=2017;
SELECT M.Brand, M.SubBrand, M.Detail, S.Units, S.OB, [PRODUCTION_DATE] = RIGHT(S.DateId,2)
, [PRODUCTION_YEAR] = LEFT(S.DateId,4), [PRODUCTION_MONTH] = SUBSTRING(CONVERT(VARCHAR(10), S.DateId),5,2)
Into #tmpData
FROM [fact].[Sales] V
LEFT JOIN [dim].[Material] M ON M.MaterialId = S.MaterialId
INNER JOIN (SELECT [MaterialId]
,[ROF]
,[OB]
FROM [Objectives].[load].[FactData_KPI_Sales_OB_ROF]
WHERE ROF > 0) RFOB ON RFOB.MaterialId = S.MaterialId
WHERE LEFT(S.DateId,4) > @year
SELECT Brand, SubBrand, Detail, [PRODUCTION_MONTH], [PRODUCTION_YEAR], OB, [01], [02], [03], [04], [05], [06], [07], [08], [09], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31]
FROM #tmpData
Pivot (
SUM(Units)
FOR [PRODUCTION_DATE] In
([01], [02], [03], [04], [05], [06], [07], [08], [09], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])
) AS piv
答案 1 :(得分:0)
DECLARE @year INT=2017;
WITH data AS (
SELECT M.Brand, M.SubBrand, M.Detail, S.Units, S.OB, [PRODUCTION_DATE] = RIGHT(S.DateId,2)
, [PRODUCTION_YEAR] = LEFT(S.DateId,4), [PRODUCTION_MONTH] = SUBSTRING(CONVERT(VARCHAR(10), S.DateId),5,2) FROM [fact].[Sales] S
LEFT JOIN [dim].[Material] M ON M.MaterialId = S.MaterialId
INNER JOIN [Objectives].[load].[FactData_KPI_Sales_OB_ROF] RFOB
在RFOB.MaterialId = S.MaterialId上 LEFT(S.DateId,4)> @year和RFOB.ROF> 0 )