就像你可能知道的那樣,你不能通過自聯接爲一個視圖建立索引。實際上,甚至連同一張表的兩個連接,即使它不是技術上的自連接。微軟的幾個人提出了一個解決方案。但它太複雜了,我不明白!需要一些自我加入問題的認真幫助
的問題的解決方案是在這裏:http://jmkehayias.blogspot.com/2008/12/creating-indexed-view-with-self-join.html
我想圍繞應用這項工作的視圖是:
create VIEW vw_lookup_test
WITH SCHEMABINDING
AS
select
count_big(*) as [count_all],
awc_txt,
city_nm,
str_nm,
stru_no,
o.circt_cstdn_nm [owner],
t.circt_cstdn_nm [tech],
dvc.circt_nm,
data_orgtn_yr
from
((dbo.dvc
join dbo.circt
on dvc.circt_nm = circt.circt_nm)
join dbo.circt_cstdn o
on circt.circt_cstdn_user_id = o.circt_cstdn_user_id)
join dbo.circt_cstdn t
on dvc.circt_cstdn_user_id = t.circt_cstdn_user_id
group by
awc_txt,
city_nm,
str_nm,
stru_no,
o.circt_cstdn_nm,
t.circt_cstdn_nm,
dvc.circt_nm,
data_orgtn_yr
go
任何幫助將大大apreciated!
非常感謝!
編輯:所以我發現這也將工作。請注意,我在第一個索引視圖中加入表格一次,第二次在第二個非索引視圖中加入表格。
alter VIEW vw_lookup_owner_test2
WITH SCHEMABINDING
AS
select
count_big(*) as [countAll],
awc_txt,
city_nm,
str_nm,
stru_no,
dvc.circt_nm,
circt_cstdn_nm,
data_orgtn_yr,
dvc.circt_cstdn_user_id
from dbo.dvc
join dbo.circt
on dvc.circt_nm = circt.circt_nm
join dbo.circt_cstdn o
on circt.circt_cstdn_user_id = o.circt_cstdn_user_id
group by
awc_txt,
city_nm,
str_nm,
stru_no,
dvc.circt_nm,
circt_cstdn_nm,
data_orgtn_yr,
dvc.circt_cstdn_user_id
go
和
CREATE UNIQUE CLUSTERED INDEX [idx_vw_lookup_owner2_test1] ON [dbo].[vw_lookup_owner_test2]
(
[awc_txt] ASC,
[city_nm] ASC,
[str_nm] ASC,
[stru_no] ASC,
[circt_nm] ASC,
[circt_cstdn_nm] ASC,
[data_orgtn_yr] ASC,
[circt_cstdn_user_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
和
create view vw_lookup_dvc_loc
as
select
awc_txt,
city_nm,
str_nm,
stru_no,
circt_nm,
o.circt_cstdn_nm as [owner],
--o.circt_cstdn_user_id,
t.circt_cstdn_nm as tech,
data_orgtn_yr
from vw_lookup_owner_test2 o With (NOEXPAND)
join circt_cstdn t
on o.circt_cstdn_user_id = t.circt_cstdn_user_id
group by
awc_txt,
city_nm,
str_nm,
stru_no,
circt_nm,
o.circt_cstdn_nm,
data_orgtn_yr,
t.circt_cstdn_nm
--o.circt_cstdn_user_id
然後我可以在第一視圖中創建additon指標如我所願。我不確定此解決方案(或解決方案)是否會提高性能,但我會通知您。
是您最後一次查詢您發佈了另一個視圖嗎?因爲我的理解是我不能索引引用另一個視圖的視圖......或者我要索引第一個視圖?並使用第二個...? – kralco626 2010-06-16 12:03:22
O.K.我重讀了你的帖子,我很確定我會索引第一個視圖。但是,我如何索引第一個視圖? – kralco626 2010-06-16 12:06:51
好吧!我創建了第一個視圖。創建了一個超過8列的獨特聚集索引,然後我從第二個視圖運行select語句,並獲得了300486行而不是299925,這是我運行原始語句時得到的數量。大聲笑你preformed魔術:)你創造了561行...任何想法? – kralco626 2010-06-16 13:52:07