使用CTE sql server查看索引

时间:2017-03-13 12:43:04

标签: sql-server indexing

我创建了一个这样的视图。我可以在列上添加聚集索引"开始"它会有任何影响吗?

create view v_Weeks with SchemaBinding as 
with Periodes AS 
(
SELECT start = CONVERT(Date,'2013-08-05')
union ALL
SELECT DateAdd(day,7,start) from Periodes where start < dateadd(year,2,GETDATE())   
)
select Start,DATEADD(DAY,7,Start) as [End], datepart(ISO_WEEK,start) as week, DATEPART(YEAR,Start) as Year  from Periodes 

1 个答案:

答案 0 :(得分:1)

您需要一张日历表。这正是您在没有技术的情况下尝试做的事情。

如果你每周只需要一天,而且只需要两年就可以使用这样的东西:

declare @fromdate date = '20130805';
declare @thrudate date = '20150805';

create table v_Weeks (
    [Start]   date      not null primary key
  , iso_week  tinyint   not null
  , [year]    smallint  not null
  );

;with n as (select n from (values(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) t(n))
, dates as (
  select top (datediff(week,@fromdate,@thrudate)+1) 
    [Date]=convert(date
          ,dateadd(week, row_number() over (order by (select 1)) -1, @fromdate)
          )
  from n as deka cross join n as hecto cross join n as kilo cross join n as tenK
  order by [Date]
)

insert into v_weeks 
select 
    Start    = [Date]
  , iso_week = datepart(iso_week,[date])
  , [year]   = datepart(year,[date])
from dates ;

日历和数字表格引用: