{"id":82027,"date":"2008-02-26T23:10:24","date_gmt":"2008-02-26T23:10:24","guid":{"rendered":"https:\/\/www.webstaging.red-gate.com\/simple-talk\/?p=73179"},"modified":"2018-12-12T13:37:46","modified_gmt":"2018-12-12T13:37:46","slug":"2008-rebuilding-a-heap","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/2008-rebuilding-a-heap\/","title":{"rendered":"2008: Rebuilding a Heap"},"content":{"rendered":"<p>In 2005, rebuilding a table that was a heap (no clustered index) wasn&#8217;t easy.&nbsp; You could copy it to a different table, or you could add a clustered index and then drop it. <\/p>\n<p>In 2008, this is a far easier thing to do.&nbsp; They have added to the ALTER TABLE command a method to rebuild the table, which is the same as rebuilding the clustered index for a clustered table, but for a HEAP, it is the only way to go.&nbsp; <\/p>\n<p>In the following code sample, I create a heap, load it will &#8220;little data&#8221;, then expand every row to much larger values.&nbsp; The result is a little bit of fragmentation, but more than that, tons of forwarding pointers (when a row won&#8217;t fit on the same heap page, it gets moved to a different page, but the pointer to the row does not change in the indexes.)&nbsp; Rebuilding the heap is now really simple:<\/p>\n<p>create table heapDemo<br \/>(&nbsp;&nbsp; <br \/>&nbsp;&nbsp;&nbsp; value varchar(1000)<br \/>)<br \/>GO<br \/>set nocount on<br \/>insert into heapDemo<br \/>select &#8216;hi&#8217;<br \/>go 10000<br \/>&#8211;Expand the values to 500 times the size they were<br \/>update heapDemo<br \/>set value = replicate(&#8216;hi&#8217;,500)<\/p>\n<p>Now, check the stats of the table (using the index stats dmv, no less)<\/p>\n<p>select index_type_desc, fragment_count, page_count, forwarded_record_count&nbsp; <br \/>from sys.dm_db_index_physical_stats(db_id(),default,default,default,&#8217;DETAILED&#8217;)<br \/>where object_id = object_id(&#8216;heapDemo&#8217;) <\/p>\n<p>This returns:<\/p>\n<p>index_type_desc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fragment_count&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; page_count&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; forwarded_record_count<br \/>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>HEAP&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1443&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9961<\/p>\n<p>Now, you can rebuild the heap with the command:<\/p>\n<p><strong>alter table heapDemo rebuild<\/strong><\/p>\n<p>Check the values now:<\/p>\n<p>select index_type_desc, fragment_count, page_count, forwarded_record_count<br \/>from sys.dm_db_index_physical_stats(db_id(),default,default,default,&#8217;DETAILED&#8217;)<br \/>where object_id = object_id(&#8216;heapDemo&#8217;) <\/p>\n<p>This shows no that the forwarded_record_count is 0, which is the idea situation for your heap.<\/p>\n<p>index_type_desc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fragment_count&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; page_count&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; forwarded_record_count<br \/>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>HEAP&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1440&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 <\/p>\n<p>Nice new addition!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In 2005, rebuilding a table that was a heap (no clustered index) wasn&#8217;t easy.&nbsp; You could copy it to a different table, or you could add a clustered index and then drop it. In 2008, this is a far easier thing to do.&nbsp; They have added to the ALTER TABLE command a method to rebuild&#8230;&hellip;<\/p>\n","protected":false},"author":56085,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[19684],"class_list":["post-82027","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82027","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/56085"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=82027"}],"version-history":[{"count":1,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82027\/revisions"}],"predecessor-version":[{"id":82373,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82027\/revisions\/82373"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=82027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=82027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=82027"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=82027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}