我是性能調優存儲過程和SQL Enlight拋出一個高門檻,猜測是因爲許多自聯接。有沒有另一種方法來重寫自連接?是否有替代重寫自連接?
這是我使用的代碼。
在此先感謝。
SELECT x.ClientName
,x.Category
,x.vchSSNumber
,x.dtDateOfBirth
,x.CYC0
,x.Total0
,Limit0 = CASE
WHEN x.category = 'IRA'
THEN i0.mLimit
WHEN x.category = 'Simple'
THEN s0.mLimit
ELSE NULL
END
,Balance0 = CASE
WHEN x.category = 'IRA'
THEN CASE
WHEN i0.mLimit > 0
THEN i0.mLimit - x.Total0
END
WHEN x.category = 'Simple'
THEN CASE
WHEN s0.mLimit > 0
THEN s0.mLimit - x.Total0
END
ELSE NULL
END
,x.CYC1
,x.PYC1
,x.Total1
,i1.mLimit AS Limit1
,Balance1 = CASE
WHEN i1.mLimit > 0
THEN i1.mLimit - x.Total1
END
,x.CYC2
,x.PYC2
,x.Total2
,i2.mLimit AS Limit2
,Balance2 = CASE
WHEN i2.mLimit > 2
THEN i2.mLimit - x.Total2
END
,Over70AndHalf = CASE
WHEN x.dtDateOfBirth < @year70
THEN 1
ELSE 0
END
FROM ClientInfo x
--Only get contribution limits for IRA Category. Have to get for each year
--IF DOB is NULL, Assume it is today. This makes them under 50 yrs old
LEFT JOIN dbo.IRAContributionLimits i0 WITH (NOLOCK) ON @year0 - DATEPART(yy, ISNULL(x.dtDateOfBirth, @date)) BETWEEN i0.iMinAge
AND i0.iMaxAge
AND i0.iYear = @year0
AND i0.vchCategory = x.category
AND x.category = 'IRA'
LEFT JOIN dbo.IRAContributionLimits i1 WITH (NOLOCK) ON @year1 - DATEPART(yy, ISNULL(x.dtDateOfBirth, @date)) BETWEEN i1.iMinAge
AND i1.iMaxAge
AND i1.iYear = @year1
AND i1.vchCategory = x.category
AND x.category = 'IRA'
LEFT JOIN dbo.IRAContributionLimits i2 WITH (NOLOCK) ON @year2 - DATEPART(yy, ISNULL(x.dtDateOfBirth, @date)) BETWEEN i2.iMinAge
AND i2.iMaxAge
AND i2.iYear = @year2
AND i2.vchCategory = x.category
AND x.category = 'IRA'
LEFT JOIN dbo.IRAContributionLimits s0 WITH (NOLOCK) ON @year0 - DATEPART(yy, ISNULL(x.dtDateOfBirth, @date)) BETWEEN s0.iMinAge
AND s0.iMaxAge
AND s0.iYear = @year0
AND s0.vchCategory = x.category
AND x.category = 'Simple'
ORDER BY x.ClientName
,x.category
,x.vchSSNumber;
也許你可以離開最後的連接,在'Simple'上搜索,你可以在第一個左連接上添加例如:x.category IN('IRA','Simple'),僅當dbo.IRAContributionLimits i0不能包含數據都爲 – Thomas