Sql Server 索引的应用

概念:

数据库中的索引类似于一本书的目录,我们在看一本书的时候通过目录就可以很快的找到我们想看的内容,索引的道理也相似,主要目的是为了提高sql server 系统的性能,加快数据查询速度

索引分类:

唯一索引(UNIQUE):

每行的索引值都是唯一的,不会重复的
(如果表创建了唯一约束,那么系统将自动创建唯一索引)

聚集索引(CLUSTERED):

聚集索引相当于使用字典的拼音查找,因为聚集索引存储记录是物理上连续存在的。

非聚集索引(NONCLUSTERED)

非聚集索引就相当于字典的部首查找,非聚集索引是逻辑上的连续,物理上不连续

注意:

聚集索引在一个表中只能有一个,而非聚集索引一个表可以存放多个

创建索引:

语法:

Create [unique][clustered][nonclustered]  
Index  index_name
On table_name(列名) 

unique 唯一索引
clustered 聚集索引
noclustered 非聚集索引

例子:

--创建唯一聚集索引
create unique clustered ' 表示创建唯一聚集索引
index uo_clu_stuno  ‘----索引名称
on student(s_stuno)  '---  数据表名称(建立索引的列名)

---创建唯一非聚集索引
create unique nonclustered '---  表示创建唯一非聚集索引
index uo_clu_stuno  '  ----索引名称
on student(s_stuno) 

---创建非聚集索引
create nonclustered index nonclu_index 
on student(s_stuno)

---创建唯一索引
create unique index Nonclu_index 
on student(s_stuno)


创建复合索引:
--创建非聚集复合索引
create nonclustered index Index_StuNo_SName
on Student(S_StuNo,S_Name)


--创建非聚集复合索引,未指定默认为非聚集索引
create index Index_StuNo_SName
on Student(S_StuNo,S_Name)
with(drop_existing=on)             

在实际应用当中有些列是适合创建索引的,同样也有一些列是不适合创建索引的

适合的:

当表中的某列被频繁的用于查询,而且对该列进行排序可以创建成索引

查看索引:

EXEC index_stuno_sname

删除索引:

drop index table_name.index_stuno_sname  

猜你喜欢

转载自blog.csdn.net/qq_30631063/article/details/81868100