This article is half-done without your Comment! *** Please share your thoughts via Comment ***
In this post, I am sharing a T-SQL script to find an average percentage of index fragmentation in SQL Server.
When we are not getting the desired performance of the index, we should check the average percentage of Index Fragmentation.
The non-clustered index data stores into different pages, so there is always a chance of fragmentation.
I prepared a script to find index fragmentation, and if you see the index fragmentation, you should execute INDEX REBUILD or INDEX REORGANIZE on highly fragmented indexes to achieve the good performance of indexes.
1 2 3 4 5 6 7 8 9 |
SELECT object_name(ips.object_id) AS TableName ,i.name AS IndexName ,ips.index_type_desc AS IndexType ,ips.avg_fragmentation_in_percent ,ips.fragment_count ,ips.page_count FROM sys.dm_db_index_physical_stats (DB_ID(),NULL, NULL, NULL ,NULL) AS ips INNER JOIN sys.indexes AS i ON ips.OBJECT_ID = i.OBJECT_ID AND ips.index_id = i.index_id |