{"id":129,"date":"2008-10-20T17:12:03","date_gmt":"2008-10-20T15:12:03","guid":{"rendered":"https:\/\/www.sqlinthewild.co.za\/?p=129"},"modified":"2008-10-20T17:12:03","modified_gmt":"2008-10-20T15:12:03","slug":"what-is-fragmentation","status":"publish","type":"post","link":"https:\/\/www.sqlinthewild.co.za\/index.php\/2008\/10\/20\/what-is-fragmentation\/","title":{"rendered":"What is fragmentation?"},"content":{"rendered":"<p>It&#8217;s common knowledge that SQL indexes become fragmented over time and should be rebuilt to remove that fragmentation. But what exactly is fragmentation?<\/p>\n<p>First, a little bit on index structure.<\/p>\n<p>The index key defines the logical order of the index leaf pages. Each page has two pointers, one to the previous page and one to the next page in the index.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-136\" title=\"Index Structure\" src=\"https:\/\/www.sqlinthewild.co.za\/wp-content\/uploads\/2008\/10\/indexpages.png\" alt=\"A hypothetical index on an integer column\" width=\"413\" height=\"89\" \/><\/p>\n<p>In an ideal situation, the logical order of the pages would correspond to the physical order, and a page with higher index key values\u00a0 would have a higher page number (corresponding to physical position in the file) than a page with lower index key values.<\/p>\n<p>If that is the case for all pages in the index leaf level, then the index has no fragmentation. Unfortunatly, that is not normally the case. Inserts and updates to the index can cause page splits. A page split moves half the rows on one of the index leaf pages to a new page and then linkes that page into the chain. It&#8217;s unlikely that a free page will be in the correct physical logation in the file and hence after the page split, the physical and logical ordering of the pages will no longer be the same.<\/p>\n<p><!--more--><\/p>\n<p>The logical fragmentation is a measure of how different the logical and physical orderings are. It&#8217;s a ratio of the number of pages found out of order (lower in the file than the previous page in the index) to the total number of pages in the index.<\/p>\n<p>As an example, consider an index with 1000 pages in it. While traversing the index leaf level, 400 of those pages are found at a physical location lower than the page before them in the index. Hence the ratio of out of order pages to total pages is 400:1000 and we can say that the index is 40% fragmented.<\/p>\n<p>Because fragmentation is purely a feature of where the pages are on disk, it only affects SQL when the table\/index in question is not in memory and has to be read from disk. For this reason, it&#8217;s not usually necessary to worry about the fragmentation of tables with very few pages (&lt;100) as they will probably remain in memory if they are heavily used.<\/p>\n<p>On larger tables the presence of fragmentation at the leaf level will make scans of the index less efficient, requiring more IOs and disk time to get the data off disk. If the system already has an IO bottleneck, this can cause severe problems.<\/p>\n<h3>Detecting fragmentation<\/h3>\n<p>There are two ways to see the fragmentation of an index. The first is the older SQL 2000 way. It still works in SQL 2005 and 2008, but is deprecated in both versions and will be removed from a future version<\/p>\n<p><strong>DBCC SHOWCONTIG<\/strong><\/p>\n<p>Showcontig can display the fragmentation results for a single index, for all the indexes on a table or for all of the indexes in the database, depending on the options passed to it. It also can return results as a recordset, instead of text, which is very usweful for automated index maintenance<\/p>\n<p>[source:SQL]<br \/>\nDBCC SHOWCONTIG (&lt;Table Name&gt;) &#8212; just for this table<br \/>\nDBCC SHOWCONTIG (&lt;Table Name&gt;, &lt;Index Name&gt;) &#8212; just for this index<br \/>\nDBCC SHOWCONTIG WITH ALL_INDEXES &#8212; everything in the DB<br \/>\nDBCC SHOWCONTIG WITH TABLERESULTS &#8212; results in a recordset<br \/>\n[\/source]<\/p>\n<p><strong>sys.dm_db_index_physical_stats<\/strong><\/p>\n<p>This is one of the DMVs introduced with SQL 2005 and it&#8217;s the replacement for showcontig. It takes 5 parameters, the database ID, the table, the index, the partition and a setting for how detailed the results should be.<\/p>\n<p>[source:SQL]SELECT * FROM sys.dm_db_index_physical_stats (NULL, NULL, NULL, NULL, NULL) &#8212; all indexes on all tables in all databases<br \/>\nSELECT * FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL); &#8212; all indexes in this DB<br \/>\nSELECT * FROM sys.dm_db_index_physical_stats (DB_ID(), OBJECT_ID(&lt;Table Name&gt;), NULL, NULL, NULL); &#8212; all indexes on that table<br \/>\nSELECT * FROM sys.dm_db_index_physical_stats (DB_ID(), OBJECT_ID(&lt;Table Name&gt;), &lt;Index ID&gt;, NULL, NULL) &#8212; stats for just that index[\/source]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s common knowledge that SQL indexes become fragmented over time and should be rebuilt to remove that fragmentation. But what exactly is fragmentation? First, a little bit on index structure. The index key defines the logical order of the index&#8230; <a class=\"read-more-button\" href=\"https:\/\/www.sqlinthewild.co.za\/index.php\/2008\/10\/20\/what-is-fragmentation\/\">(Read more)<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[24,15],"tags":[],"class_list":["post-129","post","type-post","status-publish","format-standard","hentry","category-indexes","category-sql-server"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p7h6n-25","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/comments?post=129"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/posts\/129\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/media?parent=129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/categories?post=129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlinthewild.co.za\/index.php\/wp-json\/wp\/v2\/tags?post=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}