日期:2014-05-19  浏览次数:20454 次

为什么这个语句会使用索引
创建了一个表t2,设置其中的id列为簇索引,向其中插入2000条数据
create   table   t2
(
      id   int   primary   key,
      id2   int   identity(1,1)
)
go  
declare   @n   int
set   @n=0
while   @n <2000
begin
insert   into   t2(id)   values(@n)
set   @n=@n+1
end


然后使用如下sql:select   id2   from   t2
我察看执行计划时发现使用了id列上的索引,这是为什么阿?
我没有在id2列上建立索引阿



------解决方案--------------------
primary key 和 unique 约束会自动建立唯一性索引。
------解决方案--------------------
sql server 的 unique 是靠索引来实现的.......
pk当然也是

select id2 from t2
默认会使用 pk的排序 规则