Skip to content

Commit add7df7

Browse files
authored
Merge pull request #337 from HannahIgboke/main
SQL-415: Rebuilding All Indexes in a SQL Database
2 parents c4c0cbd + b3397ca commit add7df7

6 files changed

+94
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
USE University;
2+
GO
3+
4+
DECLARE @TableName NVARCHAR(255);
5+
DECLARE @SQL NVARCHAR(MAX);
6+
7+
DECLARE table_cursor CURSOR FOR
8+
SELECT QUOTENAME(SCHEMA_NAME(t.schema_id)) + '.' + QUOTENAME(t.name)
9+
FROM sys.tables t
10+
WHERE t.is_ms_shipped = 0;
11+
12+
OPEN table_cursor;
13+
FETCH NEXT FROM table_cursor INTO @TableName;
14+
15+
WHILE @@FETCH_STATUS = 0
16+
BEGIN
17+
SET @SQL = 'ALTER INDEX ALL ON ' + @TableName + ' REBUILD WITH (FILLFACTOR = 90, SORT_IN_TEMPDB = ON);';
18+
PRINT 'Rebuilding indexes on: ' + @TableName;
19+
EXEC sp_executesql @SQL;
20+
FETCH NEXT FROM table_cursor INTO @TableName;
21+
END;
22+
23+
CLOSE table_cursor;
24+
DEALLOCATE table_cursor;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
USE University;
2+
GO
3+
SELECT t.name AS Table_name,
4+
i.name AS Index_name,
5+
ps.avg_fragmentation_in_percent
6+
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, 'LIMITED') AS ps
7+
INNER JOIN sys.indexes i
8+
ON ps.object_id = i.object_id AND ps.index_id = i.index_id
9+
INNER JOIN sys.tables t
10+
ON i.object_id = t.object_id
11+
WHERE i.type_desc IN ('CLUSTERED', 'NONCLUSTERED') AND ps.database_id = DB_ID()
12+
ORDER BY ps.avg_fragmentation_in_percent DESC;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
IF NOT EXISTS (
2+
SELECT 1
3+
FROM sys.database_principals
4+
WHERE name = 'index_maintenance' AND type = 'R'
5+
)
6+
BEGIN
7+
CREATE ROLE index_maintenance;
8+
END
9+
GO
10+
11+
GRANT ALTER ON OBJECT::dbo.Teaching TO index_maintenance;
12+
GO
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
BACKUP DATABASE University
2+
TO DISK = 'C:\Backups\University_FULL.bak'
3+
WITH FORMAT,
4+
INIT,
5+
NAME = 'Full Backup Before Index Rebuild',
6+
SKIP,
7+
STATS = 10;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
USE University;
2+
GO
3+
4+
DECLARE @TableName NVARCHAR(255);
5+
DECLARE @IndexName NVARCHAR(255);
6+
DECLARE @SQL NVARCHAR(MAX);
7+
8+
DECLARE frag_cursor CURSOR FOR
9+
SELECT
10+
QUOTENAME(SCHEMA_NAME(t.schema_id)) + '.' + QUOTENAME(t.name),
11+
QUOTENAME(i.name)
12+
FROM
13+
sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, 'LIMITED') AS ps
14+
JOIN sys.indexes i ON ps.object_id = i.object_id AND ps.index_id = i.index_id
15+
JOIN sys.tables t ON i.object_id = t.object_id
16+
WHERE
17+
ps.avg_fragmentation_in_percent > 30
18+
AND i.type_desc IN ('CLUSTERED', 'NONCLUSTERED');
19+
20+
OPEN frag_cursor;
21+
FETCH NEXT FROM frag_cursor INTO @TableName, @IndexName;
22+
23+
WHILE @@FETCH_STATUS = 0
24+
BEGIN
25+
SET @SQL = 'ALTER INDEX ' + @IndexName + ' ON ' + @TableName + ' REBUILD WITH (FILLFACTOR = 90, SORT_IN_TEMPDB = ON);';
26+
PRINT 'Rebuilding index: ' + @IndexName + ' on table: ' + @TableName;
27+
EXEC sp_executesql @SQL;
28+
29+
FETCH NEXT FROM frag_cursor INTO @TableName, @IndexName;
30+
END;
31+
32+
CLOSE frag_cursor;
33+
DEALLOCATE frag_cursor;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ALTER INDEX [PK__Teaching__3213E83FFA8FEA28]
2+
ON dbo.Teaching
3+
REBUILD WITH (
4+
FILLFACTOR = 90,
5+
SORT_IN_TEMPDB = ON,
6+
STATISTICS_NORECOMPUTE = OFF);

0 commit comments

Comments
 (0)