假設我有一個已定義的視圖如下:TSQL - 查看VS存儲過程:當應用謂詞性能考慮
CREATE VIEW [dbo].[vData_Values] AS
SELECT ParentId, TimeStamp, Value From Table1
UNION ALL
SELECT ParentId, TimeStamp, Value FROM Table2
UNION ALL
SELECT ParentId, TimeStamp, Value FROM Table3
UNION ALL
SELECT ParentId, TimeStamp, Value FROM Table4
假設我寫反對我的視圖的查詢,如下所示:
SELECT ParentId, TimeStamp, Value
FROM vData_Values
WHERE ParentId IN (...) and TimeStamp BETWEEN '1/1/2013' AND '3/31/2013 23:59'
有沒有人希望查詢的視圖執行不同於存儲過程與視圖相同的內部查詢,但在每個步驟具有相同的WHERE
子句。例如:
CREATE PROCEDURE [dbo].[GetValues]
(
...
)
AS
BEGIN
SELECT ParentId, TimeStamp, Value From Table1
WHERE ParentId IN (...) and TimeStamp BETWEEN '1/1/2013' AND '3/31/2013 23:59'
UNION ALL
SELECT ParentId, TimeStamp, Value FROM Table2
WHERE ParentId IN (...) and TimeStamp BETWEEN '1/1/2013' AND '3/31/2013 23:59'
UNION ALL
SELECT ParentId, TimeStamp, Value FROM Table3
WHERE ParentId IN (...) and TimeStamp BETWEEN '1/1/2013' AND '3/31/2013 23:59'
UNION ALL
SELECT ParentId, TimeStamp, Value FROM Table4
WHERE ParentId IN (...) and TimeStamp BETWEEN '1/1/2013' AND '3/31/2013 23:59'
END
我想概念化如果/如何/如何這兩個將被處理/優化不同。通過「分塊」處理各個查詢並將謂詞分別應用於每個查詢,是否有任何性能提升?
謝謝。
檢查執行計劃。我希望SQL Server在這種情況下推送謂詞沒有困難。你也應該使用明確的dateformat作爲'datetime'字面值,並且可能不使用'BETWEEEN',除非時間絕對不是以秒存儲的。 – 2013-05-09 13:16:40