Скрипт перестраивает только необходимые индексы.
-- фрагментированные индексы
use base1
Select
IDENTITY(int, 1,1) as idstr,
'base1' as dbn
,ob.name as tbl
,i.name as nameindex
,f.*
into tempdb.dbo.ifrag
from sys.dm_db_index_physical_stats(DB_ID('base1'), OBJECT_ID(NULL), NULL, NULL, NULL) f
left join sys.tables ob on ob.object_id = f.object_id
JOIN sys.indexes AS i ON f.object_id = i.object_id AND f.index_id = i.index_id
where f.avg_fragmentation_in_percent > 0 and f.page_count > 100
order by f.avg_fragmentation_in_percent desc
declare @i int = 0
declare @cou int
declare @dinsql nvarchar(1000)
set @cou = (select COUNT(*) from tempdb.dbo.ifrag)
while @i <= @cou
begin
set @i = @i + 1
set @dinsql = (select ' ALTER INDEX ' + t.nameindex + ' ON ' + t.tbl + ' REBUILD ; ' from tempdb.dbo.ifrag t where t.idstr = @i)
execute(@dinsql)
end
drop table tempdb.dbo.ifrag
Комментариев нет:
Отправить комментарий