我发现查询花了很长时间,因为哈希匹配左连接。 Hash Key probs是
@tempHPDW.DeliveryWMID,@tempHPEng.EngID,Db.METRC.MetMeasure.MeasureId
我读到在这些列上有索引会有所帮助。添加索引后,我应该排序。并将我的表变量更改为临时表。还可以根据这些列进行排序。
查询:
SELECT distinct
NISD.EngID,
NISD.DeliveryWMID,
MEM.MeasureId,
1
FROM #Master NISD
inner join Db.metrc.MetMeasure MEM ON MEM.MetID = nisd.MetID
LEFT OUTER JOIN
(SELECT ENG.EngID As EngID,
DMU.DeliveryWMID AS DeliveryWMID,
MEM.MeasureId AS MeasureID,
DMUT.DeliveryMoId AS DeliveryWMUTypeID
FROM @tempHPEng ENG
INNER JOIN Db.DWMU.DeliveryWorkUnit DMUt ON DMUt.EngID = ENG.EngID
INNER JOIN @tempHPDW DMU ON DMUT.DeliveryWorkUnitId = DMU.DeliveryWMID
LEFT OUTER JOIN Db.METRC.DeliveryWMScope DS
ON DS.DeliveryWorkUnitId = DMU.DeliveryWMID
AND DS.EndPeriodDttm = (select MAX(EndPeriodDttm) from Db.METRC.DeliveryScope
where DeliveryWorkUnitId = DS.DeliveryWorkUnitId
AND MetID = DS.MetID
AND EndPeriodDttm<=@LastReportingWeek)
LEFT OUTER JOIN Db.METRC.MetMeasure MEM ON MEM.MetID = DS.MetID
WHERE DS.InScopeInd = 'Y' ) MIND ON MIND.DeliveryWMID = NISD.DeliveryWMID
AND MIND.EngID = NISD.EngID
AND MIND.MeasureID = MEM.MeasureId
where nisd.DeliveryWMID <> -1 AND MIND.MeasureID IS NULL
答案 0 :(得分:0)
这可能有效 但是你的查询没有意义 你正在加入MIND.MeasureID,但后来有一个MIND.MeasureID IS NULL
SELECT distinct NISD.EngID, NISD.DeliveryWMID, MEM.MeasureId, 1
FROM #Master NISD
join Db.metrc.MetMeasure MEM
ON MEM.MetID = nisd.MetID
AND nisd.DeliveryWMID <> -1
LEFT OUTER JOIN
( SELECT ENG.EngID As EngID,
DMU.DeliveryWMID AS DeliveryWMID,
MEM.MeasureId AS MeasureID,
DMUT.DeliveryMoId AS DeliveryWMUTypeID,
rownumber() over (partition by DS.DeliveryWorkUnitId, DS.MetID order by EndPeriodDttm desc) as rowNum
FROM @tempHPEng ENG
JOIN Db.DWMU.DeliveryWorkUnit DMUt
ON DMUt.EngID = ENG.EngID
JOIN @tempHPDW DMU
ON DMUT.DeliveryWorkUnitId = DMU.DeliveryWMID
LEFT OUTER JOIN Db.METRC.DeliveryWMScope DS
ON DS.DeliveryWorkUnitId = DMU.DeliveryWMID
AND EndPeriodDttm<=@LastReportingWeek
LEFT OUTER JOIN Db.METRC.MetMeasure MEM
ON MEM.MetID = DS.MetID
WHERE DS.InScopeInd = 'Y' ) MIND
ON MIND.DeliveryWMID = NISD.DeliveryWMID
AND MIND.EngID = NISD.EngID
AND MIND.MeasureID = MEM.MeasureId
AND rownum = 1
WHERE MIND.MeasureID IS NULL