下面给出的原始存储过程T-SQL运行速度非常慢,需要大约4.5分钟才能运行以恢复2000多条记录的数据库。
以下是(原始查询):
DECLARE @Today as Smalldatetime = GetDate()
DECLARE @Organization as nvarchar(200) = replace(db_name(), '_MSCRM', '')
--drop table #DaysToAdd
IF OBJECT_ID('tempdb..#DaysToAdd', 'U') IS NULL
BEGIN
CREATE TABLE #DaysToAdd (
ipmvpopt INT,
postretrofitslamanalysisrequired BIT,
postretrofitpowermeteringrequired BIT,
daysAdded INT,
daysAddedFinal INT
)
INSERT INTO #DaysToAdd Values
(777790000, 0, 0, -14, 196),
(777790000, 0, 1, -14, 196),
(777790000, 1, 0, -14, 196),
(777790000, 1, 1, -14, 196),
(777790004, 0, 0, -56, 412),
(777790004, 0, 1, -56, 412),
(777790004, 1, 0, -79, 412),
(777790004, 1, 1, -56, 412),
(777790006, 0, 0, -56, 412),
(777790006, 0, 1, -56, 412),
(777790006, 1, 0, -79, 412),
(777790006, 1, 1, -56, 412),
(777790005, 0, 0, -56, 451),
(777790005, 0, 1, -56, 451),
(777790005, 1, 0, -56, 451),
(777790005, 1, 1, -56, 451),
(777790003, 0, 0, -56, 451),
(777790003, 0, 1, -56, 451),
(777790003, 1, 0, -56, 451),
(777790003, 1, 1, -56, 451)
END
SELECT
distinct
I.filenumber as [File #],
I.initiativename as [Initiative Name],
I.projectname as [Project Name],
I.mvplancompletedon as [M&V Plan Completion Date],
I.owneridname as [Key Account Manager],
I.approveddatestep4 as [Signed Agreement Recieved Date],
ID.initiativedetailid,
ID.mvcomment as [Comments],
[Data Incomplete] =
CASE
WHEN (ID.mvengineerid IS NULL OR ID.ipmvpopt IS NULL OR ID.requiredmeteringequipment IS NULL OR
ID.preretrofitpowermeteringrequired IS NULL OR ID.postretrofitpowermeteringrequired IS NULL OR
ID.customerpostretrofitdatarequired IS NULL OR ID.preretrofitslamanalysisrequired IS NULL OR
ID.postretrofitslamanalysisrequired IS NULL OR ID.customerbaselinedatarequired IS NULL OR
ID.baselinecorrelationrequired IS NULL OR ID.casanalysisrequired IS NULL) THEN 'Yes'
ELSE 'No'
END,
(SELECT TOP (1) [Location] = CASE WHEN (NOT S.serviceaddress_city IS NULL) THEN S.serviceaddress_city + ' / ' + S.name ELSE S.name END FROM Filtered_site S WHERE S.siteid = ID.customersiteid) as [Location],
ID.mvengineeridname as [MV Engineer],
ID.mvengineersecondaryidname as [MV Engineer Secondary],
ID.ipmvpoptname as [IPMVP Option],
STUFF((SELECT ',' + ECM.name FROM Filtered_ecm ECM JOIN Filtered_installation I on ECM.ecmid = I.ecmid WHERE I.initiativedetailid = ID.initiativedetailid for xml path('')), 1, 1, '') as [ECM Description],
(SELECT SUM(ESM.energysavings) FROM Filteredenergysavingsmeasurement ESM JOIN Filteredinstallation I on ESM.installationid = I.installationid WHERE ESM.energysavingstype=777790002 AND ESM.statecode=0 AND I.initiativedetailid=ID.initiativedetailid) as [Est. Annual Savings kWh], -- PSE Initial Review
(SELECT SUM(ESM.energysavings) FROM Filteredenergysavingsmeasurement ESM JOIN Filteredinstallation I on ESM.installationid = I.installationid WHERE ESM.energysavingstype=100000003 AND ESM.statecode=0 AND I.initiativedetailid=ID.initiativedetailid) as [Site Inspection Savings kWh], -- PSE Site Inspection
(SELECT TOP (1) ESM.MeasurementDate FROM Filteredenergysavingsmeasurement ESM JOIN Filteredinstallation I on ESM.installationid = I.installationid WHERE I.InitiativeDetailId = ID.InitiativeDetailId AND ESM.EnergySavingsType = 100000003 AND ESM.statecode=0 ORDER BY ESM.MeasurementDate Desc) as [Site Inspection Date],
[Payment Date] =
CASE
WHEN NOT (SELECT TOP (1) C.datemet FROM Filteredcommitment C WHERE C.initiativeid = I.initiativeId AND C.commitmenttype = 777790000) IS NULL THEN (SELECT TOP (1) C.datemet FROM Filteredcommitment C WHERE C.initiativeid = I.initiativeId AND C.commitmenttype = 777790000)
WHEN (SELECT SUM(C.partialpayment) FROM Filteredcommitment C WHERE C.initiativeid = I.initiativeId AND C.commitmenttype = 777790001) > 40 THEN (SELECT TOP(1) C.datemet FROM Filteredcommitment C WHERE C.initiativeid = I.initiativeId AND C.commitmenttype = 777790001 ORDER BY C.datemet Desc)
ELSE Null
END,
ID.postretrofitpowermeteringinstallationdate as [Post-Retrofit Power Metering Installation Date],
ID.postretrofitlightingloggerinstallationdat as [Post-Retrofit Lighting Loggers Installed Date],
ID.mostrecentdatacollection as [Most Recent Data Collection Date],
D.mvreportduedateextensiondays as [Final M&V Report Due Extention Days],
[Trigger Based Final Report Due Date] =
CASE
-- Inspection Date is null
WHEN (SELECT TOP (1) ESM.MeasurementDate FROM Filteredenergysavingsmeasurement ESM JOIN Filteredinstallation I on ESM.installationid = I.installationid WHERE I.InitiativeDetailId = ID.InitiativeDetailId AND ESM.EnergySavingsType = 100000003 AND ESM.statecode=0) IS NULL THEN NULL
-- A, A&C, C, and B-Others without power metering
WHEN (ID.ipmvpopt = 777790000 OR ID.ipmvpopt = 777790005 OR ID.ipmvpopt = 777790003
OR ((ID.ipmvpopt = 777790004 OR ID.ipmvpopt = 777790006) AND ID.postretrofitslamanalysisrequired <> 1 AND ID.postretrofitpowermeteringrequired <> 1)) THEN
DATEADD(d,
CASE
WHEN (ID.ipmvpopt = 777790000 AND (SELECT TOP(1) D.datereceived FROM Filtereddeliverable D WHERE D.initiativedetailid=ID.InitiativeDetailId AND D.deliverabletype=777790008) IS NULL) THEN 1 --A without schedule C
WHEN (ID.ipmvpopt = 777790000) THEN 196 --A with schedule C
WHEN (ID.ipmvpopt = 777790004 OR ID.ipmvpopt = 777790006) THEN 412 -- B-Others
ELSE 451 -- A&C or C
END,
(SELECT TOP (1) ESM.MeasurementDate FROM Filteredenergysavingsmeasurement ESM WHERE ESM.InitiativeDetailId = ID.InitiativeDetailId AND ESM.EnergySavingsType = 100000003 AND statecode=0 ORDER BY ESM.MeasurementDate Desc))
-- B-others with power metering
WHEN ((ID.ipmvpopt = 777790004 OR ID.ipmvpopt = 777790006) AND ID.postretrofitslamanalysisrequired <> 1 AND ID.postretrofitpowermeteringrequired = 1) THEN
CASE
WHEN (ID.postretrofitpowermeteringinstallationdate IS NULL) THEN NULL--'Meters Required'
ELSE DATEADD(d, 421, ID.postretrofitpowermeteringinstallationdate)
END
-- B-Ltg
WHEN ((ID.ipmvpopt = 777790004 OR ID.ipmvpopt = 777790006) AND ID.postretrofitslamanalysisrequired = 1 AND ID.postretrofitpowermeteringrequired <> 1) THEN
CASE
WHEN (ID.postretrofitlightingloggerinstallationdat IS NULL) THEN NULL--'Loggers Required'
ELSE DATEADD(d, 414, ID.postretrofitlightingloggerinstallationdat)
END
ELSE NULL--'UNKNOWN'
END,
[I&R Based Final Report Due Date] =
CASE
WHEN NOT (SELECT TOP (1) C.datemet FROM Filteredcommitment C WHERE C.initiativeid = I.initiativeId AND C.commitmenttype = 777790000) IS NULL THEN DATEADD(d, 365, (SELECT TOP (1) C.datemet FROM Filteredcommitment C WHERE C.initiativeid = I.initiativeId AND C.commitmenttype = 777790000))
WHEN (SELECT SUM(C.partialpayment) FROM Filteredcommitment C WHERE C.initiativeid = I.initiativeId AND C.commitmenttype = 777790001) > 75 THEN DATEADD(d, 365, (SELECT TOP(1) C.datemet FROM Filteredcommitment C WHERE C.initiativeid = I.initiativeId AND C.commitmenttype = 777790001 ORDER BY C.datemet Desc))
ELSE NULL
END,
D.duedate as [Final M&V Report Due Date],
D.loggersmetersrequired as [Loggers Or Meters Required],
CASE
WHEN (NOT D.mvreportauthor IS NULL) THEN 'Done'
WHEN (D.duedate IS NULL) THEN 'N/A'
WHEN (D.duedate > @Today) THEN CONVERT(nvarchar(16), DATEDIFF(d, @Today, D.duedate))
ELSE 'Overdue'
END as [Report Due In Days],
@Organization
FROM
Filteredinitiative I
JOIN Filteredinitiativedetail ID on I.initiativeid = ID.initiativeid
join Filtereddeliverable d on i.initiativeid = d.initiativeid AND D.deliverabletype = 777790035 -- M&V Final Report
and d.statecode = 0
left join #DaysToAdd A On A.ipmvpopt = ID.ipmvpopt AND a.postretrofitpowermeteringrequired = ID.postretrofitpowermeteringrequired AND A.postretrofitslamanalysisrequired = ID.postretrofitslamanalysisrequired
WHERE
-- Global Filters
I.statecode = 0
AND I.mvinvolvement = 1
AND I.initiativetype = 777790003 --Incentive
-- Query Logic
-- Final MV Report Due Date is a date
AND NOT D.duedate IS NULL
-- Final MV report author is null
AND (D.mvreportcompletiondate is NULL OR D.mvanalysiscompletiondate is NULL )
-- Final MV Report Due Date - Final MV Report Preparation Time < Current Date
AND (ID.ipmvpopt = 777790000 OR ID.ipmvpopt = 777790004 OR ID.ipmvpopt = 777790003 OR ID.ipmvpopt = 777790005 OR ID.ipmvpopt = 777790006)
AND DATEADD(d, A.daysAdded, D.duedate) < @Today
DROP TABLE tempdb..#DaysToAdd
我尝试通过删除来优化它:
成本最高的列,然后在从临时表中获取行时再添加这些列。
我将执行时间减少到3:07分钟。第一期不会让我回到预期的数据集,该数据集应该是2346行(正如原始查询所做的那样),而是带回2630条记录。
带有内联示例的列(SELECT TOP(1)...... ORDER BY和CASE等似乎代价很高。我可以优化这些并模仿上面的原始查询,以便正确获取所有数据行。)
问题: 你能告诉我一个优化这个查询的更好方法吗? 仅供参考:我们已添加有关所需表的统计信息,并具有聚簇索引和非聚簇索引。 有没有更好的方法来正确优化此查询? 有没有办法将那些SELECT TOP ... ORDER BY移动到TEMP表中并减少优化它。?
我的优化查询如下:
DECLARE @Today as Smalldatetime = GetDate()
DECLARE @Organization as nvarchar(200) = replace(db_name(), '_MSCRM', '')
IF OBJECT_ID('tempdb..#DaysToAdd', 'U') IS NULL
BEGIN
CREATE TABLE #DaysToAdd (
ipmvpopt INT,
postretrofitslamanalysisrequired BIT,
postretrofitpowermeteringrequired BIT,
daysAdded INT,
daysAddedFinal INT
)
INSERT INTO #DaysToAdd Values
(777790000, 0, 0, -14, 196),
(777790000, 0, 1, -14, 196),
(777790000, 1, 0, -14, 196),
(777790000, 1, 1, -14, 196),
(777790004, 0, 0, -56, 412),
(777790004, 0, 1, -56, 412),
(777790004, 1, 0, -79, 412),
(777790004, 1, 1, -56, 412),
(777790006, 0, 0, -56, 412),
(777790006, 0, 1, -56, 412),
(777790006, 1, 0, -79, 412),
(777790006, 1, 1, -56, 412),
(777790005, 0, 0, -56, 451),
(777790005, 0, 1, -56, 451),
(777790005, 1, 0, -56, 451),
(777790005, 1, 1, -56, 451),
(777790003, 0, 0, -56, 451),
(777790003, 0, 1, -56, 451),
(777790003, 1, 0, -56, 451),
(777790003, 1, 1, -56, 451)
END
SELECT
distinct
I.filenumber,
I.initiativename,
I.projectname,
I.mvplancompletedon,
I.owneridname,
I.approveddatestep4,
ID.initiativedetailid,
ID.mvcomment,
ID.mvengineeridname,
ID.mvengineersecondaryidname,
ID.ipmvpoptname,
ID.postretrofitpowermeteringinstallationdate,
ID.postretrofitlightingloggerinstallationdat,
ID.mostrecentdatacollection,
D.mvreportduedateextensiondays,
D.duedate as duedate,
D.loggersmetersrequired,
@Organization as Organization,
D.mvreportcompletiondate,
D.mvanalysiscompletiondate,
ID.mvengineerid,
ID.ipmvpopt,
ID.requiredmeteringequipment,
ID.preretrofitpowermeteringrequired,
ID.postretrofitpowermeteringrequired,
ID.preretrofitslamanalysisrequired,
ID.postretrofitslamanalysisrequired,
ID.customerbaselinedatarequired,
ID.baselinecorrelationrequired,
ID.casanalysisrequired,
ID.customersiteid,
ID.customerpostretrofitdatarequired,
D.mvreportauthor,
I.initiativeId
INTO tempdb..#MainTable
FROM
Filteredinitiative I
JOIN Filteredinitiativedetail ID on I.initiativeid = ID.initiativeid
join Filtereddeliverable d on i.initiativeid = d.initiativeid AND D.deliverabletype = 777790035 -- M&V Final Report
and d.statecode = 0
left join #DaysToAdd A On A.ipmvpopt = ID.ipmvpopt AND a.postretrofitpowermeteringrequired = ID.postretrofitpowermeteringrequired AND A.postretrofitslamanalysisrequired = ID.postretrofitslamanalysisrequired
WHERE
-- Global Filters
I.statecode = 0
AND I.mvinvolvement = 1
AND I.initiativetype = 777790003 --Incentive
-- Query Logic
-- Final MV Report Due Date is a date
AND D.duedate IS NOT NULL
-- Final MV report author is null
AND (D.mvreportcompletiondate is NULL OR D.mvanalysiscompletiondate is NULL )
-- Final MV Report Due Date - Final MV Report Preparation Time < Current Date
AND (ID.ipmvpopt = 777790000 OR ID.ipmvpopt = 777790004 OR ID.ipmvpopt = 777790003 OR ID.ipmvpopt = 777790005 OR ID.ipmvpopt = 777790006)
AND DATEADD(d, A.daysAdded, D.duedate) < @Today
Select main.filenumber as [File #],
main.initiativename as [Initiative Name],
main.projectname as [Project Name],
main.mvplancompletedon as [M&V Plan Completion Date],
main.owneridname as [Key Account Manager],
main.approveddatestep4 as [Signed Agreement Recieved Date],
main.initiativedetailid,
main.mvcomment as [Comments],
[Data Incomplete] =
CASE
WHEN (main.mvengineerid IS NULL OR main.ipmvpopt IS NULL OR main.requiredmeteringequipment IS NULL OR
main.preretrofitpowermeteringrequired IS NULL OR main.postretrofitpowermeteringrequired IS NULL OR
main.customerpostretrofitdatarequired IS NULL OR main.preretrofitslamanalysisrequired IS NULL OR
main.postretrofitslamanalysisrequired IS NULL OR main.customerbaselinedatarequired IS NULL OR
main.baselinecorrelationrequired IS NULL OR main.casanalysisrequired IS NULL) THEN 'Yes'
ELSE 'No'
END,
(SELECT TOP (1) [Location] = CASE WHEN (NOT s.serviceaddress_city IS NULL) THEN s.serviceaddress_city + ' / ' + S.name ELSE S.name END FROM Filteredsite S WHERE S.siteid = main.customersiteid) as [Location],
main.mvengineeridname as [MV Engineer],
main.mvengineersecondaryidname as [MV Engineer Secondary],
main.ipmvpoptname as [IPMVP Option],
STUFF((SELECT ',' + ECM.name FROM Filteredecm ECM JOIN Filteredinstallation I on ECM.ecmid = I.ecmid WHERE I.initiativedetailid = main.initiativedetailid for xml path('')), 1, 1, '') as [ECM Description],
(SELECT SUM(ESM.energysavings) FROM Filteredenergysavingsmeasurement ESM JOIN Filteredinstallation I on ESM.installationid = I.installationid WHERE ESM.energysavingstype=777790002 AND ESM.statecode=0 AND I.initiativedetailid=main.initiativedetailid) as [Est. Annual Savings kWh], -- PSE Initial Review
(SELECT SUM(ESM.energysavings) FROM Filteredenergysavingsmeasurement ESM JOIN Filteredinstallation I on ESM.installationid = I.installationid WHERE ESM.energysavingstype=100000003 AND ESM.statecode=0 AND I.initiativedetailid=main.initiativedetailid) as [Site Inspection Savings kWh], -- PSE Site Inspection
(SELECT TOP (1) ESM.MeasurementDate FROM Filteredenergysavingsmeasurement ESM JOIN Filteredinstallation I on ESM.installationid = I.installationid WHERE I.InitiativeDetailId = main.InitiativeDetailId AND ESM.EnergySavingsType = 100000003 AND ESM.statecode=0 ORDER BY ESM.MeasurementDate Desc) as [Site Inspection Date],
[Payment Date] =
CASE
WHEN NOT (SELECT TOP (1) C.datemet FROM Filteredcommitment C WHERE C.initiativeid = main.initiativeId AND C.commitmenttype = 777790000) IS NULL THEN (SELECT TOP (1) C.datemet FROM Filteredcommitment C WHERE C.initiativeid = main.initiativeId AND C.commitmenttype = 777790000)
WHEN (SELECT SUM(C.partialpayment) FROM Filteredcommitment C WHERE C.initiativeid = main.initiativeId AND C.commitmenttype = 777790001) > 40 THEN (SELECT TOP(1) C.datemet FROM Filteredcommitment C WHERE C.initiativeid = main.initiativeId AND C.commitmenttype = 777790001 ORDER BY C.datemet Desc)
ELSE Null
END,
main.postretrofitpowermeteringinstallationdate as [Post-Retrofit Power Metering Installation Date],
main.postretrofitlightingloggerinstallationdat as [Post-Retrofit Lighting Loggers Installed Date],
main.mostrecentdatacollection as [Most Recent Data Collection Date],
main.mvreportduedateextensiondays as [Final M&V Report Due Extention Days],
[Trigger Based Final Report Due Date] =
CASE
-- Inspection Date is null
WHEN (SELECT TOP (1) ESM.MeasurementDate FROM Filteredenergysavingsmeasurement ESM JOIN Filteredinstallation I on ESM.installationid = I.installationid WHERE I.InitiativeDetailId = main.InitiativeDetailId AND ESM.EnergySavingsType = 100000003 AND ESM.statecode=0) IS NULL THEN NULL
-- A, A&C, C, and B-Others without power metering
WHEN (main.ipmvpopt = 777790000 OR main.ipmvpopt = 777790005 OR main.ipmvpopt = 777790003
OR ((main.ipmvpopt = 777790004 OR main.ipmvpopt = 777790006) AND main.postretrofitslamanalysisrequired <> 1 AND main.postretrofitpowermeteringrequired <> 1)) THEN
DATEADD(d,
CASE
WHEN (main.ipmvpopt = 777790000 AND (SELECT TOP(1) D.datereceived FROM Filtereddeliverable D WHERE D.initiativedetailid=main.InitiativeDetailId AND D.deliverabletype=777790008) IS NULL) THEN 1 --A without schedule C
WHEN (main.ipmvpopt = 777790000) THEN 196 --A with schedule C
WHEN (main.ipmvpopt = 777790004 OR main.ipmvpopt = 777790006) THEN 412 -- B-Others
ELSE 451 -- A&C or C
END,
(SELECT TOP (1) ESM.MeasurementDate FROM Filteredenergysavingsmeasurement ESM WHERE ESM.InitiativeDetailId = main.InitiativeDetailId AND ESM.EnergySavingsType = 100000003 AND statecode=0 ORDER BY ESM.MeasurementDate Desc))
-- B-others with power metering
WHEN ((main.ipmvpopt = 777790004 OR main.ipmvpopt = 777790006) AND main.postretrofitslamanalysisrequired <> 1 AND main.postretrofitpowermeteringrequired = 1) THEN
CASE
WHEN (main.postretrofitpowermeteringinstallationdate IS NULL) THEN NULL--'Meters Required'
ELSE DATEADD(d, 421, main.postretrofitpowermeteringinstallationdate)
END
-- B-Ltg
WHEN ((main.ipmvpopt = 777790004 OR main.ipmvpopt = 777790006) AND main.postretrofitslamanalysisrequired = 1 AND main.postretrofitpowermeteringrequired <> 1) THEN
CASE
WHEN (main.postretrofitlightingloggerinstallationdat IS NULL) THEN NULL--'Loggers Required'
ELSE DATEADD(d, 414, main.postretrofitlightingloggerinstallationdat)
END
ELSE NULL--'UNKNOWN'
END,
[I&R Based Final Report Due Date] =
CASE
WHEN NOT (SELECT TOP (1) C.datemet FROM Filteredcommitment C WHERE C.initiativeid = main.initiativeId AND C.commitmenttype = 777790000) IS NULL THEN DATEADD(d, 365, (SELECT TOP (1) C.datemet FROM Filteredcommitment C WHERE C.initiativeid = main.initiativeId AND C.commitmenttype = 777790000))
WHEN (SELECT SUM(C.partialpayment) FROM Filteredcommitment C WHERE C.initiativeid = main.initiativeId AND C.commitmenttype = 777790001) > 75 THEN DATEADD(d, 365, (SELECT TOP(1) C.datemet FROM Filteredcommitment C WHERE C.initiativeid = main.initiativeId AND C.commitmenttype = 777790001 ORDER BY C.datemet Desc))
ELSE NULL
END,
main.duedate as [Final M&V Report Due Date],
main.loggersmetersrequired as [Loggers Or Meters Required],
CASE
WHEN (main.mvreportauthor IS NOT NULL) THEN 'Done'
WHEN (main.duedate IS NULL) THEN 'N/A'
WHEN (main.duedate > @Today) THEN CONVERT(nvarchar(16), DATEDIFF(d, @Today, main.duedate))
ELSE 'Overdue'
END as [Report Due In Days],
@Organization as Organization
FROM tempdb..#MainTable main
DROP TABLE tempdb..#MainTable
答案 0 :(得分:0)
你在看执行计划吗?这将告诉您瓶颈是否存在,并帮助您了解优化工作的重点。
这是一个很好的介绍:https://www.simple-talk.com/sql/performance/execution-plan-basics/
一旦您知道查询的哪些部分很慢,您就可以询问具体的问题区域。