|
1 | | -USE SUSDB; |
2 | | -GO |
3 | 1 | SET NOCOUNT ON; |
| 2 | +SET ANSI_NULLS ON; |
| 3 | +SET QUOTED_IDENTIFIER ON; |
| 4 | +SET ANSI_PADDING ON; |
| 5 | +SET ANSI_WARNINGS ON; |
| 6 | +SET CONCAT_NULL_YIELDS_NULL ON; |
| 7 | +SET ARITHABORT ON; |
| 8 | +SET NUMERIC_ROUNDABORT OFF; |
4 | 9 |
|
5 | | -DECLARE @schema sysname, @table sysname, @index sysname, @sql nvarchar(max); |
| 10 | +SET NOCOUNT ON; |
| 11 | + |
| 12 | +DECLARE @MinPages INT = 1000; |
| 13 | +DECLARE @ReorgPct FLOAT = 10; |
| 14 | +DECLARE @RebuildPct FLOAT = 30; |
| 15 | + |
| 16 | +DECLARE @schema SYSNAME, @table SYSNAME, @index SYSNAME, @sql NVARCHAR(MAX); |
6 | 17 |
|
7 | | -DECLARE cur CURSOR FAST_FORWARD FOR |
| 18 | +DECLARE c CURSOR LOCAL FAST_FORWARD FOR |
8 | 19 | SELECT |
9 | | - s.name AS SchemaName, |
10 | | - t.name AS TableName, |
11 | | - i.name AS IndexName, |
12 | | - ips.avg_fragmentation_in_percent AS Frag, |
13 | | - ips.page_count AS Pages |
14 | | -FROM sys.dm_db_index_physical_stats(DB_ID('SUSDB'), NULL, NULL, NULL, 'LIMITED') AS ips |
15 | | -JOIN sys.indexes AS i ON ips.object_id = i.object_id AND ips.index_id = i.index_id |
16 | | -JOIN sys.tables AS t ON t.object_id = i.object_id |
17 | | -JOIN sys.schemas AS s ON s.schema_id = t.schema_id |
18 | | -WHERE i.name IS NOT NULL |
19 | | - AND ips.page_count >= 100 -- avoid unnecessary work |
| 20 | + OBJECT_SCHEMA_NAME(ips.object_id) AS SchemaName, |
| 21 | + OBJECT_NAME(ips.object_id) AS TableName, |
| 22 | + i.name AS IndexName, |
| 23 | + ips.page_count, |
| 24 | + ips.avg_fragmentation_in_percent |
| 25 | +FROM sys.dm_db_index_physical_stats(DB_ID('SUSDB'), NULL, NULL, NULL, 'LIMITED') ips |
| 26 | +JOIN sys.indexes i |
| 27 | + ON ips.object_id = i.object_id AND ips.index_id = i.index_id |
| 28 | +WHERE ips.index_id > 0 |
20 | 29 | AND i.is_disabled = 0 |
21 | | - AND i.type_desc <> 'HEAP' -- heaps do not support REBUILD |
| 30 | + AND ips.page_count >= @MinPages |
| 31 | + AND ips.avg_fragmentation_in_percent >= @ReorgPct |
22 | 32 | ORDER BY ips.avg_fragmentation_in_percent DESC; |
23 | 33 |
|
24 | | -OPEN cur; |
25 | | -DECLARE @frag float, @pages int; |
| 34 | +OPEN c; |
| 35 | + |
| 36 | +DECLARE @page_count BIGINT, @frag FLOAT; |
| 37 | + |
| 38 | +FETCH NEXT FROM c INTO @schema, @table, @index, @page_count, @frag; |
26 | 39 |
|
27 | | -FETCH NEXT FROM cur INTO @schema, @table, @index, @frag, @pages; |
28 | 40 | WHILE @@FETCH_STATUS = 0 |
29 | 41 | BEGIN |
30 | | - IF @frag >= 30 |
31 | | - SET @sql = N'ALTER INDEX ['+@index+'] ON ['+@schema+'].['+@table+'] REBUILD WITH (SORT_IN_TEMPDB = OFF, MAXDOP = 1);'; |
32 | | - ELSE IF @frag >= 5 |
33 | | - SET @sql = N'ALTER INDEX ['+@index+'] ON ['+@schema+'].['+@table+'] REORGANIZE;'; |
| 42 | + IF (@frag >= @RebuildPct) |
| 43 | + SET @sql = N'ALTER INDEX [' + REPLACE(@index,']',']]') + N'] ON [' + REPLACE(@schema,']',']]') + N'].[' + REPLACE(@table,']',']]') + N'] REBUILD WITH (ONLINE = OFF);'; |
34 | 44 | ELSE |
35 | | - SET @sql = NULL; -- ignora |
| 45 | + SET @sql = N'ALTER INDEX [' + REPLACE(@index,']',']]') + N'] ON [' + REPLACE(@schema,']',']]') + N'].[' + REPLACE(@table,']',']]') + N'] REORGANIZE;'; |
36 | 46 |
|
37 | | - IF @sql IS NOT NULL |
38 | | - BEGIN |
39 | | - PRINT CONCAT('>> ', @schema, '.', @table, ' [', @index, '] Frag=', CONVERT(varchar(10), @frag), '% Pages=', @pages); |
40 | | - EXEC sp_executesql @sql; |
41 | | - END |
| 47 | + PRINT @sql; |
| 48 | + EXEC sp_executesql @sql; |
42 | 49 |
|
43 | | - FETCH NEXT FROM cur INTO @schema, @table, @index, @frag, @pages; |
| 50 | + FETCH NEXT FROM c INTO @schema, @table, @index, @page_count, @frag; |
44 | 51 | END |
45 | 52 |
|
46 | | -CLOSE cur; DEALLOCATE cur; |
47 | | -GO |
| 53 | +CLOSE c; |
| 54 | +DEALLOCATE c; |
| 55 | + |
| 56 | +EXEC sp_updatestats; |
0 commit comments