日期:2014-05-17  浏览次数:20526 次

关于是否需要为Cluster Index中的字段另外建立index
假设在表ta中Fcode,DeptNo,AcctNo为组合primary key(也就是Fcode,DeptNo,AcctNo为一个Cluster Index),假如我经常需要执行以下语句,
请问是否另外需要为Fcode或DeptNo建立索引,是单独为fcode/deptNo每个字段建立索引还是fcode与deptNo一起建立组合non-cluster索引,请问是为什么要那样处理?谢谢!

Select * from ta  where fcode='某值1'
Select * from ta  where DeptNo='某值2'
Select * from ta  where fcode='某值1' and DeptNo='某值2'

------解决方案--------------------
Select * from ta  where fcode='某值1'
--> 能用上聚集索引.

Select * from ta  where DeptNo='某值2'
--> 无法用上聚集索引,需额外在DeptNo上建索引:
create nonclustered index ix_ta_DeptNo on ta(DeptNo)

Select * from ta  where fcode='某值1' and DeptNo='某值2'
--> 能用上聚集索引.
------解决方案--------------------
没错 

------解决方案--------------------
太對了!完全正確!