雖然在你的簡單示例中事情將是一樣的使用嵌套視圖時需要謹慎。
我在一個系統上工作,在這個系統上,在大約6級嵌套視圖上建立了30秒後,查詢超時,並且通過重寫基本表的查詢,設法加快了約100倍。
下面是一個可能出現的問題類型的簡單示例。
CREATE VIEW MaxTypes
AS
SELECT
[number],
MAX(type) AS MaxType
FROM [master].[dbo].[spt_values]
GROUP BY [number]
GO
CREATE VIEW MinTypes
AS
SELECT
[number],
MIN(type) AS MinType
FROM [master].[dbo].[spt_values]
GROUP BY [number]
GO
SET STATISTICS IO ON
SELECT MaxTypes.number, MinTypes.MinType, MaxTypes.MaxType
FROM MinTypes INNER JOIN
MaxTypes ON MinTypes.number = MaxTypes.number
ORDER BY MaxTypes.number
/*
Gives
Table 'spt_values'. Scan count 2, logical reads 16, physical reads 0,
read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
*/
GO
SELECT
[number],
MAX(type) AS MaxType,
MIN(type) AS MinType
FROM [master].[dbo].[spt_values]
GROUP BY [number]
ORDER BY [number]
/*
Gives
Table 'spt_values'. Scan count 1, logical reads 8, physical reads 0,
read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
*/
+1雖然其他答案對於所發佈的具體示例是正確的,但在一般情況下,您的答案是正確的,即取決於查詢!物化的VIEW也值得一提。 – onedaywhen 2010-07-12 09:08:37
那麼我們假設經驗法則是您不創建視圖視圖? – MichaelD 2010-07-16 14:31:20
@MichaelD - 我認爲經驗法則應該是,當處理嵌套視圖時,檢查執行計劃並驗證視圖的使用不會引入額外的不必要的操作符(特別是如果您有意見加入視圖) – 2010-07-16 14:39:57