日期:2014-05-16  浏览次数:20426 次

自动维护索引--脱机重建或组织索引
最近老是发现 以前写的 自动维护索引的 作业 报错。提示:

 

 不能对 索引 '某某' 执行联机操作,因为该索引包含数据类型为 text、ntext、image、varchar(max)、nvarchar(max)、varbinary(max)、xml 或大型 CLR 类型的列 'additional_parameters'。对于非聚集索引,该列可能是索引的包含列。对于聚集索引,它可能是该表的任何列。如果使用 DROP_EXISTING,则该列可能是新索引或旧索引的一部分。该操作必须离线执行。

于是 花了 一上午时间将以前的写法 改写了一下。

 

CREATE  procedure [pr_auto_indexdefrag_offline]
as

begin
set nocount on
 declare @Db_name nvarchar(256)
   ,@SchemaName nvarchar(256)
   ,@TableName Nvarchar(256)
   ,@IndexName Nvarchar(512)
   ,@PctFrag decimal
   ,@Defrag nvarchar(max)
   
 if exists(select 1 from sys.objects where object_id =object_id(N'#tmp')) Drop table #tmp;
 if exists(select 1 from sys.objects where object_id =object_id(N'#tmp_sub')) Drop table #tmp_sub;
 
create table #tmp_sub(database_id int,dbname nvarchar(32),tablename nvarchar(128),index_type_desc nvarchar(128))
create table #tmp(database_id int,dbname nvarchar(256),tablename nvarchar(256),indexname nvarchar(256),type_desc nvarchar(128),schemaname nvarchar(256),avgfragment decimal)

------找出  text、ntext、image、varchar(max)、nvarchar(max)、varbinary(max)、xml 或大型 CLR 类型的列

exec sp_MSforeachdb 'insert into #tmp_sub(database_id,dbname,tablename,index_type_desc)
select distinct c.database_id,''?'' dbname,b.name,''CLUSTERED''
 from 
   ?.sys.dm_db_index_physical_stats(DB_ID(''?''),NULL,NULL,NULL,''SAMPLED'') as a
 join ?.sys.tables as b on a.object_id=b.object_id 
 join sys.databases as c on a.database_id=c.database_id
 join ?.sys.all_columns d on d.object_id =a.object_id 
 join ?.sys.sysobjects e on d.object_id=e.id and e.xtype=''U'' 
 join ?.sys.types f on d.user_type_id=f.user_type_id 
where  b.type_desc=''USER_TABLE'' and b.is_ms_shipped=0 and (d.max_length =-1
 OR (f.name in (''image'',''text'',''ntext'',''xml'',''varbinary'',''binary'')))'
 
----找出 所有库中的索引

exec sp_MSforeachdb 'insert into #tmp(database_id,dbname,tablename,indexname,type_desc,schemaname,avgfragment)
select  distinct d.database_id,''?'' dbname,c.name,b.name,b.type_desc,e.name,a.avg_fragmentation_in_percent
from ?.sys.dm_db_index_physical_stats(DB_ID(''?''),NULL,NULL,NULL,''SAMPLED'') as a
    join ?.sys.indexes as b on a.object_id=b.object_id and a.index_id=b.index_id 
    join ?.sys.tables as c on a.object_id=c.object_id 
    join sys.databases as d on a.database_id=d.database_id
    join ?.sys.schemas as e on c.schema_id=e.schema_id
    join ?.sys.sysobjects f on c.object_id=f.id
    join ?.sys.all_columns g on f.id=g.object_id
    join ?.sys.types h on g.user_type_id=h.user_type_id
    where a.avg_fragmentation_in_percent >20
     and c.type=''U'' and f.xtype=''U'' 
     and c.is_ms_shipped=0  '
     
 declare frg_cur cursor for
  select dbname,tablename,indexname,
   schemaname,avgfragment 
    from #tmp 
    where  exists (select 1 from #tmp_sub b where database_id=b.database_id and tablename=b.tablename and type_desc=b.index_type_desc)
    
  open frg_cur
  fetch next from  frg_cur into @Db_name,@TableName,@IndexName,@SchemaName,@PctFrag
  while @@FETCH_STATUS=0
   begin
    if @PctFrag between 20.0 and 40.0
     begin
      set @Defrag=N' ALTER INDEX '+@IndexName+' ON'+@Db_name+'.'+@SchemaName+'.'+ @TableName +' REORGANIZE'--重新组织索引页不删除索引
      EXEC SP_EXECUTESQL @Defrag
     end
     else if @PctFrag>40.0
     begin
      SET @Defrag=N' ALTER INDEX '+@IndexName+' ON'+@Db_name+'.'+@SchemaName+'.'+ @TableName +' REBUILD WITH (ONLINE = OFF )'--脱机重建索引。
      EXEC SP_EXECUTESQL @Defrag
     end
     fetch next from  frg_cur into @Db_name,@TableName,@IndexName,@SchemaName,@PctFrag
   end
   close frg_cur
   deallocate frg_cur

end

set nocount off