我有一个非常复杂的 SQL 视图。我的问题是,当我想报告时,它非常慢并且需要大量内存。视图的作用:它查询一个总值并将其乘以一个因子显然这是因为它包含很多子查询......(如果你算上它,则为 9)
该查询在具有数百万个数据集的系统上运行。
我的问题。优化点在哪里?如果我制作准备好的程序或为子查询创建 sql 视图,它会更快吗?
SELECT
CASE BID WHEN 1 THEN
b.TOTAL
ELSE
(b.TOTAL * - 1)
END
*
CASE WHEN
(SELECT
(CAST(DATEDIFF(day,
ISNULL
((SELECT dbo.TableA.TimestampFrom + 1 AS start FROM dbo.TableB INNER JOIN
dbo.TableA ON dbo.TableB.SID = dbo.TableA.SID
WHERE (dbo.TableB.PID = dbo.TableC.PID) AND
(dbo.TableB.MString = 'S' OR dbo.TableB.MString = 'ST') AND
(dbo.TableB.TimestampCreated <
(SELECT TimestampCreated
FROM dbo.TableB AS TableB_2
WHERE (RID = dbo.TableC.RID)))
AND
(dbo.TableB.VPFrom =
(SELECT VPFrom FROM dbo.TableB AS TableB_1
WHERE (RID = dbo.TableC.RID))
)), VPFrom), VPTo)
AS Float) + 1)
/
(CAST(DATEDIFF(day, start, Vertragsende) AS Float) + 1) AS Faktor
FROM dbo.TableC
WHERE (LaufNr = b.LaufNr))
> 1
THEN
1
ELSE
(SELECT (CAST(DATEDIFF(day,
ISNULL
((SELECT dbo.TableA.TimestampFrom + 1 AS start FROM dbo.TableB INNER JOIN
dbo.TableA ON dbo.TableB.SID = dbo.TableA.SID
WHERE (dbo.TableB.PID = dbo.TableC.PID) AND
(dbo.TableB.MString = 'S' OR dbo.TableB.MString = 'ST') AND
(dbo.TableB.TimestampCreated <
(SELECT TimestampCreated FROM dbo.TableB AS TableB_2
WHERE (RID = dbo.TableC.RID)))
AND (dbo.TableB.VPFrom =
(SELECT VPFrom FROM dbo.TableB AS TableB_1
WHERE (RID = dbo.TableC.RID)))
), VPFrom),VPTo) AS Float) + 1)
/ (CAST(DATEDIFF(day, start, Vertragsende) AS Float) + 1) AS Faktor
FROM dbo.TableC
WHERE (LaufNr = b.LaufNr))
END AS GrossPremium
,RID, PID, ONR, OPosLaufNr, LaufNr, bdate
FROM dbo.TableC AS b