Get Index Fragmentation Percentage for all Tables in SQL Server Database
Here is a useful script I've been able to put together from multiple posts online that gets the estimated index fragmentation for all tables in your database where the fragmentation percentage is greater than the where clause. Here's the SQL:
select object_name(itable.object_id) as tablename,
itable.name as IndexName,indexfrag.avg_fragmentation_in_percent
from sys.dm_db_index_physical_stats(db_id(), null, null, null, 'DETAILED') indexfrag
inner join sys.indexes itable on itable.object_id = indexfrag.object_id
and itable.index_id = indexfrag.index_id-- make sure to set this where clause to the percentage below which you want to exclude results.where indexfrag.avg_fragmentation_in_percent > 20order by avg_fragmentation_in_percent desc, tablename
Popular Articles
Last viewed:
- How to back up windows with Bacula
- How to Highlight the Day in the ASP.NET Calendar Control with the SelectedDate Property
- Data Access Layer using SqlDataReader and C# - Code Explanation
- Install Windws 2003 Terminal Service Licenses (Remote Desktop)
- Get the list of ODBC data source names programatically using C#
- C# Store/Retrieve File in Database Image Field using ODBC

Recent comments
13 hours 52 min ago
1 day 13 hours ago
3 days 14 hours ago
4 days 4 hours ago
4 days 15 hours ago
4 days 20 hours ago
5 days 36 min ago
1 week 21 hours ago
1 week 1 day ago
1 week 4 days ago