2/08/2018 09:51:00 a. m.
Share:
Blog Informático sobre Análisis y Desarrollo de Software.
-- -- ALTER LOGIN sa ENABLE ; GO ALTER LOGIN sa WITH PASSWORD = '' ; GO
-- -- sp_depends MIGRACION_INCREMENTAL_TABLAS
--
--
SELECT referencing_schema_name, referencing_entity_name, referencing_id, referencing_class_desc, is_caller_dependent
FROM sys.dm_sql_referencing_entities ('dbo.MIGRACION_INCREMENTAL_TABLAS', 'OBJECT');
GO
-- -- select name, c.id, c.text, o.xtype, o.crdate from syscomments c join sysobjects o on c.id = o.id where c.TEXT like '%MIGRACION_INCREMENTAL_TABLAS%'
-- -- create procedure proc_prueba as begin DECLARE @VARIABLE nvarchar(2000) set @VARIABLE = 'select * from MIGRACION_INCREMENTAL_TABLAS' end
-- -- use AdventureWorks2012 GO; SELECT s.name, tab.name as tablename, ind.name as indexname FROM sys.indexes ind INNER JOIN sys.tables tab ON ind.object_id = tab.object_id INNER JOIN sys.schemas s on tab.schema_id = s.schema_id where tab.name = 'Department' and s.name = 'HumanResources' ORDER BY s.name,tab.name
--
--
SELECT a.index_id, name as nombre_indice, avg_fragmentation_in_percent as porcentaje_fragmentacion
FROM sys.dm_db_index_physical_stats (DB_ID(N'BD_TRIBUTACION'),
OBJECT_ID(N'dbo.Cta_Corriente'), NULL, NULL, NULL) AS a
JOIN sys.indexes AS b
ON a.object_id = b.object_id AND a.index_id = b.index_id;
GO
-- o en AdventureWorks2012
SELECT a.index_id, name, avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats (DB_ID(N'AdventureWorks2012'),
OBJECT_ID(N'HumanResources.Department'), NULL, NULL, NULL) AS a
JOIN sys.indexes AS b
ON a.object_id = b.object_id AND a.index_id = b.index_id;
GO
-- -- ALTER INDEX [NombreDelIndice] ON [schema].[TablaDondeEstaElIndice] REORGANIZE
-- -- ALTER INDEX [NombreDelIndice] ON [schema].[TablaDondeEstaElIndice] REBUILD
-- -- USE BD_TRIBUTACION; GO -- Reorganizar todos los índices de la tabla dbo.Cta_corriente. ALTER INDEX ALL ON dbo.Cta_corriente REBUILD; GO
--
--
SELECT a.index_id, name as nombre_indice, OBJECT_name(a.object_id) as nombre_tabla,
avg_fragmentation_in_percent as porcentaje_fragmentacion,
case when avg_fragmentation_in_percent <= 30 then 'RO' -- REORGANIZAR
else 'RI' -- REINDEXAR
END as 'ACCION'
into #reorganizar_reindexar
FROM sys.dm_db_index_physical_stats (DB_ID('N'BD_TRIBUTACION'), OBJECT_ID(N'dbo.cta_corriente'), NULL, NULL, NULL) AS a
JOIN sys.indexes AS b ON a.object_id = b.object_id AND a.index_id = b.index_id
where avg_fragmentation_in_percent >= 5
--select * from #reorganizar_reindexar
DECLARE @i int = 1, @ix int
declare @nombre_indice varchar(200), @nombre_tabla varchar(200), @accion varchar(10)
declare @sql nvarchar(200)=''
select top 1 @i = index_id, @nombre_tabla = nombre_tabla, @nombre_indice = nombre_indice, @accion = accion
from #reorganizar_reindexar
order by index_id
while @i is not null
begin
--RAISERROR(@i, 0, 1) WITH NOWAIT
Print @i
RAISERROR(N'', 0, 1) WITH NOWAIT
if @accion = 'RO'
begin
set @sql = 'ALTER INDEX ' + @nombre_indice + ' ON dbo.' + @nombre_tabla + ' REORGANIZE' --WITH (ONLINE = ON);
end
else
begin
set @sql = 'ALTER INDEX '+ @nombre_indice + ' ON dbo.' + @nombre_tabla + ' REBUILD' --WITH (ONLINE = ON);;
end
--RAISERROR(@sql, 0, 1) WITH NOWAIT
Print @sql
RAISERROR(N'', 0, 1) WITH NOWAIT
execute sp_executesql @sql
set @ix = null
select top 1 @ix = index_id, @nombre_tabla = nombre_tabla, @nombre_indice = nombre_indice, @accion = accion
from #reorganizar_reindexar
where index_id > @i
order by index_id
set @i = @ix
end
drop table #reorganizar_reindexar
0 comentarios: