Skip to content

Commit 073e49a

Browse files
Update wsus-reindex-smart.sql
Signed-off-by: LUIZ HAMILTON ROBERTO DA SILVA <[email protected]>
1 parent f5b58a0 commit 073e49a

1 file changed

Lines changed: 41 additions & 32 deletions

File tree

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,56 @@
1-
USE SUSDB;
2-
GO
31
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;
49

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);
617

7-
DECLARE cur CURSOR FAST_FORWARD FOR
18+
DECLARE c CURSOR LOCAL FAST_FORWARD FOR
819
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
2029
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
2232
ORDER BY ips.avg_fragmentation_in_percent DESC;
2333

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;
2639

27-
FETCH NEXT FROM cur INTO @schema, @table, @index, @frag, @pages;
2840
WHILE @@FETCH_STATUS = 0
2941
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);';
3444
ELSE
35-
SET @sql = NULL; -- ignora
45+
SET @sql = N'ALTER INDEX [' + REPLACE(@index,']',']]') + N'] ON [' + REPLACE(@schema,']',']]') + N'].[' + REPLACE(@table,']',']]') + N'] REORGANIZE;';
3646

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;
4249

43-
FETCH NEXT FROM cur INTO @schema, @table, @index, @frag, @pages;
50+
FETCH NEXT FROM c INTO @schema, @table, @index, @page_count, @frag;
4451
END
4552

46-
CLOSE cur; DEALLOCATE cur;
47-
GO
53+
CLOSE c;
54+
DEALLOCATE c;
55+
56+
EXEC sp_updatestats;

0 commit comments

Comments
 (0)