SQL SERVER 索引与视图学习

   索引的优点。创建唯一性索引可以确保行数据的唯一性;可以大大提高数据的检索速度;可以加速表与表之间的连接;在使用order by,group by之句时,可以减少查询中分组和排序的时间。

   索引的缺点。创建和维护索引需要耗费时间;索引占用物理空间;当对表中的数据进行,删除,更改时,索引也要动态的维护。

创建索引:

 1 CREATE [UNIQUE] [CLUSTERED | NONCLUSTERED]
 2 INDEX <index name> ON <table or view name>(<column name> [ASC|DESC][,...n])
 3 INCLUDE (<column name> [,...n])
 4 [
 5     WITH
 6     [PAD_INDEX = {ON | OFF}]
 7     [[,] FILLFACTOR = <fillfactor>]
 8     [[,] IGNORE_DUR_KEY = {ON | OFF}]
 9     [[,] DROP_EXISTING = {ON | OFF}]
10     [[,] STATISTICS_NORECOMPUTE = {ON | OFF}]
11     [[,] SORT_IN_TEMPDB = {ON | OFF}]
12     [[,] ONLINE = {ON | OFF}]
13     [[,] ALLOW_ROW_LOCKS = {ON | OFF}]
14     [[,] ALLOW_PAGE_LOCKS = {ON | OFF}]
15     [[,] MAXDOP = <maxinum degree of parallelism>
16 ]
17 [ON {<filegroup> | <partition scheme name> | DEFAULT}]

eg.

USE test1

GO

IF EXISTS(SELECT * FROM SYSINDEXES WHERE NAME=‘ceshi_index‘)

DROP INDEX ceshi_index

Create unique nonclustered index ceshi_index

ON orders(customerID)

With FILLFACTOR = 30

GO

 

修改索引

1 Alter index ceshi_index on ceshi Rebuild 
2 With (FILLFACTOR=30,IGNORE_DUP_KEY=ON)

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。