Реорганізація індексу таблиці
ALTER INDEX ALL ON HumanResources.Employee
REORGANIZE;
Реорганізація окремого індексу
ALTER INDEX IX_Employee_OrganizationalLevel_OrganizationalNode ON HumanResources.Employee REORGANIZE;
Перебудова всіх індексів таблиці
ALTER INDEX ALL ON Production.Product REBUILD WITH (FILLFACTOR = 80, SORT_IN_TEMPDB = ON, STATISTICS_NORECOMPUTE = ON);
Перебудова окремого індексу
ALTER INDEX PK_Employee_BusinessEntityID ON HumanResources.Employee REBUILD;
Реорганізація окремого індексу
ALTER INDEX IX_Employee_OrganizationalLevel_OrganizationalNode ON HumanResources.Employee REORGANIZE;
Перебудова всіх індексів таблиці
ALTER INDEX ALL ON Production.Product REBUILD WITH (FILLFACTOR = 80, SORT_IN_TEMPDB = ON, STATISTICS_NORECOMPUTE = ON);
Перебудова окремого індексу
ALTER INDEX PK_Employee_BusinessEntityID ON HumanResources.Employee REBUILD;
Перебудова всіх індексів послідовно з повідомленнями
DECLARE @indexName sysname; DECLARE @tableName sysname = 'MarketingToolRecords'; DECLARE @objectId int = OBJECT_ID(@tableName); DECLARE @sql nvarchar(max); DECLARE @fragmentation float; DECLARE @fragmentationText nvarchar(20); DECLARE @now nvarchar(20); DECLARE @message nvarchar(400); DECLARE index_cursor CURSOR FOR SELECT name FROM sys.indexes WHERE object_id = @objectId AND name IS NOT NULL AND is_primary_key = 0; -- виключаємо PK OPEN index_cursor FETCH NEXT FROM index_cursor INTO @indexName WHILE @@FETCH_STATUS = 0 BEGIN -- Отримати фрагментацію SELECT TOP 1 @fragmentation = avg_fragmentation_in_percent FROM sys.dm_db_index_physical_stats( DB_ID(), @objectId, (SELECT index_id FROM sys.indexes WHERE name = @indexName AND object_id = @objectId), NULL, 'LIMITED' ); -- Форматуємо текст SET @fragmentationText = FORMAT(@fragmentation, 'N2'); SET @now = CONVERT(varchar, GETDATE(), 120); SET @message = 'Index: ' + @indexName + ' | Fragmentation: ' + @fragmentationText + '%% at ' + @now; RAISERROR(@message, 0, 1) WITH NOWAIT; -- REORGANIZE SET @sql = 'ALTER INDEX [' + @indexName + '] ON ' + @tableName + ' REORGANIZE;'; EXEC sp_executesql @sql; FETCH NEXT FROM index_cursor INTO @indexName END CLOSE index_cursor DEALLOCATE index_cursor;
DECLARE @indexName sysname; DECLARE @tableName sysname = 'MarketingToolRecords'; DECLARE @objectId int = OBJECT_ID(@tableName); DECLARE @sql nvarchar(max); DECLARE @fragmentation float; DECLARE @fragmentationText nvarchar(20); DECLARE @now nvarchar(20); DECLARE @message nvarchar(400); DECLARE index_cursor CURSOR FOR SELECT name FROM sys.indexes WHERE object_id = @objectId AND name IS NOT NULL AND is_primary_key = 0; -- виключаємо PK OPEN index_cursor FETCH NEXT FROM index_cursor INTO @indexName WHILE @@FETCH_STATUS = 0 BEGIN -- Отримати фрагментацію SELECT TOP 1 @fragmentation = avg_fragmentation_in_percent FROM sys.dm_db_index_physical_stats( DB_ID(), @objectId, (SELECT index_id FROM sys.indexes WHERE name = @indexName AND object_id = @objectId), NULL, 'LIMITED' ); -- Форматуємо текст SET @fragmentationText = FORMAT(@fragmentation, 'N2'); SET @now = CONVERT(varchar, GETDATE(), 120); SET @message = 'Index: ' + @indexName + ' | Fragmentation: ' + @fragmentationText + '%% at ' + @now; RAISERROR(@message, 0, 1) WITH NOWAIT; -- REORGANIZE SET @sql = 'ALTER INDEX [' + @indexName + '] ON ' + @tableName + ' REORGANIZE;'; EXEC sp_executesql @sql; FETCH NEXT FROM index_cursor INTO @indexName END CLOSE index_cursor DEALLOCATE index_cursor;
Andriy Kravchenko
Admin, Writer, File Uploader
20.10.2024 16:03:51