[英]Table with Unique Clustered Index and an Indexed View are stored in same way. What is the benefit of Indexed View then?
[英]Unique filtered index and indexed view are ignored by optimizer
假设我有一个这样的表模式:
Partners
表
ID Name
1 Test
Partners_Codes
表
Partner_ID Code
1 'Test_Code'
我在Partners_Codes
Partner_ID
上也有唯一的聚簇索引:
create unique clustered index IX_Partners_Codes on Partners_Codes (Partner_ID);
现在,当我这样查询时:
select
P.ID, P.Name
from dbo.Partners as P
left outer join dbo.Partners_Codes as PC on PC.Partner_ID = P.ID;
SQL Server优化器非常智能,可以在Partners_Codes
上查看索引,而不是查询Partners_Codes,这有利于提高性能。
现在假设我必须向Partners_Codes
添加一个Active
列,因此我可以为每个Partner
提供许多代码,但只有一个代码可以是Active
。
Partners_Codes2
表
Partner_ID Code Active
1 'Test_Code' 1
1 'Test_Code_old1' 0
1 'Test_Code_old2' 0
我尝试了两种方法 - 在Active = 1
上使用过滤索引或在此视图上创建视图和唯一索引:
create table Partners_Codes2 (Partner_ID int, Code nvarchar(128), Active bit);
create view vw_Partners_Codes2
with schemabinding
as
select
Partner_ID, Code
from dbo.Partners_Codes2
where Active = 1;
create unique clustered index IX_vw_Partners_Codes2 on vw_Partners_Codes2 (Partner_ID);
create table Partners_Codes3 (Partner_ID int, Code nvarchar(128), Active bit);
create unique clustered index IX_Partners_Codes31 on Partners_Codes3 (Partner_ID, Code);
create unique nonclustered index IX_Partners_Codes32 on Partners_Codes3 (Partner_ID) include(Code, Active) where (Active = 1);
但是对于这两种方法,SQL Server优化器将查询Partners_Codes
表,即使它应该知道表中只有一行或零行,并且我没有从表中获取任何数据。
我的实际架构有点复杂,我不想将数据拆分成几个表。 问题是 - 是否可以创建过滤索引或索引视图,以便优化程序在显示的情况下使用它?
相关链接:
我刚才读到这篇文章 ,我发现我错过了最后一个字符串:
即使在Enterprise Edition中也需要NOEXPAND提示,以确保优化程序使用视图索引提供的唯一性保证。
我认为这篇文章应该在文章开头用粗体50字体书写。
所以我刚刚改变了我的查询:
select
P.ID
from dbo.Partners as P
left outer join dbo.vw_Partners_Codes2 as PC with (noexpand) on PC.Partner_ID = P.ID;
它工作正常!!!
更多链接:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.