SQL索引

--索引 index
create [ unique ] [ clustered |nonclustered ] index index_name   
    on <object> ( column_name [ asc | desc ] [ ,...n ] ) 
    [ include ( column_name [ ,...n ] ) ]
    [ where <filter_predicate> ]
    [ with <backward_compatible_index_option> [ ,...n ] ]  
    [ on { partition_scheme_name ( column_name )
         | filegroup_name 
         | "default" 
         }
    ]  
    [ FILESTREAM_ON { filestream_filegroup_name | partition_scheme_name | "NULL" } ]
    [ ; ]


    <object> ::=  
{  
    [ database_name. [ owner_name ] . | owner_name. ]   
    table_or_view_name  
}  
  
<backward_compatible_index_option> ::=  
{   
    PAD_INDEX ={ON |OFF}            --指定是否索引填充。默认为 OFF。
  | FILLFACTOR = fillfactor         --用于指定在创建索引时,每个索引页的数据占索引页大小的百分比,fillfactor 的值为1到100。
  | SORT_IN_TEMPDB={ON |OFF}        --用于指定创建索引时的中间排序结果将存储在 tempdb 数据库中。 默认为 OFF。
  | IGNORE_DUP_KEY ={ON |OFF}       --指定在插入操作尝试向唯一索引插入重复键值时的错误响应。默认为 OFF。
  | STATISTICS_NORECOMPUTE={ON |OFF --用于指定过期的索引统计是否自动重新计算。 默认为 OFF。
  | DROP_EXISTING ={ON |OFF}        --表示如果这个索引还在表上就 drop 掉然后在 create 一个新的。 默认为 OFF。
  | ONLINE = {ON |OFF}              --表示建立索引时是否允许正常访问,即是否对表进行锁定。默认为 OFF。
}  

--创建简单允许重复索引
create index ix_Persons on Persons(id asc,name desc)

--创建唯一索引
create unique index ix_Persons on Persons(id asc,name desc)

--创建聚集索引
create clustered index ix_Persons on Persons(id asc,name desc)

--创建非聚集索引
create nonclustered index ix_Persons on Persons(id asc,name desc)

--创建唯一聚集索引
create unique clustered index ix_Persons on Persons(id asc,name desc)

--创建唯一非聚集索引
create unique nonclustered index ix_Persons on Persons(id asc,name desc)


--修改索引
alter index { index_name | ALL }
    on <object>
    { rebuild 
        [ [PARTITION = ALL]
                    [ WITH ( <rebuild_index_option> [ ,...n ] ) ] 
          | [ PARTITION = partition_number 
                [ WITH ( <single_partition_rebuild_index_option>
                        [ ,...n ] )
                ] 
            ]
        ]
    | disable
    | REORGANIZE 
        [ PARTITION = partition_number ]
        [ WITH ( LOB_COMPACTION = { ON | OFF } ) ]
  | SET ( <set_index_option> [ ,...n ] ) 
    }
[ ; ]


--禁用名为 NonClu_Index 的索引
alter index NonClu_Index on Student disable

--删除指定表 Student 中名为 Index_StuNo_SName 的索引
drop index Student.Index_StuNo_SName

--检查表 Student 中索引 UQ_S_StuNo 的碎片信息
dbcc showcontig(Student,UQ_S_StuNo)

--整理 Test 数据库中表 Student 的索引 UQ_S_StuNo 的碎片
dbcc indexdefrag(Test,Student,UQ_S_StuNo)

--更新表 Student 中的全部索引的统计信息
update statistics Student

 

posted @ 2019-04-11 23:25  唧唧复唧唧木兰当户织  阅读(447)  评论(0编辑  收藏  举报