<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Fragmentation (part 4): what are heaps?</title><link>http://blogs.msdn.com/sqlserverstorageengine/archive/2006/09/19/761437.aspx</link><description>Ok - really catching up with the various blog post series I started back in June/July - time to bang out the next few posts in the series on index fragmentation. Remember I'm starting from first principles and covering What are records? What are pages?</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>The table scan from hell</title><link>http://blogs.msdn.com/sqlserverstorageengine/archive/2006/09/19/761437.aspx#945667</link><pubDate>Fri, 03 Nov 2006 23:00:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:945667</guid><dc:creator>SELECT Hints, Tips, Tricks FROM Hugo Kornelis WHERE RDBMS = 'SQL Server'</dc:creator><description>&lt;p&gt;Greg Linwood, a fellow SQL Server MVP, has started a series of articles in which he attempts to prove...&lt;/p&gt;</description></item><item><title>re: Fragmentation (part 4): what are heaps?</title><link>http://blogs.msdn.com/sqlserverstorageengine/archive/2006/09/19/761437.aspx#1120295</link><pubDate>Wed, 22 Nov 2006 08:15:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1120295</guid><dc:creator>Gent</dc:creator><description>&lt;p&gt;Paul - I just wante to make sure I understand correctly your second bullet point on &amp;quot;what is a heap&amp;quot;: &lt;/p&gt;
&lt;p&gt;&amp;quot;This means that rows will be inserted into the heap anywhere where there is space (don't let this statement confuse you - for a table that has nothing but inserts, records will always be appended to the last allocated page)&amp;quot;&lt;/p&gt;
&lt;p&gt;Microsoft kb article 297861 indicates:&lt;/p&gt;
&lt;p&gt;&amp;quot;In SQL Server 7.0, and later, SQL Server generally optimizes inserts into a heap with the assumption that saving space is more important than performance. That is the tradeoff you choose to make when you decide to leave a table as a heap. Therefore, an insert into a heap often spends time searching for a location to insert a new row.&amp;quot;&lt;/p&gt;
&lt;p&gt;Are you saying that if I have a heap which has never had any deletes or updates, SQL Server 7.0 and later do not try to optimize for space usage - they simnply insert the new rows at the last allocated page? Does the same apply if I start from an empty heap and insert millions of rows with a single INSERT/SELECT FROM statement?&lt;/p&gt;
</description></item><item><title>re: Fragmentation (part 4): what are heaps?</title><link>http://blogs.msdn.com/sqlserverstorageengine/archive/2006/09/19/761437.aspx#1201644</link><pubDate>Mon, 04 Dec 2006 03:20:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1201644</guid><dc:creator>Paul Randal - MSFT</dc:creator><description>&lt;p&gt;Hi Gent,&lt;/p&gt;
&lt;p&gt;The KB article really only applies when there is free space in the pages that comprise the heap. For a heap that is append-only, with no deletes, the free space will always be in the last allocated page - so there's nothing to optimize - the only place to insert the record is in the last allocated page. The same applies to the insert/select example you give.&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;
</description></item><item><title>re: Fragmentation (part 4): what are heaps?</title><link>http://blogs.msdn.com/sqlserverstorageengine/archive/2006/09/19/761437.aspx#1218478</link><pubDate>Wed, 06 Dec 2006 06:53:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1218478</guid><dc:creator>Gent</dc:creator><description>&lt;p&gt;Paul - thank you for the reply.&lt;/p&gt;
&lt;p&gt;If I create a variable length table then, depending on the data inserted on a page, it might happen that there is free space in pages other then the last allocated one. For example:&lt;/p&gt;
&lt;p&gt;create table T1(col1 varchar(8000))&lt;/p&gt;
&lt;p&gt;insert into T1 select replicate('a', 100)&lt;/p&gt;
&lt;p&gt;insert into T1 select replicate('a', 8000)&lt;/p&gt;
&lt;p&gt;The above two inserts allocate two pages with free space left on both of them. &lt;/p&gt;
&lt;p&gt;I used DBCC PAGE to see where would data go if I ran a third insert (by the way, thank you for the great posting on DBCC PAGE):&lt;/p&gt;
&lt;p&gt;insert into T1 select replicate('a', 10)&lt;/p&gt;
&lt;p&gt;Although there should be enough free space on the second page, in all my tests SQL Server consistently inserted the data in the first one. I ran into the same behaviour if I changed the second insert (the one that allocates the second page) to insert only 7800 characters:&lt;/p&gt;
&lt;p&gt;insert into T1 select replicate('a', 7800)&lt;/p&gt;
&lt;p&gt;Again, the third insert (inserting a 10 character row) would consistently use the first page, although there should be more than 200 bytes free on the second one.&lt;/p&gt;
&lt;p&gt;I also ran a loop as in the following and watched the FreeSpace Scan/sec counter being high for the duration of the loop (I was running these tests on my local machine with nothing else running on SQL Server):&lt;/p&gt;
&lt;p&gt;declare @counter int&lt;/p&gt;
&lt;p&gt;set @counter = 1&lt;/p&gt;
&lt;p&gt;while (@counter &amp;lt; 10000)&lt;/p&gt;
&lt;p&gt;begin&lt;/p&gt;
&lt;p&gt;	insert into T1 select replicate('a', 1000)&lt;/p&gt;
&lt;p&gt;	set @counter = @counter + 1&lt;/p&gt;
&lt;p&gt;end&lt;/p&gt;
&lt;p&gt;I ran my tests both on SQL Server 2000 and SQL Server 2005 with similar results.&lt;/p&gt;
&lt;p&gt;All the above seems to indicate that even in the case of 'insert only' heaps, SQL Server still tries to optimize for space and will utilize a page which might not necessarily be the last allocated one. What am I missing?&lt;/p&gt;
&lt;p&gt;Thanks again for your time and thank you for the great information and knowledge you are sharing in your posts.&lt;/p&gt;
&lt;p&gt;Gent&lt;/p&gt;
</description></item><item><title>re: Fragmentation (part 4): what are heaps?</title><link>http://blogs.msdn.com/sqlserverstorageengine/archive/2006/09/19/761437.aspx#1236220</link><pubDate>Fri, 08 Dec 2006 05:46:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1236220</guid><dc:creator>Paul Randal - MSFT</dc:creator><description>&lt;p&gt;Yes, when the records are randomly sized then it will make use of any free space in earlier pages.&lt;/p&gt;
</description></item><item><title>The table scan from hell</title><link>http://blogs.msdn.com/sqlserverstorageengine/archive/2006/09/19/761437.aspx#1431480</link><pubDate>Mon, 08 Jan 2007 01:05:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1431480</guid><dc:creator>SELECT Hints, Tips, Tricks FROM Hugo Kornelis WHERE RDBMS = 'SQL Server'</dc:creator><description>&lt;p&gt;Greg Linwood, a fellow SQL Server MVP, has started a series of articles in which he attempts to prove&lt;/p&gt;
</description></item><item><title>re: Fragmentation (part 4): what are heaps?</title><link>http://blogs.msdn.com/sqlserverstorageengine/archive/2006/09/19/761437.aspx#3713828</link><pubDate>Fri, 06 Jul 2007 01:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3713828</guid><dc:creator>alpha1105omega</dc:creator><description>&lt;p&gt;Hi, Paul&lt;/p&gt;
&lt;p&gt;Very nice blog on those fragmentation series..but where is you next one on the clustered index and non-clustered index?&lt;/p&gt;
&lt;p&gt;My another question,&lt;/p&gt;
&lt;p&gt;if all the rows from a page are deleted, that page gets deallocated and reclaimed by the database and if all pages are empty due to range deleteion in an extent, the extent will get deallocated and reclaimed by the database. Is that correct statement? Is that the same for the heap? I dont think so as &amp;quot;empty&amp;quot; page(due to deletion) do not reclaimed and deallocated for the heap.&lt;/p&gt;
&lt;p&gt;Can you explain in that area in respect to the when a range deletion occurs on the page, what happens to the page?&lt;/p&gt;
&lt;p&gt;You can ping me at billkan@microsoft.com &lt;/p&gt;
&lt;p&gt;Thank you,&lt;/p&gt;
&lt;p&gt;Bill&lt;/p&gt;
</description></item><item><title>re: Fragmentation (part 4): what are heaps?</title><link>http://blogs.msdn.com/sqlserverstorageengine/archive/2006/09/19/761437.aspx#3714009</link><pubDate>Fri, 06 Jul 2007 01:07:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3714009</guid><dc:creator>Paul Randal - MSFT</dc:creator><description>&lt;p&gt;Hi Bill,&lt;/p&gt;
&lt;p&gt;I haven't done them yet - I'll probably redo most of the series at the end of the summer.&lt;/p&gt;
&lt;p&gt;When pages and extents are deallocated is complicated, and the subject of a whole blog post. I'll get to it...&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;
</description></item><item><title>More Pages Reported Than Actually Exist in the Table</title><link>http://blogs.msdn.com/sqlserverstorageengine/archive/2006/09/19/761437.aspx#5692226</link><pubDate>Fri, 26 Oct 2007 17:42:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:5692226</guid><dc:creator>Kevin Kline</dc:creator><description>&lt;p&gt;MVP Hugo Kornelius once reported that he encountered a situation in which it was possible to perform&lt;/p&gt;
</description></item></channel></rss>