我是新来的性能问题。所以我不确定我的方法应该是什么。如何提高哈希匹配的外部连接的SQL Server性能问题
这是超过7分钟运行的查询。
INSERT INTO SubscriberToEncounterMapping(PatientEncounterID, InsuranceSubscriberID)
SELECT
PV.PatientVisitId AS PatientEncounterID,
InsSub.InsuranceSubscriberID
FROM
DB1.dbo.PatientVisit PV
JOIN
DB1.dbo.PatientVisitInsurance PVI ON PV.PatientVisitId = PVI.PatientVisitId
JOIN
DB1.dbo.PatientInsurance PatIns on PatIns.PatientInsuranceId = PVI.PatientInsuranceId
JOIN
DB1.dbo.PatientProfile PP On PP.PatientProfileId = PatIns.PatientProfileId
LEFT OUTER JOIN
DB1.dbo.Guarantor G ON PatIns.PatientProfileId = G.PatientProfileId
JOIN
Warehouse.dbo.InsuranceSubscriber InsSub ON InsSub.InsuranceCarriersID = PatIns.InsuranceCarriersId
AND InsSub.OrderForClaims = PatIns.OrderForClaims
AND ((InsSub.GuarantorID = G.GuarantorId) OR (InsSub.GuarantorID IS NULL AND G.GuarantorId IS NULL))
JOIN
Warehouse.dbo.Encounter E ON E.PatientEncounterID = PV.PatientVisitId
执行计划指出,有一个
查询哈希匹配右外连接,成本89%
。
没有一个右外连接查询,所以我不明白问题出在哪里。
如何使查询更有效?
首先:我没有看到你的语句使用您在.....也行的你'SELECT'列表使用'InsSub'别名任何表:你*真的*需要加入所有这些表格才能得到这两条信息? –
你可以显示哈希匹配的细节吗?什么是探测器,输出是什么?从屏幕截图中不清楚。我猜想这个谓词会导致你的问题 - '(InsSub.GuarantorID = G.GuarantorId)或(InsSub.GuarantorID IS NULL AND G.GuarantorId IS NULL)',你可能想要考虑使用两个查询,并且结合结果通常当你有这样的OR或谓词时,它会导致次优计划,而且这两个单独的查询能够更好地利用索引。 – GarethD
@GarethD也许在where子句中使用EXISTS而不是在连接中使用这两个谓词? – dfundako