SQL Server 建立索引方法

转自 《SQL Server 建立索引的 5 种方法》 地址:https://www.cnblogs.com/JiangLe/p/4007091.htmlhtml

前期准备:性能

            create table Employee (
            ID int not null primary key,
            Name nvarchar(4),
            Credit_Card_ID varbinary(max)); --- 当心这种数据类型。
            gospa

            说明:本表上的索引,都会在建立下一个索引前删除。3d

-------------------------------------------------------------------------------------------------------------------------------------------------------------htm

操做 一、blog

         建立汇集索引索引

         方法 一、get

               alter table table_name add constraint cons_name priamry key(columnName ASC|DESC,[.....]) with (drop_existing = on);it

               alter table Employee
               add constraint PK_for_Employee primary key clustered (ID);
               goio

               这个是一种特别的方法,由于在定义主键的时候,会自动添加索引,好在加的是汇集索引仍是非汇集索引是咱们人为能够控制的。

               经过sp_helpindex 能够查看表中的索引

               execute sp_helpindex @objname = 'Employee';

               go

               

              注意、

                     这个索引是没法删除的,不信! 你去删一下

                     drop index Employee.PK__Employee__3214EC277D95E615;

                     go

                     

        方法 二、

                 create clustered index ix_name on table_name(columnName ASC|DESC[,......]) with (drop_existing = on);方法

                 create clustered index ix_clu_for_employee_ID on Employee(ID);

                 go

                 查看建立的索引

                 

 

操做 二、

         建立复合索引

         create index ix_com_Employee_IDName on Employee (ID,Name)with (drop_existing = on);

         

        这样就算是建立一个复合索引了,不过脚下的路很长,咱们看下一个复合索引的例句:

        create index ix_com_Employee_IDCreditCardID on Employee(ID,Credit_Card_ID);

        看到这句话,你先问一下本身它有没有错!

        

       能够发现它错了,varbinary是不能够建索引的。

操做 三、

         建立覆盖索引

         create index index_name on table_Name (columnName ASC|DESC[,......]) include(column_Name_List)with (drop_existing = on);

         create index ix_cov_Employee_ID_Name on Employee (ID) include(Name);
         go

         首先,覆盖索引它只是非汇集索引的一种特别形式,下文说的非汇集索引不包涵覆盖索引,固然这个约定只适用于这一段话,这样作的

         目的是为了说明各中的区别。

         首先:

               一、非汇集索引不包涵数据,经过它找到的只是文件中数据行的引用(表是堆的状况下)或是汇集索引的引用,SQL Server

                    要通这个引用去找到相应的数据行。

               二、正由于非汇集索引它没有数据,才引起第二次查找。

               三、覆盖索引就是把数据加到非汇集索引上,这样就不须要第二次查找了。这是一种以空间换性能的方法。非汇集索引也是。

                    只是作的没有它这么出格。

操做 四、

         建立惟一索引

         create unique index index_name on table_name (column ASC|DESC[,.....])with (drop_existing = on);

         正如我前面所说,在建立表上的索引前,我会删除表上的全部索引,这里为何我要再说一下呢!由于我怕你忘了。二来这个例子用的到它。

         目前表是一个空表,我给它加两行数据。

         insert into Employee(ID,Name) values(1,'AAA'),(1,'BBB');

         

        这下咱们为表加惟一索引,它定义在ID这个列上

        create unique index ix_uni_Employee_ID on Employee(ID);
        go -- 能够想到由于ID有重复,因此它建立不了。

        

        结论 一、 若是在列上有重复值,就不能够在这个列上定义,惟一索引。

        下面咱们把表清空: truncate table Employee;

        

        接下来要作的就是先,建立惟一索引,再插入重复值。

        create unique index ix_uni_Employee_ID on Employee(ID);
        go

        

        insert into Employee(ID,Name) values(1,'AAA'),(1,'BBB');

        go

        

       

       结论 二、

                定义惟一索引后相应的列上不能够插入重复值。

操做 五、

         筛选索引

         create index index_name on table_name(columName) where boolExpression;

         create index ix_Employee_ID on Employee(ID) where ID>100 and ID< 200;
         go

         只对热点数据加索引,若是大量的查询只对ID 由 100 ~ 200 的数据感兴趣,就能够这样作。

         一、能够减少索引的大小

         二、为据点数据提升查询的性能。      

 

 

总结:

      BTree 索引有汇集与非汇集之分。

                                               就查看上到汇集索引性能比非汇集索引性能要好。

      非汇集索引分    覆盖索引,惟一索引,复合索引(固然汇集索引也有复合的,复合二字,只是说明索引,引用了多列),通常非汇集索引

                                               就查看上到非汇集索引中覆盖索引的性能比别的非汇集索引性能要好,它的性能和汇集索引差很少,但是

                                               它也不是’银弹‘ 它会用更多的磁盘空间。

 

最后说一下这个

                  with (drop_existing = on|off),加上这个的意思是若是这个索引还在表上就drop 掉而后在create 一个新的。特别是在汇集索引上

                   用使用这个就能够不会引发非汇集索引的重建。

                  with (online = on|off) 建立索引时用户也能够访问表中的数据,

                  with(pad_index = on|off fillfactor = 80); fillfactor 用来设置填充百分比,pad_index 只是用来链接fillfactor 可是它又不难少,

                  这点无语了。

                  with(allow_row_locks = on|off   |   allow_page_locks = on |off);  是否容许页锁 or 行锁

                  with (data_compression = row | page );  这样能够压缩索引大小

相关文章
相关标签/搜索