我将尽可能简化这一过程。经过数小时的调试,我发现了DRASTIC速度下降的根源。
DECLARE @Id INT = 9999
SELECT *
FROM dbo.FN_Whatever(@Id, NULL, NULL)
执行时间:15分钟
SELECT *
FROM dbo.FN_Whatever(9999, NULL, NULL)
执行时间:0秒
请帮助我了解这个令人沮丧的问题...
以下是该功能的摘要(私人公司代码等等)
CREATE FUNCTION FN_Whatever
(@Id INT,
@V1 INT = NULL,
@V2 INT = NULL)
RETURNS TABLE
AS
RETURN
SELECT T1.Id, T1.Value
FROM Table T1
LEFT JOIN (SELECT X
FROM Table T2
WHERE Id = @Id
AND (@V1 IS NULL OR x = @V1)
AND (@V2 IS NULL OR y = @V2)
GROUP BY x, y, z) SH_List ON ...
JOIN
FN_2 (@Id, @V1) FN1 ON ...
WHERE
Id = @Id
AND (@V1 IS NULL OR x = @V1)
AND (@V2 IS NULL OR y = @V2)