-
-
Save marcemarc/9d6553e881a7cf536c8bd98f8c35fd92 to your computer and use it in GitHub Desktop.
How Fragmented are your database indexes
This file contains 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
SELECT | |
[dbindexes].[object_id] as [Id], | |
[dbtables].[name] as [Table], | |
[dbindexes].[name] as [IndexName], | |
[dbindexes].[type_desc] as [IndexType], | |
[indexstats].[avg_fragmentation_in_percent] as [FragmentationPercent], | |
[indexstats].[page_count] as [PageCount], | |
CASE lob_data_space_id WHEN 0 THEN 0 ELSE 1 END AS [HasLobData] | |
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].[indexes] [dbindexes] ON [dbindexes].[object_id] = [indexstats].[object_id] AND [indexstats].[index_id] = [dbindexes].[index_id] | |
WHERE [indexstats].[database_id] = DB_ID() | |
AND [dbindexes].[name] IS NOT NULL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment