{"id":710,"date":"2014-07-30T12:05:02","date_gmt":"2014-07-30T12:05:02","guid":{"rendered":"http:\/\/www.theSQLReport.com\/?p=710"},"modified":"2014-09-20T12:34:05","modified_gmt":"2014-09-20T12:34:05","slug":"tsql-query-to-find-fragmented-indexes-including-partitioned-indexes","status":"publish","type":"post","link":"https:\/\/www.theSQLReport.com\/?p=710","title":{"rendered":"TSQL Query to Find Fragmented Indexes Including Partitioned Indexes"},"content":{"rendered":"<p>What I wanted to find is the most fragmented indexes, including the partition number (for the partitioned tables) and size for each index.\u00a0 I want to bring back only the information that lets me make a quick decision on how to handle it.\u00a0 The query is set to bring back only those indexes with fragmentation greater then 5 percent set in the WHERE clause.<\/p>\n<p>This is a query that I have forged together from a couple of queries found on the internet (don&#8217;t remember where I found the queries, so that I can give credit).<\/p>\n<pre> --\r\n -- Highest Index Fragmentation % with Partition Number and Index Size\r\n --\r\n SELECT\r\n object_name(a.object_id) AS table_name,\r\n a.index_id,\r\n b.name AS index_name,\r\n b.type_desc,\r\n a.partition_number,\r\n a.avg_fragmentation_in_percent,\r\n FS.Indexsize as 'IndexSize(MB)'\r\n FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, 'LIMITED') a\r\n JOIN sys.indexes b on a.object_id = b.object_id and a.index_id = b.index_id\r\n join (SELECT\r\n i.OBJECT_ID,\r\n i.name AS IndexName,\r\n i.index_id AS IndexID,\r\n p.partition_number as partition_number,\r\n (8 * SUM(a.used_pages))\/1024 AS 'Indexsize'\r\n FROM sys.indexes AS i\r\n JOIN sys.partitions AS p ON p.OBJECT_ID = i.OBJECT_ID AND p.index_id = i.index_id\r\n JOIN sys.allocation_units AS a ON a.container_id = p.partition_id\r\n GROUP BY i.OBJECT_ID,i.index_id,i.name,p.partition_number) AS FS\r\n on a.object_id = FS.OBJECT_ID and b.name = FS.IndexName and a.partition_number = FS.partition_number\r\n where a.avg_fragmentation_in_percent &gt; 5\r\n order by a.avg_fragmentation_in_percent desc,object_name(a.object_id), a.index_id, b.name, b.type_desc, a.partition_number\r\n --\r\n --<\/pre>\n<p><a href=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/07\/Query_For_Index_Fragmentation.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-714 size-full\" src=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/07\/Query_For_Index_Fragmentation.jpg\" alt=\"Query_For_Index_Fragmentation\" width=\"1467\" height=\"719\" srcset=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/07\/Query_For_Index_Fragmentation.jpg 1467w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/07\/Query_For_Index_Fragmentation-300x147.jpg 300w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/07\/Query_For_Index_Fragmentation-1024x501.jpg 1024w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/07\/Query_For_Index_Fragmentation-210x102.jpg 210w\" sizes=\"(max-width: 1467px) 100vw, 1467px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What I wanted to find is the most fragmented indexes, including the partition number (for the partitioned tables) and size for each index.\u00a0 I want to bring back only the &hellip; <a class=\"readmore\" href=\"https:\/\/www.theSQLReport.com\/?p=710\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":714,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[20,21],"class_list":["post-710","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sql-server","tag-fragmented-indexes","tag-partitions"],"_links":{"self":[{"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/posts\/710"}],"collection":[{"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=710"}],"version-history":[{"count":13,"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/posts\/710\/revisions"}],"predecessor-version":[{"id":779,"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/posts\/710\/revisions\/779"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/media\/714"}],"wp:attachment":[{"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}