Código para reindexar todas las tablas de una base de datos
-- DECLARE @TableName varchar(200) DECLARE TableCursor CURSOR FOR SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' OPEN TableCursor FETCH NEXT FROM TableCursor INTO @TableName WHILE @@FETCH_STATUS = 0 BEGIN PRINT 'Reindexando ' + @TableName DBCC DBREINDEX (@TableName) FETCH NEXT FROM TableCursor INTO @TableName END CLOSE TableCursor DEALLOCATE TableCursor --
ADDENDUM
-- DECLARE @TableName VARCHAR(255) DECLARE @sql NVARCHAR(500) DECLARE @fillfactor INT SET @fillfactor = 80 DECLARE TableCursor CURSOR FOR SELECT QUOTENAME(OBJECT_SCHEMA_NAME([object_id]))+'.' + QUOTENAME(name) AS TableName FROM sys.tables OPEN TableCursor FETCH NEXT FROM TableCursor INTO @TableName WHILE @@FETCH_STATUS = 0 BEGIN SET @sql = 'ALTER INDEX ALL ON ' + @TableName + ' REBUILD WITH (FILLFACTOR = ' + CONVERT(VARCHAR(3),@fillfactor) + ')' PRINT @sql EXEC (@sql) FETCH NEXT FROM TableCursor INTO @TableName END CLOSE TableCursor DEALLOCATE TableCursor GO --
Crear Store Procedure (SP) para:
1.- De fragmentar Indices
2.- Reconstruir Indices
--Exec xOptimiza 0
Create Proc xOptimiza
@mov int
AS
-- Declaración de variables necesarias para los procesos
DECLARE @ID int, @Name varChar(128), @Reindexa int, @Indice varchar(128), @DB VarChar(128)
/*
Los posibles valores que pude tomar la variable @Reindexa:
0 : Muestra la Fragmentacion de la información e indices
1 : Defragmenta los indices de las tablas
2 : Muestra la fragmentación de la informacion e indices,
y despues reconstrulle los indices de las tablas
cambiandoles el fill-factor a 80 para un mejor desempeño
3 : Revisa la integridad de la información, indices y la
imagen de la página
4 : Informa el espacio utilizado por las tablas
5 : Revisa la consistenacia de el almacenamiento de la informacion en el
disco duro
*/
-- Select db_name()
-- Asigana un valor correcto a esta variable para iniciar el proceso deseado
Select @Reindexa = @mov,
@DB = db_name()
If @Reindexa < 5
Begin
-- Se obtiene la lista de las tablas de usuario para despues poderlas analizar
Declare Tablas Cursor For
--Select id, rTrim(name) from sysobjects where xtype = 'U' Order by name
--Se mejoro metodo de enlistado de tablas al agregar su esquema correspondiente
Select id, rTrim(QUOTENAME(OBJECT_SCHEMA_NAME([id]))+'.' + QUOTENAME(name)) from sysobjects where xtype = 'U' Order by name
-- Abre el cursor "TABLAS" para recorrer tabla por tabla
Open Tablas
-- Obtiene el primer registro de las tablas
Fetch Next From Tablas
Into @ID, @Name
While @@Fetch_Status = 0
Begin
Print '******************************************************************* ' + @Name
IF @Reindexa = 0
-- Muestra la Fragmentacion de la información e indices
Dbcc Showcontig (@ID)
IF @Reindexa = 1
Begin
-- Defragmenta los indices de una tabla
If Exists (Select Name From sysindexes Where Id = @ID and IndId <> 0)
Begin
Declare Indices Cursor For
Select name From sysindexes Where id = @ID and indid <> 0 and rows <> 0
Open Indices
Fetch Next From Indices
Into @Indice
While @@Fetch_Status = 0
Begin
Print '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ' + @Indice
DBCC INDEXDEFRAG (@DB,@Name, @Indice)
-- Select @Indice, @Name
Fetch Next From Indices
Into @Indice
End
Close Indices
Deallocate Indices
End
Else
print '-- No tiene indices que defragmentar'
End
IF @Reindexa = 2
Begin
-- Muestra la fragmentación de la informacion e indices, y despues
-- reconstrulle los indices de la tabla cambiandoles el fill-factor a 60 para un mejor desempeño
If Exists (Select name From sysindexes Where id = @ID and indid <> 0)
Begin
Dbcc Showcontig (@ID)
Declare Indices Cursor For
Select name From sysindexes Where id = @ID and indid <> 0 and rows <> 0
Open Indices
Fetch Next From Indices
Into @Indice
While @@Fetch_Status = 0
Begin
Print '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ' + @Indice
DBCC DBREINDEX (@Name, @Indice, 80)
-- Select @Indice, @Name
Fetch Next From Indices
Into @Indice
End
Close Indices
Deallocate Indices
End
Else
print '-- No tiene indices que defragmentar'
End
IF @Reindexa = 3
-- Revisa la integridad de la información, indices y la imagen de la página
DBCC CheckTable (@Name)
IF @Reindexa = 4
-- Informa el espacio utilizado por las tablas
EXEC sp_spaceused @Name
Print ''
Print ''
-- Obtiene siguiente registro de tablas
Fetch Next From Tablas
Into @ID, @Name
End
-- Cierra y libera el cursor "TABLAS"
Close Tablas
Deallocate Tablas
End
IF @Reindexa = 5
-- Revisa la consistenacia de el almacenamiento de la informacion en el
-- disco duro
DBCC CHECKALLOC (@DB)
IF @Reindexa = 6
DBCC CHECKALLOC (@DB, REPAIR_REBUILD)
-- Reactiva la opcion para contar los registros
--Set NoCount OFF
Comentarios