-1
我有一個非常複雜的SQL視圖。我的問題是,它非常緩慢,需要大量的內存當我想做一個報告。 查看做些什麼: 它查詢總價值,並與一個因子相乘 顯然,這是因爲它含有大量的子查詢(9如果算上的話)如何使用子查詢優化複雜的SQL查詢/視圖
查詢系統上運行數以百萬計的數據集。
我的問題。點優化它在哪裏?如果我準備好的過程或爲子查詢創建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
這可以通過表定義,數據量,索引來改進。 – Laurence
這似乎不是一個有效的查詢。有些地方可以引用別名'b',但沒有別的別名爲'b'。 – Laurence
謝謝,那是因爲我編輯了查詢。 b = bbbbb(現在我編輯了帖子,b = b ...)並且有數百萬個數據集。 – Sam