-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSQL004-IndexRebuildOrReorganizeScriptGenerator.sql
More file actions
44 lines (38 loc) · 1.48 KB
/
SQL004-IndexRebuildOrReorganizeScriptGenerator.sql
File metadata and controls
44 lines (38 loc) · 1.48 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
/*
-----------------------------------------------@kisinamso-----------------------------------------------
|If you want to generate index reorganize or rebuild scripts in a database you can use this script. |
|I explaid parts to work do to below. |
-----------------------------------------------@kisinamso-----------------------------------------------
*/
--Please Enter DatabaseName to below.
USE [DatabaseName]
GO
DECLARE @param VARCHAR(MAX)
DECLARE curs CURSOR LOCAL FAST_FORWARD FOR
SELECT
--OBJECT_NAME(ind.OBJECT_ID) AS TableName,
--ind.name AS IndexName,
--indexstats.index_type_desc AS IndexType,
--indexstats.avg_fragmentation_in_percent,
CASE
WHEN indexstats.avg_fragmentation_in_percent BETWEEN 5 AND 30
THEN 'ALTER INDEX ' + name + ' ON ' + OBJECT_NAME(ind.OBJECT_ID) + ' REORGANIZE WITH (ONLINE = ON);'
WHEN indexstats.avg_fragmentation_in_percent > 30
THEN 'ALTER INDEX ' + name + ' ON ' + OBJECT_NAME(ind.OBJECT_ID) + ' REBUILD WITH (ONLINE = ON);'
ELSE
NULL
END AS Script
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, NULL) indexstats
INNER JOIN sys.indexes ind ON ind.object_id = indexstats.object_id AND ind.index_id = indexstats.index_id
WHERE indexstats.avg_fragmentation_in_percent >= 5 AND ind.name IS NOT NULL
ORDER BY indexstats.avg_fragmentation_in_percent DESC
OPEN curs
FETCH NEXT FROM curs INTO @param
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT(@param)
EXEC( @param)
FETCH NEXT FROM curs INTO @param
END
CLOSE curs
DEALLOCATE curs