-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIndexFragmentation.sql
More file actions
103 lines (90 loc) · 2.64 KB
/
IndexFragmentation.sql
File metadata and controls
103 lines (90 loc) · 2.64 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
DROP TABLE IF EXISTS #IndexBuild
DECLARE @SQL VARCHAR(MAX)
DECLARE @TableName VARCHAR(255)
DECLARE @SchemaName VARCHAR(255)
DECLARE @IndexName VARCHAR(255)
DECLARE @FragThreshold float = 80 -- To stop rebuilds set this to 110
DECLARE @SchemaFilter VARCHAR(255) = 'ncov_store'
DECLARE @Rebuild BIT = 0
SELECT
dbschemas.[name] as 'SchemaName',
dbtables.[name] as 'TableName',
dbindexes.[name] as 'IndexName',
dbindexes.type_desc AS 'IndexType',
indexstats.avg_fragmentation_in_percent,
indexstats.page_count
INTO #IndexBuild
FROM
sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS indexstats
INNER JOIN
sys.tables dbtables
ON
dbtables.[object_id] = indexstats.[object_id]
INNER JOIN
sys.schemas dbschemas
ON
dbtables.[schema_id] = dbschemas.[schema_id]
INNER JOIN
sys.indexes AS dbindexes
ON
dbindexes.[object_id] = indexstats.[object_id]
AND
indexstats.index_id = dbindexes.index_id
WHERE
indexstats.database_id = DB_ID()
AND
dbschemas.[name] LIKE '%' + ISNULL(@SchemaFilter, '') + '%'
AND
indexstats.avg_fragmentation_in_percent >= @FragThreshold
ORDER BY
indexstats.avg_fragmentation_in_percent DESC
SELECT * FROM #IndexBuild
/*
WHILE ((SELECT COUNT(*) FROM #IndexBuild) > 0) AND (@Rebuild = 1)
BEGIN
SELECT TOP 1
@TableName = TableName,
@SchemaName = SchemaName,
@IndexName = IndexName
FROM
#IndexBuild
SET @SQL = 'ALTER INDEX ' + @IndexName + ' ON ' + @SchemaName + '.' + @TableName + ' REBUILD'
RAISERROR(@SQL, 0, 0) WITH NOWAIT
EXEC (@SQL)
DELETE
FROM
#IndexBuild
WHERE
TableName = @TableName
AND
SchemaName = @SchemaName
AND
IndexName = @IndexName
END
-- ALTER TABLE [ncov_store].[FACT_DAY_AGG] REBUILD;
SELECT dbschemas.[name] as 'Schema',
dbtables.[name] as 'Table',
dbindexes.[name] as 'Index',
indexstats.avg_fragmentation_in_percent,
indexstats.page_count
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS indexstats
INNER JOIN sys.tables dbtables on dbtables.[object_id] = indexstats.[object_id]
INNER JOIN sys.schemas dbschemas on dbtables.[schema_id] = dbschemas.[schema_id]
INNER JOIN sys.indexes AS dbindexes ON dbindexes.[object_id] = indexstats.[object_id]
AND indexstats.index_id = dbindexes.index_id
WHERE indexstats.database_id = DB_ID()
AND dbschemas.[name] = 'ncov_store'
AND dbtables.[name] = 'NCOV_LINELIST'
ORDER BY indexstats.avg_fragmentation_in_percent desc
*/
/*
ALTER INDEX IX__Sys_IsCurrent
ON ncov_store.NCOV_LINELIST REBUILD
ALTER INDEX PK_NCOV_LINELIST
ON ncov_store.NCOV_LINELIST REBUILD
ALTER INDEX IX__Sys_BatchID
ON ncov_store.NCOV_LINELIST REBUILD
ALTER INDEX IX__Sys_RowTitle
ON ncov_store.NCOV_LINELIST REBUILD
*/
--UPDATE STATISTICS ncov_store.NCOV_LINELIST