Retrieve Fragmented Indexes for Current DB

PHOTO EMBED

Wed Feb 02 2022 21:08:55 GMT+0000 (Coordinated Universal Time)

Saved by @jimbrig #sql

-- all fragmented indexes on current db, % fragmentation > 30
SELECT a.index_id, OBJECT_NAME(a.object_id), name, avg_fragmentation_in_percent  
FROM sys.dm_db_index_physical_stats (DB_ID(DB_NAME()), 
      NULL, NULL, NULL, NULL) AS a  
     JOIN sys.indexes AS b 
     ON a.object_id = b.object_id AND a.index_id = b.index_id
where avg_fragmentation_in_percent > 30
order by avg_fragmentation_in_percent desc
GO 
content_copyCOPY

Percent Fragmented > 30%

https://gist.github.com/jimbrig/5d91eef57ce1de7d7f799e92d565631d