Column | Type of | Comment |
---|---|---|
proc_id | int | The sequence number of the procedure for identification |
start_time | datetime | Starting the ALTER INDEX query |
end_time | datetime | Completion of the ALTER INDEX query |
database_id | smallint | Database ID |
object_id | Int | Table ID |
table_name | varchar (50) | Table name |
index_id | Int | Index ID |
index_name | varchar (50) | Index name |
avg_frag_percent_before | float | The percentage of index fragmentation before running ALTER INDEX |
fragment_count_before | bigint | The number of fragments before defragmentation |
pages_count_before | bigint | Number of index pages before defragmentation |
fill_factor | tinyint | Index page fill level |
partition_num | int | Section number |
avg_frag_percent_after | float | The percentage of index fragmentation after ALTER INDEX |
fragment_count_after | bigint | The number of fragments after defragmentation |
pages_count_after | bigint | Number of index pages after defragmentation |
action | varchar (10) | Action performed |
DECLARE @currentProcID INT –- –- , SELECT @currentProcID = ISNULL(MAX(proc_id), 0) + 1 FROM dba_tasks.dbo.index_defrag_statistic -- INSERT INTO dba_tasks.dbo.index_defrag_statistic ( proc_id, database_id, [object_id], table_name, index_id, index_name, avg_frag_percent_before, fragment_count_before, pages_count_before, fill_factor, partition_num) SELECT @currentProcID, dm.database_id, dm.[object_id], tbl.name, dm.index_id, idx.name, dm.avg_fragmentation_in_percent, dm.fragment_count, dm.page_count, idx.fill_factor, dm.partition_number FROM sys.dm_db_index_physical_stats(DB_ID(), null, null, null, null) dm INNER JOIN sys.tables tbl ON dm.object_id = tbl.object_id INNER JOIN sys.indexes idx ON dm.object_id = idx.object_id AND dm.index_id = idx.index_id WHERE page_count > 8 AND avg_fragmentation_in_percent > 10 AND dm.index_id > 0
-- DECLARE @partitioncount INT -- DECLARE @action VARCHAR(10) --, DECLARE @start_time DATETIME -- ALTER INDEX DECLARE @end_time DATETIME -- ALTER INDEX -- DECLARE @object_id INT DECLARE @index_id INT DECLARE @tableName VARCHAR(250) DECLARE @indexName VARCHAR(250) DECLARE @defrag FLOAT DECLARE @partition_num INT DECLARE @fill_factor INT -- , , MAX, , , . DECLARE @sql NVARCHAR(MAX) -- DECLARE defragCur CURSOR FOR SELECT [object_id], index_id, table_name, index_name, avg_frag_percent_before, fill_factor, partition_num FROM dba_tasks.dbo.index_defrag_statistic WHERE proc_id = @currentProcID ORDER BY [object_id], index_id DESC -- OPEN defragCur FETCH NEXT FROM defragCur INTO @object_id, @index_id, @tableName, @indexName, @defrag, @fill_factor, @partition_num WHILE @@FETCH_STATUS=0 BEGIN SET @sql = N'ALTER INDEX ' + @indexName + ' ON ' + @tableName SELECT @partitioncount = count (*) FROM sys.partitions WHERE object_id = @object_id AND index_id = @index_id; -- , , , , IF (@fill_factor != 80) BEGIN @sql = @sql + N' REBUILD WITH (FILLFACTOR = 80)' SET @action = 'rebuild80' END ELSE BEGIN -- , MS IF (@defrag > 30) -- 30%, REBUILD BEGIN SET @sql = @sql + N' REBUILD' SET @action = 'rebuild' END ELSE -- REORGINIZE BEGIN SET @sql = @sql + N' REORGANIZE' SET @action = 'reorginize' END END -- IF @partitioncount > 1 SET @sql = @sql + N' PARTITION=' + CAST(@partition_num AS nvarchar(5)) print @sql -- -- SET @start_time = GETDATE() EXEC sp_executesql @sql -- SET @end_time = GETDATE() -- UPDATE dba_tasks.dbo.index_defrag_statistic SET start_time = @start_time, end_time = @end_time, [action] = @action WHERE proc_id = @currentProcID AND [object_id] = @object_id AND index_id = @index_id FETCH NEXT FROM defragCur INTO @object_id, @index_id, @tableName, @indexName, @defrag, @fill_factor, @partition_num END CLOSE defragCur DEALLOCATE defragCur
UPDATE dba SET dba.avg_frag_percent_after = dm.avg_fragmentation_in_percent, dba.fragment_count_after = dm.fragment_count, dba.pages_count_after = dm.page_count FROM sys.dm_db_index_physical_stats(DB_ID(), null, null, null, null) dm INNER JOIN dba_tasks.dbo.index_defrag_statistic dba ON dm.[object_id] = dba.[object_id] AND dm.index_id = dba.index_id WHERE dba.proc_id = @currentProcID AND dm.index_id > 0
Source: https://habr.com/ru/post/155933/
All Articles