Finding Fragmentation Rates of Indexes in SQL Server
Hello to everyone,
In this article, I will try to give information about finding Fragmentation rates of Indexes in SQL Server.
In SQL Server, in some cases, you may want to find the Fragmentation rates of Indexes.
You can easily do this using the code below.
SELECT ps.object_id,
i.name AS IndexName,
OBJECT_SCHEMA_NAME(ps.object_id) AS ObjectSchemaName,
OBJECT_NAME(ps.object_id) AS ObjectName,
ps.avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, 'LIMITED') ps
INNER JOIN sys.indexes i
ON i.object_id = ps.object_id
AND i.index_id = ps.index_id
WHERE avg_fragmentation_in_percent > 5
AND ps.index_id > 0
ORDER BY avg_fragmentation_in_percent DESC;
When you run the above code, you will see a result similar to the one below.
As can be seen, the Fragmentation rates of the Indexes have been found.
Good luck to everyone in business and life.