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 @   唧唧复唧唧木兰当户织  阅读(460)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示