• SQL Server 索引重建或索引重組


    查询索引的碎裂状态T-SQL语法(适用于SQL Server 2005以上):

    SELECT OBJECT_NAME(dt.object_id)      ,
    
            si.name                        ,
    
            dt.avg_fragmentation_in_percent,
    
            dt.avg_page_space_used_in_percent
    
    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(), NULL, NULL, NULL, 'DETAILED')
    
            WHERE   index_id <> 0
    
            ) AS dt --does not return information about heaps
    
            INNER JOIN sys.indexes si
    
            ON     si.object_id = dt.object_id
    
               AND si.index_id  = dt.index_id
    



     

    索引重组的时机

        *检查 Externalfragmentation 部分

             o 当avg_fragmentation_in_percent 的值介于 10 到 15 之间

        *检查 Internalfragmentation 部分

             o 当avg_page_space_used_in_percent 的值介于 60 到 75 之间

     

    索引重建的时机

        *检查 Externalfragmentation 部分

             o 当avg_fragmentation_in_percent 的值大于 15

        *检查 Internalfragmentation 部分

             o 当avg_page_space_used_in_percent 的值小于 60

     

    调整过的自动帮你算出哪些索引需要被重建或重组T-SQL 语法:

    SELECT 'ALTER INDEX [' + ix.name + '] ON [' + s.name + '].[' + t.name + '] ' +
    
           CASE
    
                  WHEN ps.avg_fragmentation_in_percent > 15
    
                  THEN 'REBUILD'
    
                  ELSE 'REORGANIZE'
    
            END +
    
            CASE
    
                  WHEN pc.partition_count > 1
    
                  THEN ' PARTITION = ' + CAST(ps.partition_number AS nvarchar(MAX))
    
                  ELSE ''
    
            END,
    
            avg_fragmentation_in_percent
    
    FROM    sys.indexes AS ix
    
            INNER JOIN sys.tables t
    
            ON     t.object_id = ix.object_id
    
            INNER JOIN sys.schemas s
    
            ON     t.schema_id = s.schema_id
    
            INNER JOIN
    
                  (SELECT object_id                   ,
    
                          index_id                    ,
    
                           avg_fragmentation_in_percent,
    
                          partition_number
    
                  FROM  sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL)
    
                  ) ps
    
            ON     t.object_id = ps.object_id
    
               AND ix.index_id = ps.index_id
    
            INNER JOIN
    
                  (SELECT  object_id,
    
                           index_id ,
    
                           COUNT(DISTINCT partition_number) AS partition_count
    
                  FROM     sys.partitions
    
                  GROUP BY object_id,
    
                           index_id
    
                  ) pc
    
            ON     t.object_id              = pc.object_id
    
               AND ix.index_id              = pc.index_id
    
    WHERE   ps.avg_fragmentation_in_percent > 10
    
        AND ix.name IS NOT NULL
    


     

    参考: http://blog.miniasp.com/post/2009/01/18/Let-SQL-Server-Tell-You-Which-Indexes-to-Rebuild-or-Reorganize.aspx

  • 相关阅读:
    Codeforces Round #631 (Div. 1) B. Dreamoon Likes Sequences 题解(思维+求贡献)
    牛客练习赛66 C公因子 题解(区间gcd)
    evincevim控喜欢的pdf阅读器
    水手郑智化
    使用diskpart管理自己的分区
    Hacker's Browser
    How Browser Works
    解决vim ctags R失败的问题
    ubuntu更改登录对话框
    使用bcdedit删除多个Windows系统
  • 原文地址:https://www.cnblogs.com/chinalantian/p/2128150.html
Copyright © 2020-2023  润新知