假设我有一个定义如下的视图:
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
我试图概念化这两者是否/如何以不同的方式处理/优化。通过“分块”单个查询并将谓词单独应用于每个查询是否有任何性能提升?
谢谢。