2011-02-10 42 views
2

我有一個5年的數據集分爲季度表。我也有一個主視圖,將它們連接在一起。當用戶需要超過四分之一的數據時,他們經常使用主視圖,而不是將多個小區連接在一起。SQL Server表值功能性能

我的問題是,一個表值函數接受日期範圍,並只返回來自必要分區的記錄比查詢整個主視圖更快?

這是我當前視圖定義:

ALTER VIEW [dbo].[loandetails_test] 
AS 
SELECT  * 
FROM   loandetails05 
where year(date) = 2005 
UNION 
SELECT  * 
FROM   loandetails06 
where year(date) = 2006 
UNION 
SELECT  * 
FROM   loandetails07 
where year(date) = 2007 
UNION 
SELECT  * 
FROM   loandetails08 
where year(date) = 2008 
UNION 
SELECT  * 
FROM   loandetails1q09 
where date >= '1/1/2009' and date < '4/1/2009' 
UNION 
SELECT  * 
FROM   loandetails2q09 
where date >= '4/1/2009' and date < '7/1/2009' 
UNION 
SELECT  * 
FROM   loandetails3q09 
where date >= '7/1/2009' and date < '10/1/2009' 
UNION 
SELECT  * 
FROM   loandetails4q09 
where date >= '10/1/2009' and date < '1/1/2010' 
UNION 
SELECT  * 
FROM   loandetails1q10 
where date >= '1/1/2010' and date < '4/1/2010' 
UNION 
SELECT  * 
FROM   loandetails2q10 
where date >= '4/1/2010' and date < '7/1/2010' 
UNION 
SELECT  * 
FROM   loandetails3q10 
where date >= '7/1/2010' and date < '10/1/2010' 
UNION 
SELECT  * 
FROM   loandetails4q10 
where date >= '10/1/2010' and date < '1/1/2011' 
union 
SELECT  * 
FROM   loandetails_CURRENT 
where date >= '1/1/2011' and date < '4/1/2011' 


GO 
+0

可能。這當然很大程度上取決於如何編寫表值函數。但唯一的方法是使用主視圖和使用表值函數來衡量性能。 – HLGEM 2011-02-10 18:47:40

回答

2

答案應該是堅實的沒有。

分區使用隱式標準進行設置,因此如果您按日期(季度)進行分區,SQL Server就會知道哪些分區將滿足查詢(假設查詢將具有日期過濾器)。檢查確認兩個(或涉及到的分區)之間的流合併的執行計劃。

我有一個情況,N個數據庫中的表(是每個筒倉一個)在主視圖中加入,就像您一樣。該master view使用爲每一個過濾器,特別是它看起來像這樣

select source=1, col1, col2, col3 
from db1.dbo.tbl 
union all 
select source=2, col1, col2, col3 
from db2.dbo.tbl 
etc 

,要求爲where source in (2,3)任何查詢自動識別,只有2 DBS需要進行搜索,並執行計劃顯示爲多。

如果是手動創建日期分區的查詢,可以

  1. 對日期範圍的索引,每個表中
  2. 力優化識別分區

這裏一個工作示例(即使沒有索引)。請注意,Q1和Q4甚至沒有在計劃中顯示。 披露:SQL Server 2008 R2的快速

select dateadd(d, number, '20100101') TheDate, * 
into Q1data 
from master..spt_values 
where type='p' and number between 1 and 370 
and datepart(quarter, dateadd(d, number, '20100101')) =1 

select dateadd(d, number, '20100101') TheDate, * 
into Q2data 
from master..spt_values 
where type='p' and number between 1 and 370 
and datepart(quarter, dateadd(d, number, '20100101')) =2 

select dateadd(d, number, '20100101') TheDate, * 
into Q3data 
from master..spt_values 
where type='p' and number between 1 and 370 
and datepart(quarter, dateadd(d, number, '20100101')) =3 

select dateadd(d, number, '20100101') TheDate, * 
into Q4data 
from master..spt_values 
where type='p' and number between 1 and 370 
and datepart(quarter, dateadd(d, number, '20100101')) =4 
GO 

create view Ydata 
with schemabinding 
as 
select TheDate, name, number, TYPE, LOW, high, status 
from dbo.Q1Data where TheDate >= '20100101' and TheDate < '20100401' 
union all 
select TheDate, name, number, TYPE, LOW, high, status 
from dbo.Q2Data where TheDate >= '20100401' and TheDate < '20100701' 
union all 
select TheDate, name, number, TYPE, LOW, high, status 
from dbo.Q3Data where TheDate >= '20100701' and TheDate < '20101001' 
union all 
select TheDate, name, number, TYPE, LOW, high, status 
from dbo.Q4Data where TheDate >= '20101001' and TheDate < '20110101' 
GO 

select * from YData where TheDate between '20100601' and '20100831' 

查詢計劃

enter image description here

回覆:更新問題

當日期範圍處理,NEVER(除少數例外)使用日期欄中的功能。這要求函數在與另一邊比較之前針對表中的所有記錄運行。

where year(date) = 2005 
===> means scan the table, for each row take the year, compare to 2005 

不如寫成

where date >= '20050101' and date < '20060101' 
===> means given a date range, use the index to seek the range 
+0

我的查詢計劃顯示索引尋找查詢中日期範圍不必要的分區。我應該指定這些不是由SQL Server創建的分區,因爲我沒有Enterprise Edition。他們是我手動創建的。 – Colin 2011-02-10 18:59:30