zoukankan      html  css  js  c++  java
  • sqlserver2005索引维护

    SET NOCOUNT ON;
    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




    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
  • 相关阅读:
    缩点【洛谷P1262】 间谍网络
    模板-割点
    Tarjan缩点+LCA【洛谷P2416】 泡芙
    模拟赛 10-20考试记
    BFS【bzoj1667】: [Usaco2006 Oct]Cows on Skates滑旱冰的奶牛
    最短路【bzoj2464】: 中山市选[2009]小明的游戏
    linux /dev/mapper/centos-root 被占满
    Centos7中安装Mysql8并修改密码策略并远程连接
    Centos7中PHP编译安装mysqli扩展报错
    Linux中Composer 在安装依赖包与本地php版本不符问题
  • 原文地址:https://www.cnblogs.com/bigmouthz/p/2447016.html
Copyright © 2011-2022 走看看