Created
December 14, 2021 12:15
-
-
Save SBajonczak/78a55acd50a79724cad6a65dba850cab to your computer and use it in GitHub Desktop.
Get Index Statistics
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
- Ensure a USE statement has been executed first. | |
SELECT [DatabaseName] | |
,[ObjectId] | |
,[ObjectName] | |
,[IndexId] | |
,[IndexDescription] | |
,CONVERT(DECIMAL(16, 1), (SUM([avg_record_size_in_bytes] * [record_count]) / (1024.0 * 1024))) AS [IndexSize(MB)] | |
,[lastupdated] AS [StatisticLastUpdated] | |
,[AvgFragmentationInPercent] | |
FROM ( | |
SELECT DISTINCT DB_Name(Database_id) AS 'DatabaseName' | |
,OBJECT_ID AS ObjectId | |
,Object_Name(Object_id) AS ObjectName | |
,Index_ID AS IndexId | |
,Index_Type_Desc AS IndexDescription | |
,avg_record_size_in_bytes | |
,record_count | |
,STATS_DATE(object_id, index_id) AS 'lastupdated' | |
,CONVERT([varchar](512), round(Avg_Fragmentation_In_Percent, 3)) AS 'AvgFragmentationInPercent' | |
FROM sys.dm_db_index_physical_stats(db_id(), NULL, NULL, NULL, 'detailed') | |
WHERE OBJECT_ID IS NOT NULL | |
AND Avg_Fragmentation_In_Percent <> 0 | |
) T | |
GROUP BY DatabaseName | |
,ObjectId | |
,ObjectName | |
,IndexId | |
,IndexDescription | |
,lastupdated | |
,AvgFragmentationInPercent |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment