关于这种情况的最佳指数

时间:2014-07-08 16:47:10

标签: sql sql-server indexing non-clustered-index

create table #testing(id int, name varchar(50))
go

Insert Into #testing
Values(231, 'fasd')

Insert Into #testing
Values(232, 'dsffd')

Insert Into #testing
Values(233, 'xas')

Insert Into #testing
Values(234, 'asdasd')

create table #testing2(id int, name varchar(50))
go
Insert Into #testing(id)
Values(231)

Insert Into #testing(id)
Values(232)

Insert Into #testing(id)
Values(233)

Insert Into #testing(id)
Values(234)
go

update m
set name = x.name
from #testing2 m
join #testing x  
on m.id = x.id
Where m.name is null

没有关于#testing和#testing2的索引

enter image description here

#testing和#testing2

上的id列的索引

enter image description here

索引是

create nonclustered index ix_test1 on #testing1(id) include (name)
create nonclustered index ix_test1 on #testing2(id)

enter image description here

这种情况下最好的指数是什么?在#testing(name)上没有索引是正确的,因为写/更新会更慢吗?

1 个答案:

答案 0 :(得分:0)

在索引中包含列仅对读取有用。数据库中始终存在该列的另一个副本。因此,对于更新,第二个索引更好,因为数据库只需要在一个位置更新列。如果你有任何选择选择名称但只过滤id,那么第一个索引可能很有用。

要确定第一个索引是否值得,您必须检查您的环境。是否使用了第一个索引?索引上的读写比率是否读取?它是否显着提高了重要或频繁查询的速度?这是您需要回答的问题。

此外,这个问题在DatabaseAdministrators StackExchange上可能会更好,因为你会在那里找到更多的数据库专家。