• SQL Server 2012 批量重建索引


    关于索引的概念可以看看宋大牛的博客

    T-SQL查询高级—SQL Server索引中的碎片和填充因子

    整个数据库的索引很多,索引碎片多了,不可能一个个的去重建,都是重复性的工作,所以索性写了个存储过程,按范围需要重建索引。

      1 -- =============================================
      2 -- Author:    <Limy>
      3 -- Create date: <2015-12-31>
      4 -- Description:    <批量重建索引>
      5 -- EXEC pr_BatchRebuildIndex  'dbo','DatabaseLog','PK_DatabaseLog_DatabaseLogID',0
      6 -- =============================================
      7 alter proc pr_BatchRebuildIndex
      8 --@DataBase nvarchar(50)=null,--数据库名称,必输项
      9 @Scheme nvarchar(50)=null,--架构名称
     10 @TableName nvarchar(50)=null, --要重建索引的表名称,为空表示所有表
     11 @IndexName nvarchar(200)=null ,--要重建的索引名称
     12 @FragmentPercent decimal(10,5)=0 --碎片率为多少以上的药重建
     13 AS
     14 BEGIN
     15 
     16 DECLARE @Sql nvarchar(2000); 
     17 DECLARE @RebuildSql nvarchar(2000); 
     18 DECLARE @ERROR nvarchar(500);
     19 DECLARE @Tables table(TableName nvarchar(100), Indexid int,IndexName nvarchar(200), FragmentPercent decimal(10,5));
     20 
     21 ----判断数据库是否存在
     22 --IF DB_ID(@DataBase) is null
     23 --BEGIN
     24 --        RAISERROR('数据库不存在,请输入正确的数据库名称!',16,1);
     25 --        RETURN -1;
     26 --END 
     27 
     28  
     29 --判断架构是否存在
     30 IF isnull(@Scheme,'')<>'' and SCHEMA_ID(@Scheme) is null
     31 BEGIN
     32         RAISERROR('架构不存在,请输入正确的架构名称!',16,1);
     33         RETURN -1;
     34 END
     35 
     36  
     37 --表存不存在
     38 IF isnull(@TableName,'')<>''
     39 BEGIN
     40         --有架构
     41         IF isnull(@Scheme,'')<>'' and SCHEMA_ID(@Scheme) is null
     42         BEGIN
     43                 IF OBJECT_ID(@Scheme+'.'+@TableName) is null
     44                 BEGIN
     45                         SET @ERROR='表不存在,请输入正确的表名称!'
     46                         RAISERROR(@ERROR,16,2);
     47                         RETURN -1;
     48                 END
     49         END
     50         --没有架构
     51         IF not exists(select 1 from sys.tables as A  where A.name=@TableName   ) 
     52         BEGIN
     53                 SET @ERROR='表不存在,请输入正确的表名称!'
     54                 RAISERROR(@ERROR,16,5);
     55                 RETURN -1;
     56         END
     57 END
     58 
     59 --索引存不存在
     60 IF isnull(@IndexName,'')<>''
     61 BEGIN
     62         IF not exists(select 1 from sys.SysObjects where name=@IndexName and (xtype=N'UQ' OR xtype=N'PK') )
     63         BEGIN
     64                 RAISERROR('索引不存在,请输入正确的索引名称!',16,3);
     65                 RETURN -1;
     66         END
     67 END
     68 
     69 --如果表名为空表示所有表,如果索引为空表示所有索引
     70 --默认查询所有表,所有索引,所有百分比
     71  
     72 SET @Sql='SELECT D.name+''.''+object_name(a.object_id) [TableName] ,a.index_id ,B.name [IndexName] ,avg_fragmentation_in_percent   
     73         FROM sys.dm_db_index_physical_stats ( DB_ID() , NULL , NULL, NULL, NULL ) AS A
     74         JOIN sys.indexes AS B ON a.object_id = b.object_id AND a.index_id = b.index_id
     75         inner JOIN sys.tables AS C ON a.object_id=C.object_id
     76         inner JOIN sys.schemas AS D ON C.schema_id=D.schema_id
     77         WHERE   a.index_id > 0 '
     78 
     79 IF isnull(@Scheme,'') <>''
     80 BEGIN
     81         SET @Sql +=' and D.name= '''+@Scheme+''' ';
     82 END
     83 IF  isnull(@TableName,'')<>''
     84 BEGIN
     85         SET @Sql +=' and object_name(a.object_id)= '''+@TableName+''' ';
     86 END
     87 IF  isnull(@IndexName,'')<>''
     88 BEGIN
     89         SET @Sql +=' and B.name= '''+@IndexName+''' ';
     90 END
     91 
     92 IF  isnull(@FragmentPercent,0)>0
     93 BEGIN
     94         SET @Sql +=' and avg_fragmentation_in_percent>= '+convert(nvarchar(10),@FragmentPercent)+' ';
     95 END
     96  
     97 INSERT INTO @Tables
     98 EXEC (@Sql)
     99 select '重建前',* from @Tables
    100 
    101 DECLARE cur cursor for
    102         select TableName,  IndexName from @Tables 
    103 OPEN cur
    104 DECLARE @tbName NVARCHAR(100),@IXName NVARCHAR(200)
    105 FETCH NEXT FROM cur INTO @tbName,@IXName
    106 WHILE(@@fetch_status=0)
    107 BEGIN
    108         SET @RebuildSql='alter index ['+@IXName+'] on '+@tbName+' rebuild' --要加上[],否则索引里有空格会报错
    109         EXEC (@RebuildSql)
    110         FETCH NEXT FROM cur INTO @tbName,@IXName
    111 END
    112 CLOSE cur
    113 DEALLOCATE cur
    114 
    115 --重建后查询
    116 delete @Tables
    117 INSERT INTO @Tables
    118 EXEC (@Sql)
    119 select '重建后',* from @Tables
    120 
    121 Print N'执行完毕!';
    122 return 0;
    123 END

    本人才疏学浅,如有不合理之处,欢迎拍砖。

  • 相关阅读:
    [oracle] linux Oracle 安装配置
    [dns] linux dns 安装配置
    [apache] linux Apache 编译安装
    [yum] linux yum 配置本地和ftp源
    [ftp] linux ftp 安装配置
    [ssh 无密码访问]linux ssh公匙密匙无密码访问
    [php ] linux php 搭建
    [mysql ] linux mysal 修改字符集
    [ mysql ] linux mysql 忘记root密码重置
    国安是冠军
  • 原文地址:https://www.cnblogs.com/gates/p/5097918.html
Copyright © 2020-2023  润新知