SQLSERVER 創建索引視圖注意事項


一、注意點

1、索引視圖所引用的基表必須在同一個數據庫中,不是用union all引用多個數據庫的表;

2、創建索引視圖時要加上with schemabinding;

3、創建索引視圖時要指定表所屬的架構;

4、在創建索引視圖的select語句時,不能使用*,必須指定具體的列名;

5、只能為索引視圖創建唯一聚集索引;

6、索引視圖中的select包含一個或多個 UNION、INTERSECT 或 EXCEPT 運算符時,不能創建索引(創建視圖時不報錯,創建索引的時候會報錯);

 二、操作步驟

--1.創建索引視圖
create view v_customer_sch_index with schemabinding
as
select Col1,Col2 from dbo.customer
go

--2.創建普通視圖
create view v_customer
as
select Col1,Col2 from dbo.customer
union all
select Col1,Col2 from dbo.customer2007
union all
select Col1,Col2 from dbo.customer2008
go

--3.為索引視圖創建唯一聚集索引
create unique clustered index cust_uniquetb on v_customer_sch_index(Col1)
go

--4.查看聚集索引有多少行以及視圖占用多少空間
EXECUTE sp_spaceused 'v_customer_sch_index'

 --5.查詢索引視圖強制走索引(with (NOEXPAND) )

select * from run.dbo.v_customer_sch_index with (NOEXPAND) where Col1='998628'

 --6.再次插入數據的執行計划

INSERT INTO run.dbo.customer SELECT * FROM run.dbo.customer2008


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM