Skip to content

Commit fd7e3bf

Browse files
Update wsus-verify-fragmentation.sql
Signed-off-by: LUIZ HAMILTON ROBERTO DA SILVA <[email protected]>
1 parent e18318d commit fd7e3bf

1 file changed

Lines changed: 41 additions & 27 deletions

File tree

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,44 @@
1-
USE SUSDB;
1+
USE [SUSDB];
22
GO
33

4-
SELECT
5-
dbschemas.[name] AS SchemaName,
6-
dbtables.[name] AS TableName,
7-
dbindexes.[name] AS IndexName,
8-
dbindexes.type_desc AS IndexType,
9-
indexstats.page_count AS PageCount,
10-
indexstats.avg_fragmentation_in_percent AS FragmentationPercent,
11-
CASE
12-
WHEN indexstats.avg_fragmentation_in_percent >= 30 THEN 'High'
13-
WHEN indexstats.avg_fragmentation_in_percent >= 10 THEN 'Medium'
4+
DECLARE @MinPages int = 100;
5+
DECLARE @RebuildPct float = 30.0;
6+
DECLARE @ReorgPct float = 5.0;
7+
8+
SELECT
9+
s.[name] AS SchemaName,
10+
t.[name] AS TableName,
11+
i.[name] AS IndexName,
12+
i.type_desc AS IndexType,
13+
ips.partition_number AS PartitionNumber,
14+
ips.page_count AS PageCount,
15+
ROUND(CAST(ips.avg_fragmentation_in_percent AS decimal(9,4)), 2) AS FragmentationPercent,
16+
CASE
17+
WHEN ips.avg_fragmentation_in_percent >= @RebuildPct THEN 'High'
18+
WHEN ips.avg_fragmentation_in_percent >= @ReorgPct THEN 'Medium'
1419
ELSE 'Low'
15-
END AS FragmentationLevel
16-
FROM
17-
sys.dm_db_index_physical_stats(DB_ID('SUSDB'), NULL, NULL, NULL, 'LIMITED') AS indexstats
18-
INNER JOIN
19-
sys.indexes dbindexes ON indexstats.[object_id] = dbindexes.[object_id]
20-
AND indexstats.index_id = dbindexes.index_id
21-
INNER JOIN
22-
sys.tables dbtables ON dbindexes.[object_id] = dbtables.[object_id]
23-
INNER JOIN
24-
sys.schemas dbschemas ON dbtables.[schema_id] = dbschemas.[schema_id]
25-
WHERE
26-
dbindexes.[name] IS NOT NULL
27-
AND dbindexes.is_disabled = 0
28-
AND indexstats.page_count > 100
29-
ORDER BY
30-
indexstats.avg_fragmentation_in_percent DESC;
20+
END AS FragmentationLevel,
21+
CASE
22+
WHEN ips.page_count < @MinPages THEN 'NONE (small index)'
23+
WHEN ips.avg_fragmentation_in_percent >= @RebuildPct THEN 'REBUILD'
24+
WHEN ips.avg_fragmentation_in_percent >= @ReorgPct THEN 'REORGANIZE'
25+
ELSE 'NONE'
26+
END AS MaintenanceRecommendation
27+
FROM sys.dm_db_index_physical_stats(DB_ID('SUSDB'), NULL, NULL, NULL, 'LIMITED') AS ips
28+
JOIN sys.indexes AS i
29+
ON ips.[object_id] = i.[object_id]
30+
AND ips.index_id = i.index_id
31+
JOIN sys.tables AS t
32+
ON i.[object_id] = t.[object_id]
33+
JOIN sys.schemas AS s
34+
ON t.[schema_id] = s.[schema_id]
35+
WHERE
36+
i.[name] IS NOT NULL
37+
AND i.is_disabled = 0
38+
AND i.is_hypothetical = 0
39+
AND i.index_id > 0
40+
AND i.type IN (1,2)
41+
AND ips.page_count >= @MinPages
42+
ORDER BY
43+
FragmentationPercent DESC,
44+
PageCount DESC;

0 commit comments

Comments
 (0)