0% found this document useful (0 votes)
9 views3 pages

Rebuild Por Base

Uploaded by

pedrohalmeida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Rebuild Por Base

Uploaded by

pedrohalmeida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

USE [DBA]

GO
/****** Object: StoredProcedure [dbo].[USP_DBA_REBUILD_POR_BASE] Script Date:
08/14/2013 10:01:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

create procedure [dbo].[USP_DBA_REBUILD_POR_BASE] ( @database varchar(255) )


AS

set nocount on

declare @dbid int


set @dbid = db_id(@database)

if object_id('tempdb..##coleta_tabelas') is not null


drop table ##coleta_tabelas

if object_id('tempdb..##index_stats') is not null


drop table ##index_stats

create table ##index_stats


(
[base] sysname ,
[schema] sysname ,
[tabela] sysname ,
[indice] sysname ,
[fragmentacao] float ,
[paginas] int
)

declare
@schema sysname ,
@tableid int ,
@tabela sysname ,
@indexid int ,
@indice sysname ,
@cmd varchar(max)

set @cmd = 'select distinct '


+'tbl.object_id objeto,tbl.name tabela,sch.name [schema] '
+'into ##coleta_tabelas '
+'from ['+@database+'].sys.tables tbl with (nolock) '
+'inner join ['+@database+'].sys.indexes idx with (nolock) on
tbl.object_id = idx.object_id '
+'inner join ['+@database+'].sys.schemas sch with (nolock) on
tbl.schema_id = sch.schema_id '
+'where idx.type <> 0'
exec (@cmd)

declare tabelas cursor for select [objeto],tabela,[schema] from


##coleta_tabelas
open tabelas
fetch next from tabelas into @tableid,@tabela,@schema
while @@fetch_status = 0
Begin
if object_id('tempdb..##coleta_indices') is not null
drop table ##coleta_indices

set @cmd = 'select index_id objeto,name indice into ##coleta_indices


from ['
+ @database + '].sys.indexes (nolock) where object_id = '
+ convert(varchar,@tableid) + ' and type <> 0'
exec (@cmd)

declare indices cursor for select objeto,indice from


##coleta_indices

open indices
fetch next from indices into @indexid,@indice
while @@fetch_status = 0
begin
insert into ##index_stats
select @database ,
@schema ,
@tabela ,
@indice ,
avg_fragmentation_in_percent ,
page_count
from sys.dm_db_index_physical_stats(@dbid,
@tableid,
@indexid, null,
'limited')
where avg_fragmentation_in_percent >= 20
and page_count > 10

fetch next from indices into @indexid,@indice


end
close indices
deallocate indices

fetch next from tabelas into @tableid,@tabela,@schema


end
close tabelas
deallocate tabelas

declare @frag float

declare idxrebuild cursor for select indice, tabela, base,fragmentacao from


##index_stats
open idxrebuild
fetch next from idxrebuild into @indice, @tabela, @database,@frag
while @@fetch_status = 0
begin

if ( @frag >= 20 )
set @cmd = 'alter index ['+@indice+'] on ['+@database+'].
['+@schema+'].['+@tabela+'] Rebuild'
else
set @cmd = 'alter index ['+@indice+'] on ['+@database+'].
['+@schema+'].['+@tabela+'] Reorganize'

begin try
print CONVERT(VARCHAR, GETDATE(), 103) + ' '
+ convert(varchar, getdate(), 108)
+ ' rebuild do indice: [' + @indice
+ '] da tabela: [' + @tabela + ']'
exec (@cmd)
--print @cmd
end try
begin catch
print '--------- O rebuild do indice: ' + @indice
+ ' apresentou o erro: '
print error_message()
end catch

fetch next from idxrebuild into @indice, @tabela, @database,@frag


end
close idxrebuild
deallocate idxrebuild

You might also like