1.利用索引可以提高在表或视图中查找数据的速度
2.索引主要分为:
聚集索引:指表中数据行的物理存储顺序与索引顺序完全相同
非聚集索引:数据与索引分开存储,通过索引指向的地址与表中的数据发生关系
其他类型的索引:唯一索引
3.使用非聚集索引的情况:
*数据唯一性较高
*数据量较少
*已经包含聚集索引
4.创建聚集索引
唯一索引 聚集或非聚集索引
create [unique] [clustered|nonclustered]
index index_name 索引名 eg:IX_stuinfo_stuname
on table_name(column_name...)
[with fillfactor=x]填充因子(系数):指定一个0-100之间的值,表示索引页填充的百分比.一般在70-80之间
示例:
如果存在该索引,先将其删除掉
--删除索引
if exists (select * from sys.indexes where name='IX_stuinfo_stuname')
drop index stuinfo.IX_stuinfo_stuname
go
--创建索引
create unique nonclustered index IX_stuinfo_stuname on dbo.stuinfo(stuname)
go
--使用索引
select * from StuInfo with (index=IX_stuinfo_stuname)
where stuName='张三'
5.创建视图的准则
*视图名称必须遵循标识符的规则,该名称不得与该架构包含的任何表的名称相同
*SQL Server允许嵌套视图.但嵌套不得超