SQL最適化--索引


Sys.dm_exec_requests   SQL Server         

update statistics t_goods with fullscan---        
               。
                       (           key)
 


      :
           。                               。
                               。
              。


      ,SQL Server          。  ,SQL Server                            。      ,          (   、       ),SQL Server            “  ” ,    。       DBCC SHOWCONTIG         。         “  ” ,         UPDATE STATISTICS  ,  SQL Server             。

                  ,          、    、    、              
               ,      ,              ,              ,            。

          ,           ,         ,           B        。
                  ,        ,                    ,          、                   

            ,          ,                                 ,            ,                    。

--//////////
.    where        ,        。  SQL              ,                     ;           。   ,            ,         ,              。


    :
            :
select id from usertable with(index(   )) where code=@code   select * from usertable with(index=index_name) wherecode=@code--        

--//////////

          clustered      ,   clustered                     ,                     ,         。            clustered      ,                clustered   。

    (clustered indexes)   

                                    。                   ,                         。            ,               120%     ,              。                  。                             。              。              。                        。

      ,               ,        ,           (between、<、<=、>、> =)   group by order by    ,                ,                           ,        ,          。

                     ,             (  IDENTITY     ) ,           。
                ,       ,            。
          where          。         :

  ●    ,   where             。

  ●        , p_order > 100 and p_order < 1000。

  ●  group by order by     。

  ●        。

  ●           。

  :                ,     drop index   create index                      。      CREATE CLUSTERED INDEX IX_TB(CODE) WITH( DROP_EXISTING=ON)          。
                CREATE NONCLUSTERED INDEX IX_TB(CODE) WITH(DROP_EXISTING=ON)

CREATE NONCLUSTERED INDEX IX_TB ON TABLE(C1,code)
with (data_compression=row) ----            (       )
CREATE NONCLUSTERED INDEX IX_TB ON TABLE(C1,code)
with (data_compression=page) ----            (       )

     (nonclustered indexes)   

                       (       )。                     。             。             (    ),            。

  SQL Server                ,                 ,                           。                             。            ,  250      。                    。         ,                         。  ,        :

  ●           。

  ●         。

  ●          ,        。

  ●         。

           ,                             。           ,                      ,             。                    ,  ,                ,         。  ,            。             :

  ●          ( Sum,....)。

  ●      join,order by,group by。

  ●                20%。

                                       。、
              ,       ID 、 ID             。 (1:85691:10)     ,85691  10   。
                 ,               。                     。 


    ( covering indexes)   

            SQL                      。                          ,           。
        :SQL                            ,                      ,             。               ,               。

                 ,         。  update            。                    ,               。

            :                     (       )  。               (   )      。                    。
            (      ):
 1、           ,              。         (     )             。
 2、                  。
         :
  create index fugai_index on mytable(ProductID) INCLUDE (COLUMN1,COLUMN2)
                ,                  .      .

     :                                  。
                         ,                  。
          SELECT    where        。Where         ,select     。

 

  (  )  

   :  CRATE NONCLUSTERED INDEX IX_WHERE_QTY ON Production.product(qty) where qty is null; --where    。           。

                       ,        ,          。             ,                                 。
   :       NULL 。                      ,          。           ,        、CPU  、I/O  。              。
              :
  ANSI_NULLS=ON,ANSI_PADDING=ON,ANSI_WARNINGS=ON,ARITHABORT=ON,CONCAT_NULL_YIELDS_NULL=ON,QUOTED_IDENTIFIER=ON
  NUMERIC_ROUNDABORT=OFF


 
           、       ,                ,        ,            ,         ,             。

       

            :         。

      :         ,          ,      ,          ,         ,         。

      :       ,              ,  5K     ,   4k        5k   ,    1k     。

             ?
 SELECT object_name(dt.object_id) Tablename,si.name as IndexName,
  dt.avg_fragmentation_in_percent AS ExternalFragmentation,
  dt.avg_page_space_used_in_percent AS InternalFragmentation
  FROM
  ( SELECT object_id,index_id,avg_fragmentation_in_percent,
     avg_page_space_used_in_percent
    FROM sys.dm_db_index_physical_stats (db_id(N'Test'),NULL,NULL,NULL,NULL)
     WHERE index_id <> 0) AS dt 
   INNER JOIN sys.indexes si ON si.object_id=dt.object_id
     AND si.index_id=dt.index_id AND dt.avg_fragmentation_in_percent>10
     AND dt.avg_page_space_used_in_percent<75 
     ORDER BY avg_fragmentation_in_percent DESC

ExternalFragmentation  >10              .
InternalFragmentation  <75              。

        ?

              :

  1)        :       

  ALTER INDEX ALL ON TableName REORGANIZE

  2)    :       

  ALTER INDEX ALL ON TableName REBUILD WITH (FILLFACTOR=100,ONLINE=ON)

               “ALL”            ,     SQL Server              。          ,                 。DBCC INDEXDEFRAG   DBCC DBREINDEX    clustered nonculstered         。INDEXDEFRAG       (    ,           ,   ), DBREINDEX         。        ,             ,                                  。           ,INDEXDEFRAG           ,                  (transactional block)。

       ,        ?

               10-15  ,       60-75       ,           。

             ,          ,       ,        ,          ,                  ,    , SQL Server 2005  ,           ,      , ONLINE     ON,                    。

              ,                 ,           ,              ,                ,   OLTP                      。

              ,          5   ,             ,         10       。
  

     

                     ,       where   join    。            

  (1)、      where     ,              ,              。
  (2)、                 、      ,            ,         。
  (3)、        ,                            ,                ,                 。
  (4)、                ,              。
  (5)、                 ,  fillfactor(    )      ,              。          ,    fillfactor  100。
  (6)、       ,                       

                   ,      ,                   。  ,           ,                     。

     

          、         ,              ,         I/O   ,             ,    I/O   。            B-   。            :

  (1)、            。
  (2)、         。
  (3)、              。
  (4)、    I/O               。
  (5)、        ,                           。
  (6)、dbcc        。

          ,              

         

                    ,SQL Server                      :       。                         。                。    ,         ,           ,                。  ,          update statistics  :

  (1)、                 。
  (2)、  truncate table            。
  (3)、       。


                                    

         (   )

           ,                       ,             

             order by    ,           

                   

                     ,       

            .

          5   。

  ORDER BY   GROUP BY            

            ,       SQLSERVER             。

         、   、   、          、                           。

                 10%  ,                     ,      ,     。

              , where   ,           ,                 .  :
  :select * from dept where sal*12 >2500;
  :select * from dept where sal>2500/12;

         not  “!=”,             ,             ,      not   “!=” ,                。

    >=  >
   :select * from t_a  where deptno > 3
   :select * from t_a where deptno >=4
       ,  dbms        deptno  4   ,         deptno  3             deptno  3 。

        :NOT IN、NOT BETWEEN、LIKE(      %   , name like ' %')、<>、IS NULL/IS NOT、NULL、        

      、        (between, >,< ,>=,< =) order by、group by    ,        ;

        ,                  ;

                ; 
group by order by                ;