{"id":1569,"date":"2016-08-02T08:56:35","date_gmt":"2016-08-02T02:56:35","guid":{"rendered":"http:\/\/techsatwork.com\/blog\/?p=1569"},"modified":"2016-08-02T08:56:35","modified_gmt":"2016-08-02T02:56:35","slug":"script-check-index-fragmentation-sql-server","status":"publish","type":"post","link":"https:\/\/techsatwork.com\/?p=1569","title":{"rendered":"Script check the index fragmentation in SQL Server"},"content":{"rendered":"<p>Today I had a need to find the index fragmentation on one of my sql server database and I found this script buried on my computer, thought I will share it here:<\/p>\n<p>DECLARE @DatabaseID int<\/p>\n<p>SET @DatabaseID = DB_ID()<\/p>\n<p>SELECT DB_NAME(@DatabaseID) AS DatabaseName,<br \/>\nschemas.[name] AS SchemaName,<br \/>\nobjects.[name] AS ObjectName,<br \/>\nindexes.[name] AS IndexName,<br \/>\nobjects.type_desc AS ObjectType,<br \/>\nindexes.type_desc AS IndexType,<br \/>\ndm_db_index_physical_stats.partition_number AS PartitionNumber,<br \/>\ndm_db_index_physical_stats.page_count AS [PageCount],<br \/>\ndm_db_index_physical_stats.avg_fragmentation_in_percent AS AvgFragmentationInPercent<br \/>\nFROM sys.dm_db_index_physical_stats (@DatabaseID, NULL, NULL, NULL, &#8216;LIMITED&#8217;) dm_db_index_physical_stats<br \/>\nINNER JOIN sys.indexes indexes ON dm_db_index_physical_stats.[object_id] = indexes.[object_id] AND dm_db_index_physical_stats.index_id = indexes.index_id<br \/>\nINNER JOIN sys.objects objects ON indexes.[object_id] = objects.[object_id]<br \/>\nINNER JOIN sys.schemas schemas ON objects.[schema_id] = schemas.[schema_id]<br \/>\nWHERE objects.[type] IN(&#8216;U&#8217;,&#8217;V&#8217;)<br \/>\nAND objects.is_ms_shipped = 0<br \/>\nAND indexes.[type] IN(1,2,3,4)<br \/>\nAND indexes.is_disabled = 0<br \/>\nAND indexes.is_hypothetical = 0<br \/>\nAND dm_db_index_physical_stats.alloc_unit_type_desc = &#8216;IN_ROW_DATA&#8217;<br \/>\nAND dm_db_index_physical_stats.index_level = 0<br \/>\nAND dm_db_index_physical_stats.page_count &gt;= 1000<\/p>\n<p>&nbsp;<\/p>\n<p>Best practice, you rebuild the index if the fragmentation is more than 40 % , if not a reorganization will suffice.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I had a need to find the index fragmentation on one of my sql server database and I found this script buried on my computer, thought I will share it here: DECLARE @DatabaseID int SET @DatabaseID = DB_ID() SELECT DB_NAME(@DatabaseID) AS DatabaseName, schemas.[name] AS SchemaName, objects.[name] AS ObjectName, indexes.[name] AS IndexName, objects.type_desc AS ObjectType, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","site-transparent-header":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[303,358,658],"tags":[933,934,935,306],"class_list":["post-1569","post","type-post","status-publish","format-standard","hentry","category-database","category-how-to","category-sql-server-2","tag-check-index","tag-index-fragmentation","tag-reorganize-index","tag-sql-server"],"_links":{"self":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1569","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1569"}],"version-history":[{"count":1,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1569\/revisions"}],"predecessor-version":[{"id":1570,"href":"https:\/\/techsatwork.com\/index.php?rest_route=\/wp\/v2\/posts\/1569\/revisions\/1570"}],"wp:attachment":[{"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techsatwork.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}