This repository was archived by the owner on Jan 7, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathManagement.GatherIndexFragmentationLevelsps.sql
More file actions
125 lines (111 loc) · 3.9 KB
/
Management.GatherIndexFragmentationLevelsps.sql
File metadata and controls
125 lines (111 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
IF EXISTS (SELECT *
FROM tempdb.dbo.sysobjects
WHERE name = '##IndexFragmentationLevels')
drop TABLE ##IndexFragmentationLevels
CREATE TABLE ##IndexFragmentationLevels
(
ServerName NVARCHAR(128),
DBName NVARCHAR(128),
PartitionNumber SMALLINT,
SchemaName NVARCHAR(128),
TableName NVARCHAR(128),
IndexName NVARCHAR(128),
Fragmentation FLOAT,
PageTotalCount INT,
RangeScanCount BIGINT,
RecordedDateTime DATETIME
);
declare
@ScanType VARCHAR(10),
@MinimumPageCount INT,
@NumberOfThreads TINYINT
set @ScanType = 'LIMITED'
set @MinimumPageCount = 8
set @NumberOfThreads = 4
/************************************************************************************************
@ScanType sys.dm_db_index_physical_stats takes a parameter to determine how
aggresively to scan the index and determine fragmentation levels.
LIMITED - only scans parent level of the b-tree quickest and least
impacting
SAMPLED - takes a one percent sample of all data pages.
DETAILED - scans all data pages. This can kill your disk system
with heavy IO loads. use with much care!
@minPageCount default 8 generally, trying to defrag an index that is smaller than
an extent is pointless
************************************************************************************************/
DECLARE
@tablename VARCHAR(4000) = NULL,
@cmd VARCHAR(8000),
@servername VARCHAR(256),
@dbname VARCHAR(256),
@schemaname NVARCHAR(128),
@recordeddatetime DATETIME
CREATE TABLE #dbnames
(
name NVARCHAR(128)
)
SET @recordeddatetime = GETDATE()
SET @servername = CAST(Serverproperty('servername') AS VARCHAR(256))
insert into #dbnames select name
FROM MASTER.dbo.sysdatabases
WHERE name NOT IN ( 'master', 'msdb', 'model', 'pubs',
'northwind', 'tempdb' );
DECLARE db CURSOR FAST_FORWARD FOR
SELECT name
FROM #dbnames
OPEN db
FETCH NEXT FROM db INTO @dbname
WHILE ( @@FETCH_STATUS <> -1 )
BEGIN
IF ( @@FETCH_STATUS <> -2 )
BEGIN
IF (SELECT CONVERT(SYSNAME, Databasepropertyex(@dbname, 'status'))
)
=
'ONLINE'
BEGIN
set @cmd = '
INSERT INTO ##IndexFragmentationLevels
SELECT
'''+@servername+''' AS ''ServerName''
,'''+@dbname+''' AS ''DatabaseName''
, ps.partition_number AS ''PartitionNumber''
, Object_schema_name(ps.object_id,DB_ID('''+@dbname+'''))
, OBJECT_NAME(ps.object_id,db_id('''+@dbname+'''))
, si.name
, SUM(ps.avg_fragmentation_in_percent) AS ''Fragmentation''
, SUM(ps.page_count) AS ''PageTotalCount''
, os.range_scan_count
, '''+convert(varchar,@recordeddatetime, 121)+'''
FROM sys.dm_db_index_physical_stats(DB_ID('''+@dbname+'''),NULL, NULL , NULL, '''+@ScanType+''') AS ps
INNER JOIN sys.dm_db_index_operational_stats(DB_ID('''+@dbname+'''), NULL, NULL , NULL) AS os
ON ps.database_id = os.database_id
AND ps.[object_id] = os.[object_id]
AND ps.index_id = os.index_id
AND ps.partition_number = os.partition_number
INNER JOIN ['+@dbname+'].sys.indexes si
on
ps.object_id = si.object_id
and
ps.index_id = si.index_id
WHERE
ps.index_id > 0
AND ps.page_count > '+cast(@MinimumPageCount as varchar(10))+'
AND ps.index_level = 0
GROUP BY ps.database_id
, DB_NAME(ps.database_id)
, ps.[object_id]
, ps.index_id
, ps.partition_number
, os.range_scan_count
, si.name
OPTION (MAXDOP '+cast(@NumberOfThreads as varchar(3))+');'
exec(@cmd)
END
END
FETCH NEXT FROM db INTO @dbname
END
CLOSE db
DEALLOCATE db
DROP TABLE #dbnames
select * from ##IndexFragmentationLevels