我正在研究SQL Server中的现有视图。我的用户现在报告性能太慢了。有人可以让我知道如何提高查询/视图的性能
查询:
Select FH.ForecastID
,FH.FiscalYear
,FH.FiscalMonth
,FH.ForecastStatusCode
,FH.ForecastStatusDescription
,WF.SubTypeID
,WF.CustomerCode
,WF.ProductID
,WF.EndCustomerName
,CU.RELShortName [Master Customer 1]
,CASE WHEN (WF.EndCustomerName <> '' ) THEN CASE WHEN (CST.[Shortname] IS NULL ) THEN CU.ShortName ELSE CST.[Shortname] END ELSE CU.ShortName END AS CustomerShortName
,CASE WHEN (WF.EndCustomerName <> '' ) THEN CASE WHEN (CST.[Shortname] IS NULL ) THEN CASE WHEN (CU.[CUSTOMER_CATEGORY] = 'ASU') THEN 'AU' ELSE 'GP' END ELSE CASE WHEN (CST.[CUSTOMER_CATEGORY] = 'ASU') THEN 'AU' ELSE 'GP' END END ELSE CASE WHEN (CU.[CUSTOMER_CATEGORY] = 'ASU') THEN 'AU' ELSE 'GP' END END AS CustomerCategory
, '' as CustomerShortName
,WF.EndCustomerName as [Disti End Customer]
,CU.CustomerName [Sold To Customer Name]
,WF.CustomerCode [Customer Sold To Number]
,CU.CustomerClass
,CASE WHEN (CU.CustomerClass <> 'Distributor' ) THEN CU.TerritoryCode ELSE WF.[Sales Office] END AS [Sales Office]
,FH.REA_Onshore [Off Shore On Shore]
,PR.ProductFamilyName [OSC Product Family(1)]
,PR.ProductClassName [OSC Product Family(2)]
,PR.ProductPartNumber
,FH.ForecastTypeID
,FH.ForecastTypeCode
,FH.ForecastTypeDescription as ForecastTypeDescription
,WF.SubType
, ISNULL(WF.CreatedDate , '') CreatedDate
, ISNULL(WF.ModifiedDate, '') ModifiedDate
,WF.ModifiedBy
,IsNull([MON-3], '0') [MON-3]
,IsNull([MON-2], '0') [MON-2]
,IsNull([MON-1], '0') [MON-1]
,IsNull([MON00], '0') [MON00]
,IsNull([MON01], '0') [MON01]
,IsNull([MON02], '0') [MON02]
,IsNull([MON03], '0') [MON03]
,IsNull([MON04], '0') [MON04]
,IsNull([MON05], '0') [MON05]
,IsNull([MON06], '0') [MON06]
,IsNull([MON07], '0') [MON07]
,IsNull([MON08], '0') [MON08]
,IsNull([MON09], '0') [MON09]
,IsNull([MON10], '0') [MON10]
,IsNull([MON11], '0') [MON11]
,IsNull([MON12], '0') [MON12]
,IsNull([MON13], '0') [MON13]
,IsNull([MON14], '0') [MON14]
from TRX_ForecastDetailFlattened WF
left Join (
SELECT ForecastID, FiscalYear, FiscalMonth, ForecastStatusCode, ForecastStatusDescription, FH.ForecastStatusID, REA_Onshore, FH.ForecastTypeID, FT.ForecastTypeCode, FT.ForecastTypeDescription
FROM PRF_ForecastHeader FH
inner Join LKP_ForecastStatus FS with (nolock) on FS.ForecastStatusID = FH.ForecastStatusID
inner Join LKP_ForecastType FT with (nolock) on FT.ForecastTypeID = FH.ForecastTypeID ) FH
on WF.ForecastID = FH.ForecastID
left Join (
select RELShortName, [ACTUAL_END_CUSTOMER_NAME],CustomerName, CustomerClass, TerritoryCode,CU.CustomerCode, ShortName ,[CUSTOMER_CATEGORY]
FROM LKP_Customer CU
left join (SELECT distinct [ACTUAL_END_CUSTOMER_NAME],[Shortname], [CUSTOMER_CATEGORY] FROM [REA_DS].[dbo].[LCUS_CUSTOMER_SHORTNAME] ) CSTM on CSTM.[ACTUAL_END_CUSTOMER_NAME] COLLATE Latin1_General_CI_AI = CU.[RELShortName] COLLATE Latin1_General_CI_AI
left Join PRF_Territory TE with (nolock) on TE.TerritoryID = CU.TerritoryID
left Join LKP_TerritoryGroup TG with (nolock) on TG.TerritoryGroupID = TE.TerritoryGroupID and TG.TerritoryGroupName = 'SALES REP COMPANY') CU
on CU.CustomerCode = WF.CustomerCode
left Join (
select ProductID, ProductPartNumber, ProductFamilyName, ProductClassName
FROM LKP_Product PR with (nolock)
left Join LKP_ProductClass PC with (nolock) on PR.ProductClassID = PC.ProductClassID
left Join LKP_ProductFamily PF with (nolock) on PF.ProductFamilyID = PR.ProductFamilyID ) PR
on PR.ProductID = WF.ProductID
left join (SELECT distinct [ACTUAL_END_CUSTOMER_NAME],[Shortname],[CUSTOMER_CATEGORY] FROM [REA_DS].[dbo].[LCUS_CUSTOMER_SHORTNAME] ) CST
on WF.EndCustomerName COLLATE Latin1_General_CI_AI = CST.[ACTUAL_END_CUSTOMER_NAME] COLLATE Latin1_General_CI_AI
Where CU.RELShortName <> '' and wf.CustomerCode <> '100000'
group by FH.ForecastID ,FH.FiscalYear ,FH.FiscalMonth ,FH.ForecastStatusCode ,FH.ForecastStatusDescription ,WF.SubTypeID ,WF.CustomerCode ,WF.ProductID
,WF.EndCustomerName ,CU.RELShortName ,WF.EndCustomerName ,CU.CustomerName ,WF.CustomerCode ,CU.CustomerClass ,FH.REA_Onshore ,PR.ProductFamilyName
,PR.ProductClassName ,PR.ProductPartNumber ,FH.ForecastTypeID ,FH.ForecastTypeCode ,FH.ForecastTypeDescription ,WF.SubType ,WF.ModifiedBy
,CST.[Shortname] ,CU.ShortName ,CST.[CUSTOMER_CATEGORY] ,CU.[CUSTOMER_CATEGORY] ,CU.TerritoryCode ,WF.[Sales Office] ,WF.CreatedDate ,WF.ModifiedDate
, [MON-3], [MON-2], [MON-1], [MON00], [MON01], [MON02], [MON03], [MON04],[MON05], [MON06], [MON07], [MON08], [MON09], [MON10], [MON11], [MON12], [MON13], [MON14]
截图:
答案 0 :(得分:0)
--one of the way to increase performance is the usage of the temp tables instead subqueries
-------------------------------------------------------------------------------------
IF OBJECT_ID('Tempdb..#FH') IS NOT NULL
DROP TABLE #FH
SELECT ForecastID ,
FiscalYear ,
FiscalMonth ,
ForecastStatusCode ,
ForecastStatusDescription ,
FH.ForecastStatusID ,
REA_Onshore ,
FH.ForecastTypeID ,
FT.ForecastTypeCode ,
FT.ForecastTypeDescription
INTO #FH
FROM PRF_ForecastHeader FH
INNER JOIN LKP_ForecastStatus FS WITH ( NOLOCK ) ON FS.ForecastStatusID = FH.ForecastStatusID
INNER JOIN LKP_ForecastType FT WITH ( NOLOCK ) ON FT.ForecastTypeID = FH.ForecastTypeID
INNER JOIN ( SELECT DISTINCT
ForecastID
FROM TRX_ForecastDetailFlattened
) WF ON WF.ForecastID = FH.ForecastID
-------------------------------------------------------------------------------------
IF OBJECT_ID('Tempdb..#CU') IS NOT NULL
DROP TABLE #CU
SELECT RELShortName ,
[ACTUAL_END_CUSTOMER_NAME] ,
CustomerName ,
CustomerClass ,
TerritoryCode ,
CU.CustomerCode ,
ShortName ,
[CUSTOMER_CATEGORY]
INTO #CU
FROM LKP_Customer CU
INNER JOIN ( SELECT DISTINCT
CustomerCode
FROM TRX_ForecastDetailFlattened
) WF ON CU.CustomerCode = WF.CustomerCode
LEFT JOIN ( SELECT DISTINCT
[ACTUAL_END_CUSTOMER_NAME] ,
[Shortname] ,
[CUSTOMER_CATEGORY]
FROM [REA_DS].[dbo].[LCUS_CUSTOMER_SHORTNAME]
) CSTM ON CSTM.[ACTUAL_END_CUSTOMER_NAME] COLLATE Latin1_General_CI_AI = CU.[RELShortName] COLLATE Latin1_General_CI_AI
LEFT JOIN PRF_Territory TE WITH ( NOLOCK ) ON TE.TerritoryID = CU.TerritoryID
LEFT JOIN LKP_TerritoryGroup TG WITH ( NOLOCK ) ON TG.TerritoryGroupID = TE.TerritoryGroupID
AND TG.TerritoryGroupName = 'SALES REP COMPANY'
-------------------------------------------------------------------------------------
IF OBJECT_ID('Tempdb..#PR') IS NOT NULL
DROP TABLE #PR
SELECT ProductID ,
ProductPartNumber ,
ProductFamilyName ,
ProductClassName
INTO #PR
FROM LKP_Product PR WITH ( NOLOCK )
INNER JOIN ( SELECT DISTINCT
ProductID
FROM TRX_ForecastDetailFlattened
) ON PR.ProductID = WF.ProductID
LEFT JOIN LKP_ProductClass PC WITH ( NOLOCK ) ON PR.ProductClassID = PC.ProductClassID
LEFT JOIN LKP_ProductFamily PF WITH ( NOLOCK ) ON PF.ProductFamilyID = PR.ProductFamilyID
-------------------------------------------------------------------------------------
SELECT FH.ForecastID ,
FH.FiscalYear ,
FH.FiscalMonth ,
FH.ForecastStatusCode ,
FH.ForecastStatusDescription ,
WF.SubTypeID ,
WF.CustomerCode ,
WF.ProductID ,
WF.EndCustomerName ,
CU.RELShortName [Master Customer 1] ,
CASE WHEN ( WF.EndCustomerName <> '' )
THEN CASE WHEN ( CST.[Shortname] IS NULL ) THEN CU.ShortName
ELSE CST.[Shortname]
END
ELSE CU.ShortName
END AS CustomerShortName ,
CASE WHEN ( WF.EndCustomerName <> '' )
THEN CASE WHEN ( CST.[Shortname] IS NULL )
THEN CASE WHEN ( CU.[CUSTOMER_CATEGORY] = 'ASU' )
THEN 'AU'
ELSE 'GP'
END
ELSE CASE WHEN ( CST.[CUSTOMER_CATEGORY] = 'ASU' )
THEN 'AU'
ELSE 'GP'
END
END
ELSE CASE WHEN ( CU.[CUSTOMER_CATEGORY] = 'ASU' ) THEN 'AU'
ELSE 'GP'
END
END AS CustomerCategory ,
'' AS CustomerShortName ,
WF.EndCustomerName AS [Disti End Customer] ,
CU.CustomerName [Sold To Customer Name] ,
WF.CustomerCode [Customer Sold To Number] ,
CU.CustomerClass ,
CASE WHEN ( CU.CustomerClass <> 'Distributor' ) THEN CU.TerritoryCode
ELSE WF.[Sales Office]
END AS [Sales Office] ,
FH.REA_Onshore [Off Shore On Shore] ,
PR.ProductFamilyName [OSC Product Family(1)] ,
PR.ProductClassName [OSC Product Family(2)] ,
PR.ProductPartNumber ,
FH.ForecastTypeID ,
FH.ForecastTypeCode ,
FH.ForecastTypeDescription AS ForecastTypeDescription ,
WF.SubType ,
ISNULL(WF.CreatedDate, '') CreatedDate ,
ISNULL(WF.ModifiedDate, '') ModifiedDate ,
WF.ModifiedBy ,
ISNULL([MON-3], '0') [MON-3] ,
ISNULL([MON-2], '0') [MON-2] ,
ISNULL([MON-1], '0') [MON-1] ,
ISNULL([MON00], '0') [MON00] ,
ISNULL([MON01], '0') [MON01] ,
ISNULL([MON02], '0') [MON02] ,
ISNULL([MON03], '0') [MON03] ,
ISNULL([MON04], '0') [MON04] ,
ISNULL([MON05], '0') [MON05] ,
ISNULL([MON06], '0') [MON06] ,
ISNULL([MON07], '0') [MON07] ,
ISNULL([MON08], '0') [MON08] ,
ISNULL([MON09], '0') [MON09] ,
ISNULL([MON10], '0') [MON10] ,
ISNULL([MON11], '0') [MON11] ,
ISNULL([MON12], '0') [MON12] ,
ISNULL([MON13], '0') [MON13] ,
ISNULL([MON14], '0') [MON14]
FROM TRX_ForecastDetailFlattened WF
LEFT JOIN #FH AS FH ON WF.ForecastID = FH.ForecastID
LEFT JOIN #CU AS CU ON CU.CustomerCode = WF.CustomerCode
LEFT JOIN #PR AS PR ON PR.ProductID = WF.ProductID
LEFT JOIN ( SELECT DISTINCT
[ACTUAL_END_CUSTOMER_NAME] ,
[Shortname] ,
[CUSTOMER_CATEGORY]
FROM [REA_DS].[dbo].[LCUS_CUSTOMER_SHORTNAME]
) CST ON WF.EndCustomerName COLLATE Latin1_General_CI_AI = CST.[ACTUAL_END_CUSTOMER_NAME] COLLATE Latin1_General_CI_AI
WHERE CU.RELShortName <> ''
AND wf.CustomerCode <> '100000'
GROUP BY FH.ForecastID ,
FH.FiscalYear ,
FH.FiscalMonth ,
FH.ForecastStatusCode ,
FH.ForecastStatusDescription ,
WF.SubTypeID ,
WF.CustomerCode ,
WF.ProductID ,
WF.EndCustomerName ,
CU.RELShortName ,
WF.EndCustomerName ,
CU.CustomerName ,
WF.CustomerCode ,
CU.CustomerClass ,
FH.REA_Onshore ,
PR.ProductFamilyName ,
PR.ProductClassName ,
PR.ProductPartNumber ,
FH.ForecastTypeID ,
FH.ForecastTypeCode ,
FH.ForecastTypeDescription ,
WF.SubType ,
WF.ModifiedBy ,
CST.[Shortname] ,
CU.ShortName ,
CST.[CUSTOMER_CATEGORY] ,
CU.[CUSTOMER_CATEGORY] ,
CU.TerritoryCode ,
WF.[Sales Office] ,
WF.CreatedDate ,
WF.ModifiedDate