Created
June 19, 2018 23:02
-
-
Save ivaneftimov/2172feca8eb9752f69f82c8f0f37a6d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Returns index fragmentation status for a database | |
| -- Set the proper database name before executing it | |
| DECLARE @dbName nvarchar(100) | |
| SET @dbName = N'YourSitefinityDatabaseName'; | |
| DECLARE @databaseID int | |
| SELECT @databaseID = DB_ID(@dbName) | |
| IF (@databaseID IS NOT NULL) | |
| BEGIN | |
| SELECT DB.[Name] AS [DataBase], | |
| SO.[Name] AS TableName, | |
| SI.name as IndexName, | |
| IDX.[index_type_desc] as IndexType, | |
| IDX.[avg_fragmentation_in_percent] as IndexFragmentation, | |
| IDX.page_count | |
| FROM sys.dm_db_index_physical_stats ( | |
| @databaseID, | |
| NULL, | |
| NULL, | |
| NULL, | |
| NULL --'DETAILED' | |
| ) AS IDX | |
| INNER JOIN sys.objects SO ON IDX.object_id = SO.object_id | |
| INNER JOIN sys.indexes SI ON IDX.index_id = SI.index_id AND IDX.object_id = SI.object_id | |
| INNER JOIN sys.databases DB ON IDX.database_id = DB.database_id | |
| ORDER BY avg_fragmentation_in_percent DESC | |
| END | |
| ELSE | |
| PRINT 'Database not found' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment