2012-01-25 4 views
0

인덱스의 온라인 재구성을 허용하지 않는 유형의 데이터베이스에 몇 개의 인덱스가 있기 때문에 유지 관리 작업에 실패했습니다. 내 산업에서 오프라인은 옵션이 아니므로 특정 인덱스에서 인덱스를 다시 작성하는 자체 T-SQL 태스크를 만들어야합니다. 데이터베이스는 테이블과 인덱스가 많아서 커지므로 모든 인덱스와 해당 데이터 유형을 시스템에 쿼리하는 방법이 있습니까?SQL Server 2005에서 데이터 형식과 함께 데이터베이스의 모든 인덱스를 검색하는 방법은 무엇입니까?

select o.name as [object_name], 
    ix.name as [index_name], 
    coalesce(c.name,cc.name) as [column_name], 
    t.name as [type_name], 
    coalesce(c.max_length, cc.max_length) as max_length 
from sys.indexes ix 
join sys.objects o 
    on ix.object_id = o.object_id 
left join sys.index_columns ic 
    on ix.object_id = ic.object_id 
    and ix.index_id = ix.index_id 
    and ix.index_id > 1 
left join sys.columns c 
    on ic.object_id = c.object_id 
    and ic.column_id = c.column_id 
left join sys.columns cc 
    on ix.object_id = cc.object_id 
    and ix.index_id in (1,0) 
join sys.types t 
    on t.system_type_id = coalesce(c.system_type_id,cc.system_type_id) 
where o.type = 'U' 
order by object_name, index_name, column_name; 

그런 다음에 대한 안전하지 않은 사람을 식별 할 수

+0

확신 http://dba.stackexchange.com/questions/11389/is-staggering은 "HasBlobs"플래그가 -reindexing-jobs-a-good-strategy-mssql/11390 # comment16991_11390)이이를 고려합니다. –

답변

0

이렇게하면 힙과 같은 '기본 테이블'및 클러스터 된 인덱스의 열뿐만 아니라 모든 INCLUDE 열을 포함하여 모든 인덱스에 대한 모든 열을 표시합니다 온라인 인덱스 재 작성 (BLOB/XML/CLR 유형). SQL Server 2012에서 LOB 열을 사용하는 온라인 인덱스 빌드에 대한 제한이 해제되었습니다 (Online Index Operations for indexes containing LOB columns 참조).

+0

이것은 정확히 내가 찾고있는 것입니다. 고맙습니다! – stringpoet

0

우리는 최근 그러한 절차를 만들었습니다. 그것은이 목적 ([이 솔루션 모두 내가 여기에 언급]


ALTER PROCEDURE sp_rebuild_local_idexes 
@RebuildClustered bit = 0 
AS 
BEGIN 
    DECLARE @objectid int 
    DECLARE @indexid int 
    DECLARE @schemaname nvarchar(130) 
    DECLARE @objectname nvarchar(130) 
    DECLARE @indexname nvarchar(130) 
    DECLARE @partitions bigint 
    DECLARE @frag float 
    DECLARE @command nvarchar(4000) 
    DECLARE @HasBlobs bit 
    DECLARE @index_type_desc nvarchar(255) 

    -- Conditionally select tables and indexes from the sys.dm_db_index_physical_stats function 
    -- and convert object and index IDs to names. 

    SELECT 
    --object_name(object_id), 
    object_id AS objectid, 
    index_id AS indexid, 
    avg_fragmentation_in_percent AS frag, 
    CASE WHEN (
     SELECT st.object_id 
      from sys.tables st 
      inner join sys.columns sc 
       on st.object_id=sc.object_id 
      inner join sys.types styp 
       on sc.system_type_id=styp.system_type_id and sc.max_length=styp.max_length 
      inner join sys.schemas ss 
       on st.schema_id=ss.schema_id 
      where styp.schema_id=4 
        and styp.name<>'sysname' 
        and styp.name IN ('xml','nvarchar','varchar','image','text','ntext') 
        AND st.object_id = a.object_id 
      group by st.object_id 
    ) IS NULL THEN 0 ELSE 1 END AS HasBlobs, 
    a.index_type_desc 
    INTO #work_to_do 
    FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, 'LIMITED') AS a 
    WHERE avg_fragmentation_in_percent >= 5.0 AND index_id > 0 
       AND a.index_type_desc IN ('CLUSTERED INDEX','NONCLUSTERED INDEX')   
    ORDER BY a.index_type_desc 

    -- Declare the cursor for the list of partitions to be processed. 
    DECLARE partitions CURSOR FOR SELECT objectid, 
      indexid, 
      frag, 
      HasBlobs, 
      index_type_desc from #work_to_do 
select * from #work_to_do 
    -- Open the cursor. 
    OPEN partitions 

    -- Loop through the partitions. 
    WHILE (1=1) 
    BEGIN 
     FETCH NEXT FROM partitions 
     INTO @objectid, @indexid, @frag,@HasBlobs,@index_type_desc 

     IF @@FETCH_STATUS < 0 BREAK 

     IF @RebuildClustered = 1 AND @index_type_desc != 'CLUSTERED INDEX' 
      CONTINUE; 

     SELECT @objectname = QUOTENAME(o.name), @schemaname = QUOTENAME(s.name) 
     FROM sys.objects AS o 
     JOIN sys.schemas as s ON s.schema_id = o.schema_id 
     WHERE o.object_id = @objectid 

     SELECT @indexname = QUOTENAME(name) 
     FROM sys.indexes 
     WHERE object_id = @objectid AND index_id = @indexid and type!=3 

     -- 30 is an arbitrary decision point at which to switch between reorganizing and rebuilding. 
     IF @frag < 30.0 
      SET @command = N'ALTER INDEX ' + @indexname + N' ON ' + @schemaname + N'.' + @objectname + N' REORGANIZE' 

     IF @frag >= 30.0 
     BEGIN 
      print @indexname+ @[email protected] 
      SET @command = N'ALTER INDEX ' + @indexname + 
          N' ON ' + 
          @schemaname + 
          N'.' + @objectname + 
          N' REBUILD' + 
          CASE WHEN @HasBlobs = 0 THEN ' WITH(ONLINE=ON)' ELSE '' END 
      print @command 
     END 

     EXEC (@command) 
     PRINT N'Executed: ' + @command 


    END 

    -- Close and deallocate the cursor. 
    CLOSE partitions 
    DEALLOCATE partitions 
END 

+0

감사합니다. 유용합니다. – stringpoet