<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>Tim Chapman</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/timchapman/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/timchapman/feed.xml</link>
    <description>SQL Server Dedicated Support Engineer (PFE)</description>
    <lastBuildDate>Tue, 16 Apr 2019 05:33:34 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Visualizing Index Fragmenation</title>
      <link>https://docs.microsoft.com/archive/blogs/timchapman/visualizing-index-fragmenation</link>
      <pubDate>Mon, 03 Dec 2012 08:15:00 GMT</pubDate>
      <dc:creator><![CDATA[TimChapman]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/timchapman/2012/12/03/visualizing-index-fragmenation/</guid>
      <description><![CDATA[We all probably realize that fragmentation can be a performance hindrance, especially for scanning...]]></description>
      <content:encoded><![CDATA[<p>We all probably realize that fragmentation can be a performance hindrance, especially for scanning operations.&nbsp; But, what does fragmentation really mean?&nbsp; What does it look like?&nbsp; I've been doing a lot of training for customers lately in terms of fragmentation so I wanted to do a blog post and dig a bit further into what index fragmentation really looks from a SQL Server page perspective.&nbsp; I'll create a fragmented index and show you how the logical page linkages look like vs. what they should be if they were not fragmented at all.&nbsp;</p>
<p>To begin, I'll create a database named FragmentsDB.</p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">use</span> <span style="color: blue;">master</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">go</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">SET</span> <span style="color: blue;">STATISTICS</span> <span style="color: blue;">XML</span> <span style="color: blue;">OFF</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">SET</span> <span style="color: blue;">STATISTICS</span> <span style="color: blue;">IO</span> <span style="color: blue;">OFF</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">SET</span> <span style="color: blue;">NOCOUNT</span> <span style="color: blue;">ON</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">go</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">IF</span> <span style="color: fuchsia;">DB_ID<span style="color: gray;">(<span style="color: red;">'FragmentsDB'<span style="color: gray;">)</span> <span style="color: gray;">IS</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL</span> <br /></span></span></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">BEGIN</span>&nbsp;<br /></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">ALTER</span> <span style="color: blue;">DATABASE</span> <span style="color: teal;">FragmentsDB</span>&nbsp;<br /></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">SET</span> <span style="color: blue;">SINGLE_USER</span> <span style="color: blue;">WITH</span> <span style="color: blue;">ROLLBACK</span> <span style="color: blue;">IMMEDIATE</span>&nbsp;</span>&nbsp;</p>
<p><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">DROP</span> <span style="color: blue;">DATABASE</span> <span style="color: teal;">FragmentsDB</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">END</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">GO</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">CREATE</span> <span style="color: blue;">DATABASE</span> <span style="color: teal;">FragmentsDB</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">GO</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">ALTER</span> <span style="color: blue;">DATABASE</span> <span style="color: teal;">FragmentsDB</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">SET</span> <span style="color: blue;">RECOVERY</span> <span style="color: blue;">SIMPLE</span> <br /></span><span style="color: blue; font-family: Consolas; font-size: 12pt;">GO </span></p>
<p>Next, create a table named NumbersTable and load some data into it.&nbsp; The manner in which I am loading data will ensure that once I'm done the non-clustered index that I've created on the table will be noticeably fragmented.&nbsp; The way I am doing this is to load numbers into the table, skipping every 3rd number.&nbsp; Then, I'll load back into the table some of the data that I skipped before.&nbsp; In doing this, the data that I load in the second insert will need to be placed in the appropriate pages (remember, an index is ordered and we have to put the rows into the page where the sorted values fit logically), causing pages to split and logical fragmentation to occur. Note that I am not making the pages completely full, as is the default fillfactor. This will ensure that for the purposes of this test that the index isn't 100% fragmented.</p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">USE</span> <span style="color: teal;">FragmentsDB</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">GO</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">CREATE</span> <span style="color: blue;">TABLE</span> <span style="color: teal;">NumbersTable</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: gray;">(</span>&nbsp;<br /></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: teal;">NumberValue</span> <span style="color: blue;">BIGINT</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL,</span>&nbsp;<br /></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: teal;">BiggerNumber</span> <span style="color: blue;">BIGINT</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL,</span>&nbsp;<br /></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: teal;">CharacterColumn</span> <span style="color: blue;">CHAR<span style="color: gray;">(</span>50<span style="color: gray;">)</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: gray;">)</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">GO</span> </span></p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">ALTER</span> <span style="color: blue;">TABLE</span> <span style="color: teal;">NumbersTable</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">ADD</span> <span style="color: blue;">CONSTRAINT</span> <span style="color: teal;">PK_NumbersTable</span> <span style="color: blue;">PRIMARY</span> <span style="color: blue;">KEY</span> <span style="color: blue;">CLUSTERED <span style="color: gray;">(<span style="color: teal;">NumberValue<span style="color: gray;">)</span> <br /></span></span></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">GO</span>&nbsp;</span>&nbsp;</p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">CREATE</span> <span style="color: blue;">INDEX</span> <span style="color: teal;">idx_NumbersTable</span> <span style="color: blue;">ON</span> <span style="color: teal;">NumbersTable<span style="color: gray;">(<span style="color: teal;">BiggerNumber<span style="color: gray;">)</span> <br /></span></span></span></span><span style="color: blue; font-family: Consolas; font-size: 12pt;">WITH<span style="color: gray;">(<span style="color: blue;">FILLFACTOR<span style="color: gray;">=</span>75<span style="color: gray;">)</span> <br /></span></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">GO</span> </span></p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;"><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">INSERT</span> <span style="color: blue;">INTO</span> <span style="color: teal;">NumbersTable</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: gray;">(</span>&nbsp;<br /></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: teal;">NumberValue<span style="color: gray;">,</span> BiggerNumber<span style="color: gray;">,</span> CharacterColumn</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: gray;">)</span> <br /></span></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">SELECT</span>&nbsp;<br /></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: teal;">NumberValue<span style="color: gray;">,</span>&nbsp;<br /></span></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: teal;">NumberValue</span> <span style="color: gray;">+</span> 5000000<span style="color: gray;">,</span>&nbsp;<br /></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: gray;">LEFT(<span style="color: fuchsia;">REPLICATE<span style="color: gray;">((<span style="color: fuchsia;">CAST<span style="color: gray;">(<span style="color: teal;">NumberValue</span> <span style="color: blue;">as</span> <span style="color: blue;">VARCHAR<span style="color: gray;">(</span>50<span style="color: gray;">))),</span>50<span style="color: gray;">),</span>50<span style="color: gray;">)</span> <br /></span></span></span></span></span></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">FROM</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: gray;">(</span>&nbsp;<br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">SELECT</span>&nbsp;<br /></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: teal;">NumberValue</span> <span style="color: gray;">=</span> <span style="color: fuchsia;">row_number<span style="color: gray;">()</span> <span style="color: blue;">over<span style="color: gray;">(<span style="color: blue;">order</span> <span style="color: blue;">by</span> <span style="color: fuchsia;">newid<span style="color: gray;">()</span> <span style="color: blue;">asc<span style="color: gray;">)</span>&nbsp;<br /></span></span></span></span></span></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">FROM</span> <span style="color: blue;">master<span style="color: gray;">..<span style="color: teal;">spt_values</span> <span style="color: teal;">a</span>&nbsp;<br /></span></span></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: gray;">CROSS</span> <span style="color: gray;">APPLY</span> <span style="color: blue;">master<span style="color: gray;">..<span style="color: teal;">spt_values</span> <span style="color: teal;">b</span>&nbsp;<br /></span></span></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">WHERE</span> <span style="color: teal;">a<span style="color: gray;">.<span style="color: blue;">type</span> =</span> <span style="color: red;">'P'</span> <span style="color: gray;">AND</span> a<span style="color: gray;">.<span style="color: teal;">number</span> &lt;=</span> 500 <span style="color: gray;">AND</span> a<span style="color: gray;">.<span style="color: teal;">number</span> &gt;</span> 0 <span style="color: gray;">AND</span>&nbsp;<br /></span></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: teal;">b<span style="color: gray;">.<span style="color: blue;">type</span> =</span> <span style="color: red;">'P'</span> <span style="color: gray;">AND</span> b<span style="color: gray;">.<span style="color: teal;">number</span> &lt;=</span> 500 <span style="color: gray;">AND</span> b<span style="color: gray;">.<span style="color: teal;">number</span> &gt;</span> 0</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: gray;">)</span> <span style="color: teal;">a</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">WHERE</span> <span style="color: teal;">NumberValue</span> <span style="color: gray;">%</span> 3 <span style="color: gray;">!=</span> 0 <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">GO</span>&nbsp;</span>&nbsp;</p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">INSERT</span> <span style="color: blue;">INTO</span> <span style="color: teal;">NumbersTable</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: gray;">(</span>&nbsp;<br /></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: teal;">NumberValue<span style="color: gray;">,</span> BiggerNumber<span style="color: gray;">,</span> CharacterColumn</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: gray;">)</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">SELECT</span> <span style="color: blue;">TOP<span style="color: gray;">(</span>30000<span style="color: gray;">)</span>&nbsp;<br /></span></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: teal;">NumberValue<span style="color: gray;">,</span>&nbsp;</span>&nbsp;<br /></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: teal;">NumberValue</span> <span style="color: gray;">+</span> 5000000<span style="color: gray;">,</span>&nbsp;<br /></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: gray;">LEFT(<span style="color: fuchsia;">REPLICATE<span style="color: gray;">((<span style="color: fuchsia;">CAST<span style="color: gray;">(<span style="color: teal;">NumberValue</span> <span style="color: blue;">as</span> <span style="color: blue;">VARCHAR<span style="color: gray;">(</span>50<span style="color: gray;">))),</span>50<span style="color: gray;">),</span>50<span style="color: gray;">)</span> <br /></span></span></span></span></span></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">FROM</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: gray;">(</span>&nbsp;<br /></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">SELECT</span>&nbsp;<br /></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: teal;">NumberValue</span> <span style="color: gray;">=</span> <span style="color: fuchsia;">row_number<span style="color: gray;">()</span> <span style="color: blue;">over<span style="color: gray;">(<span style="color: blue;">order</span> <span style="color: blue;">by</span> <span style="color: fuchsia;">newid<span style="color: gray;">()</span> <span style="color: blue;">asc<span style="color: gray;">)</span>&nbsp;<br /></span></span></span></span></span></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">FROM</span> <span style="color: blue;">master<span style="color: gray;">..<span style="color: teal;">spt_values</span> <span style="color: teal;">a</span>&nbsp;<br /></span></span></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: gray;">CROSS</span> <span style="color: gray;">APPLY</span> <span style="color: blue;">master<span style="color: gray;">..<span style="color: teal;">spt_values</span> <span style="color: teal;">b</span>&nbsp;<br /></span></span></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">WHERE</span> <span style="color: teal;">a<span style="color: gray;">.<span style="color: blue;">type</span> =</span> <span style="color: red;">'P'</span> <span style="color: gray;">AND</span> a<span style="color: gray;">.<span style="color: teal;">number</span> &lt;=</span> 500 <span style="color: gray;">AND</span> a<span style="color: gray;">.<span style="color: teal;">number</span> &gt;</span> 0 <span style="color: gray;">AND</span>&nbsp;<br /></span></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: teal;">b<span style="color: gray;">.<span style="color: blue;">type</span> =</span> <span style="color: red;">'P'</span> <span style="color: gray;">AND</span> b<span style="color: gray;">.<span style="color: teal;">number</span> &lt;=</span> 500 <span style="color: gray;">AND</span> b<span style="color: gray;">.<span style="color: teal;">number</span> &gt;</span> 0</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: gray;">)</span> <span style="color: teal;">a</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">WHERE</span> <span style="color: teal;">NumberValue</span> <span style="color: gray;">%</span> 3 <span style="color: gray;">=</span> 0 <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">GO</span> </span></p>
<p>To look at the amount of logical fragmentation in the leaf layer of the index, use the DMV sys.dm_db_index_physical_stats.&nbsp; We can see that the fragmentation is right at 53.32%.</p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">SELECT</span> <span style="color: teal;">avg_fragmentation_in_percent<span style="color: gray;">,</span> index_level<span style="color: gray;">,</span> record_count<span style="color: gray;">,</span> page_count</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">FROM</span> <span style="color: green;">sys<span style="color: gray;">.<span style="color: green;">dm_db_index_physical_stats<span style="color: gray;">(<span style="color: fuchsia;">DB_ID<span style="color: gray;">(),</span> OBJECT_ID<span style="color: gray;">(<span style="color: red;">'NumbersTable'<span style="color: gray;">),</span> 2<span style="color: gray;">,</span> <span style="color: gray;">NULL,</span> 'Detailed'<span style="color: gray;">) </span></span></span></span></span></span></span></span></span></p>
<p>&nbsp;<a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/VisualFrag/0841.Frag1.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-VisualFrag/0841.Frag1.jpg"><img style="max-width: 550px;" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/VisualFrag/0841.Frag1.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-VisualFrag/0841.Frag1.jpg" alt="" border="0" /></a>&nbsp;</p>
<p>So, what does a logical fragmentation of 53.32% really mean?&nbsp; In the simplest of terms, it means that just over half of the pages in the leaf level of the index are not physically located in the order in which the keys in the index indicate that they should be.&nbsp; In an index that had 0 fragmentation, the leaf level pages would be listed right next to each other logically and physically.&nbsp; It would look something similar to this:</p>
<p>&nbsp;</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/VisualFrag/0827.Frag2.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-VisualFrag/0827.Frag2.jpg"><img style="max-width: 550px;" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/VisualFrag/0827.Frag2.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-VisualFrag/0827.Frag2.jpg" alt="" border="0" /></a></p>
<p>Note: for simplicity purposes I am leaving out the fact that the rows on a given page aren't stored in physical order. An offset array is used at the page level to track the ordering of the rows on a given page.</p>
<p>However, when logical fragmentation comes into play, the tree can look like something totally different.&nbsp; The physical order (the allocation order) doesn't match up to the logical order of the keys (the sequential numbers in this case).&nbsp; So, unless SQL Server is running an allocation order scan then it is very difficult to perform a scan operation in an efficient manner.&nbsp;</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/VisualFrag/6204.Frag3.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-VisualFrag/6204.Frag3.jpg"><img style="max-width: 550px;" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/VisualFrag/6204.Frag3.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-VisualFrag/6204.Frag3.jpg" alt="" border="0" /></a></p>
<p>&nbsp;</p>
<p>SQL Server 2012 exposes a new DMV, sys.dm_db_database_page_allocations, that we can use to see the page linkages for a particular entity.&nbsp; (The output from this DMV is very similar to the output to the DBCC IND command.&nbsp; It is just more easily consumable).</p>
<p>In this case, I will try to calculate the fragmentation in the index based upon checking the allocation order linkages in the DMV vs. logical page linkages that I calculate using the new <a href="https://msdn.microsoft.com/en-us/library/hh213125.aspx">LEAD</a> windowing function in 2012.&nbsp; This new function allows you to look at a value that came just before the current record in a resultset.</p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">SELECT</span>&nbsp;<br /></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: teal;">Fragmentation</span> <span style="color: gray;">=<span style="color: blue;"> <span style="color: gray;">((<span style="color: fuchsia;">COUNT<span style="color: gray;">(*)*</span>1.00 <span style="color: gray;">-</span> SUM<span style="color: gray;">(<span style="color: blue;">Case</span> <span style="color: blue;">When</span> <span style="color: teal;">NextPhysicalPageID</span> <span style="color: gray;">=</span> <span style="color: teal;">NextLogicalPageID</span> <span style="color: blue;">then</span> 1 <span style="color: blue;">else</span> 0 <span style="color: blue;">end</span></span><span style="color: blue;"><span style="color: gray;">))/<span style="color: fuchsia;">COUNT<span style="color: gray;">(*)*</span>1.00<span style="color: gray;">)*</span>100<span style="color: gray;">,</span>&nbsp;<br /></span></span></span></span></span></span></span></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">PageCount</span> <span style="color: gray;">=</span> <span style="color: fuchsia;">COUNT<span style="color: gray;">(*)</span>&nbsp;</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">FROM</span> </span></p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: gray;">(</span>&nbsp;<br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">SELECT</span> <span style="color: teal;">allocated_page_page_id<span style="color: gray;">,</span> NextLogicalPageID</span> <span style="color: gray;">=</span> <span style="color: teal;">next_page_page_id<span style="color: gray;">,</span>&nbsp;<br /></span></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: teal;">NextPhysicalPageID</span> <span style="color: gray;">=</span> <span style="color: teal;">LEAD<span style="color: gray;">(<span style="color: teal;">allocated_page_page_id<span style="color: gray;">)</span> <span style="color: blue;">over<span style="color: gray;">(<span style="color: blue;">partition</span> <span style="color: blue;">by</span> <span style="color: fuchsia;">object_id<span style="color: gray;">,</span> <span style="color: teal;">index_id</span> <span style="color: blue;">order</span> <span style="color: blue;">by</span> <span style="color: teal;">allocated_page_page_id<span style="color: gray;">)</span>&nbsp;<br /></span></span></span></span></span></span></span></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">FROM</span> <span style="color: green;"><strong>sys<span style="color: gray;">.<span style="color: teal;">dm_db_database_page_allocations</span></span></strong><span style="color: gray;">(<span style="color: fuchsia;">DB_ID<span style="color: gray;">(),</span> OBJECT_ID<span style="color: gray;">(<span style="color: red;">'NumbersTable'<span style="color: gray;">),</span> 2<span style="color: gray;">,</span> <span style="color: gray;">NULL,</span> 'DETAILED'<span style="color: gray;">)</span>&nbsp;<br /></span></span></span></span></span></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">WHERE</span> <span style="color: teal;">page_type_desc</span> <span style="color: gray;">=</span> <span style="color: red;">'INDEX_PAGE'</span> <span style="color: gray;">and</span> <span style="color: teal;">page_level</span> <span style="color: gray;">=</span> 0 <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: gray;">)</span> <span style="color: teal;">a</span> </span></p>
<p>Looking at the results from the query above, the fragmentation output that is the same as produced from the sys.dm_db_index_physical_stats DMV.&nbsp; There are a few outlying cases I've seen so far where the output between these 2 didn't match up exactly (usually off &lt; 1 %).&nbsp; I need to dig into the source code to determine why.&nbsp;</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/VisualFrag/2843.Frag4-1.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-VisualFrag/2843.Frag4_2D00_1.jpg"><img style="max-width: 550px;" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/VisualFrag/2843.Frag4-1.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-VisualFrag/2843.Frag4_2D00_1.jpg" alt="" border="0" /></a>&nbsp;&nbsp;</p>
<p>At this point, you may be thinking&hellip;"Big Deal.&nbsp; I can already find the fragmentation of an index.&nbsp; Why would I need a new way to do it?"&nbsp; The answer is &ndash; you don't.&nbsp; You should still use the regular method of identifying logical fragmentation.&nbsp; The aggregated query above is a sanity check to make sure the two methods match up.&nbsp; The real reason for this blog post is below.&nbsp; In this query I show the detail around my fragmented index.&nbsp; The logic is the same as the aggregated query above, but is this time showing the detail associated with the page linkages.</p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">SELECT</span>&nbsp;<br /></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: gray;">*,</span> <span style="color: teal;">PageLinkMatch</span> <span style="color: gray;">=</span> <span style="color: blue;">Case</span> <span style="color: blue;">When</span> <span style="color: teal;">NextPhysicalPageID</span> <span style="color: gray;">=</span> <span style="color: teal;">NextLogicalPageID</span> <span style="color: blue;">then</span> 1 <span style="color: blue;">else</span> 0 <span style="color: blue;">end</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">FROM</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: gray;">(</span>&nbsp;<br /></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">SELECT</span> <span style="color: teal;">allocated_page_page_id<span style="color: gray;">,</span> NextLogicalPageID</span> <span style="color: gray;">=</span> <span style="color: teal;">next_page_page_id<span style="color: gray;">,</span>&nbsp;<br /></span></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: teal;">NextPhysicalPageID</span> <span style="color: gray;">=</span> <span style="color: teal;">LEAD<span style="color: gray;">(<span style="color: teal;">allocated_page_page_id<span style="color: gray;">)</span> <span style="color: blue;">over<span style="color: gray;">(<span style="color: blue;">partition</span> <span style="color: blue;">by</span> <span style="color: fuchsia;">object_id<span style="color: gray;">,</span> <span style="color: teal;">index_id</span> <span style="color: blue;">order</span> <span style="color: blue;">by</span> <span style="color: teal;">allocated_page_page_id<span style="color: gray;">)</span>&nbsp;<br /></span></span></span></span></span></span></span></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">FROM</span> <span style="color: green;"><strong>sys<span style="color: gray;">.<span style="color: teal;">dm_db_database_page_allocations</span></span></strong><span style="color: gray;">(<span style="color: fuchsia;">DB_ID<span style="color: gray;">(),</span> OBJECT_ID<span style="color: gray;">(<span style="color: red;">'NumbersTable'<span style="color: gray;">),</span> 2<span style="color: gray;">,</span> <span style="color: gray;">NULL,</span> 'DETAILED'<span style="color: gray;">)</span>&nbsp;<br /></span></span></span></span></span></span><span style="font-family: Consolas; font-size: 12pt;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">WHERE</span> <span style="color: teal;">page_type_desc</span> <span style="color: gray;">=</span> <span style="color: red;">'INDEX_PAGE'</span> <span style="color: gray;">and</span> <span style="color: teal;">page_level</span> <span style="color: gray;">=</span> 0 <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: gray;">)</span> <span style="color: teal;">a</span> </span></p>
<p>&nbsp;</p>
<p>I am checking to see if the NextLogicalPage (as is returned by the DMV) matches up to the NextPhysicalPage that I am calculating.&nbsp; If the pages match, then that specific linkage is not fragmented.&nbsp; If they do not match, then the link IS fragmented.&nbsp; Let's take a look at a specific example from my output (your mileage will vary with the page numbers, etc).&nbsp; In the case of Page 997 the next physical page matches to the next logical page.&nbsp; The page before 997 pointed logically to page 3019.&nbsp; This was due to the page splits caused by the insert statements created above.&nbsp; However, the next physical page (the order of the page allocation) is page 997.&nbsp; This "logical fragmentation" is the cause of SQL Server having to break up IOs into multiple operations for scanning purposes.&nbsp;</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/VisualFrag/8764.Frag4.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-VisualFrag/8764.Frag4.jpg"><img style="max-width: 550px;" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/VisualFrag/8764.Frag4.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-VisualFrag/8764.Frag4.jpg" alt="" border="0" /></a></p>
<p>&nbsp;</p>
<p>In fact, we can further investigate the page using DBCC PAGE.&nbsp; By supplying the page number, we can see that the previous page and next page in the page header matches up to the output that we are seeing in the DMV.</p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">DBCC</span> <span style="color: teal;">TRACEON<span style="color: gray;">(</span>3604<span style="color: gray;">,</span> <span style="color: gray;">-</span>1<span style="color: gray;">)</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">DBCC</span> <span style="color: blue;">PAGE<span style="color: gray;">(<span style="color: teal;">FragmentsDB<span style="color: gray;">,</span> 1<span style="color: gray;">,</span> 997<span style="color: gray;">,</span> 2<span style="color: gray;">) </span></span></span></span></span></p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/VisualFrag/6170.Frag5.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-VisualFrag/6170.Frag5.jpg"><img style="max-width: 550px;" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/VisualFrag/6170.Frag5.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-VisualFrag/6170.Frag5.jpg" alt="" border="0" /></a>&nbsp;</p>
<p>I hope this helps in visualizing what logical fragmentation looks at from the SQL Server point of view. From a performance point of view, fragmentation isn't really that big of a deal for operations that are doing singleton style lookups &ndash; a few pages returned isn't going to be hindered by fragemented index. Scan operations will suffer because the engine will need to break up the IO for the scan into multiple IOs when fragmentation is present.</p>]]></content:encoded>
    </item>
    <item>
      <title>Index Rebuild vs Reorganize:  The transaction log edition</title>
      <link>https://docs.microsoft.com/archive/blogs/timchapman/index-rebuild-vs-reorganize-the-transaction-log-edition</link>
      <pubDate>Fri, 28 Sep 2012 09:29:00 GMT</pubDate>
      <dc:creator><![CDATA[TimChapman]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/timchapman/2012/09/28/index-rebuild-vs-reorganize-the-transaction-log-edition/</guid>
      <description><![CDATA[Recently a friend and fellow PFE Dave Levy (Twitter/Blog) and I were discussing a question someone...]]></description>
      <content:encoded><![CDATA[<p>Recently a friend and fellow PFE Dave Levy (<a title="Twitter" href="https://twitter.com/Dave_Levy">Twitter</a>/<a title="Blog" href="https://adventuresinsql.com/">Blog</a>) and I were discussing a question someone posted in a forum. The question was in regards to database mirroring latency when a reorganization occurs. Dave mentioned that he has seen in the past that switching from performing a reorganization to a rebuild resulted in better throughput with mirroring. So, I thought it would be a really interesting and really geeky (and, as it turns out really relevant) blog post to contrast the two operations in regards to the transaction log throughput.&nbsp;So, the goal of this post is to compare and contrast the log generations rates for a rebuild vs a reorg operation for a perfectly fragmented index.&nbsp; You should always consider the fragmentation levels for the index along with the requirements for your application and workload.&nbsp;For more information regarding which operation you should be choosing for rebuilding your indexes, check out this blog post by Paul Randal:&nbsp; <a href="https://www.sqlskills.com/blogs/paul/post/Where-do-the-Books-Online-index-fragmentation-thresholds-come-from.aspx">https://www.sqlskills.com/blogs/paul/post/Where-do-the-Books-Online-index-fragmentation-thresholds-come-from.aspx</a></p>
<p>Before I get to the demo, let's compare the two methods.&nbsp; Here is a snippet taken from this link:&nbsp; <a href="https://technet.microsoft.com/en-us/library/ms189858.aspx">https://technet.microsoft.com/en-us/library/ms189858.aspx</a></p>
<p><em>"<span style="color: black; font-family: Segoe UI;">You can remedy index fragmentation by reorganizing or rebuilding an index. For partitioned indexes built on a partition scheme, you can use either of these methods on a complete index or a single partition of an index. Rebuilding an index drops and re-creates the index. This removes fragmentation, reclaims disk space by compacting the pages based on the specified or existing fill factor setting, and reorders the index rows in contiguous pages. When ALL is specified, all indexes on the table are dropped and rebuilt in a single transaction. Reorganizing an index uses minimal system resources. It defragments the leaf level of clustered and nonclustered indexes on tables and views by physically reordering the leaf-level pages to match the logical, left to right, order of the leaf nodes. Reorganizing also compacts the index pages. Compaction is based on the existing fill factor value.</span>" </em></p>
<p>OK, now that we are all on the same page I'll start the demo.&nbsp; First, I'll create a database to use for the example.&nbsp; If it exists, drop it and then create it.</p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">USE</span> <span style="color: teal;">[master]</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">SET</span> <span style="color: blue;">NOCOUNT</span> <span style="color: blue;">ON</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">GO</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">IF</span> <span style="color: fuchsia;">DB_ID<span style="color: gray;">(<span style="color: red;">'RebuildReorg'<span style="color: gray;">)</span> <span style="color: gray;">IS</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL</span> <br /></span></span></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">BEGIN</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">ALTER</span> <span style="color: blue;">DATABASE</span> <span style="color: teal;">RebuildReorg</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">SET</span> <span style="color: blue;">SINGLE_USER</span> <span style="color: blue;">WITH</span> <span style="color: blue;">ROLLBACK</span> <span style="color: blue;">IMMEDIATE</span>&nbsp;</span>&nbsp;</p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">DROP</span> <span style="color: blue;">DATABASE</span> <span style="color: teal;">RebuildReorg</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">END</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">GO</span> </span></p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">CREATE</span> <span style="color: blue;">DATABASE</span> <span style="color: teal;">RebuildReorg</span> <span style="color: blue;">ON</span> <span style="color: blue;">PRIMARY</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: gray;">(</span> <span style="color: teal;">NAME</span> <span style="color: gray;">=</span> <span style="color: red;">N'RebuildReorg_Data'<span style="color: gray;">,</span> <span style="color: blue;">FILENAME</span> <span style="color: gray;">=</span> N'C:\SQL\RebuildReorg_Data.mdf'</span> <span style="color: gray;">,</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">SIZE</span> <span style="color: gray;">=</span> 2048<span style="color: teal;">MB</span> <span style="color: gray;">,</span> <span style="color: teal;">MAXSIZE</span> <span style="color: gray;">=</span> <span style="color: blue;">UNLIMITED<span style="color: gray;">,</span> <span style="color: teal;">FILEGROWTH</span> <span style="color: gray;">=</span> 50<span style="color: teal;">MB</span> <span style="color: gray;">)</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: fuchsia;">LOG</span> <span style="color: blue;">ON</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: gray;">(</span> <span style="color: teal;">NAME</span> <span style="color: gray;">=</span> <span style="color: red;">N'RebuildReorg_Log'<span style="color: gray;">,</span> <span style="color: blue;">FILENAME</span> <span style="color: gray;">=</span> N'C:\SQL\RebuildReorg_log.LDF'</span> <span style="color: gray;">,</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">SIZE</span> <span style="color: gray;">=</span> 2048<span style="color: teal;">MB</span> <span style="color: gray;">,</span> <span style="color: teal;">MAXSIZE</span> <span style="color: gray;">=</span> 5<span style="color: teal;">GB<span style="color: gray;">,</span> FILEGROWTH</span> <span style="color: gray;">=</span> 200<span style="color: teal;">MB<span style="color: gray;">)</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">GO</span> </span></p>
<p>Next I want to make sure I am in the full recovery model.&nbsp; If I happened to have the DB in the Simple recovery mode then the log would be truncated when a checkpoint occurs.&nbsp; I want the log records that I generate to stay in the log so I can see how much log is generated for a rebuild vs a reorganization.</p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">ALTER</span> <span style="color: blue;">DATABASE</span> <span style="color: teal;">RebuildReorg</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">SET</span> <span style="color: blue;">RECOVERY</span> <span style="color: blue;">FULL</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">GO</span> </span></p>
<p>Next I will create a table named Sales and pump some data into it.&nbsp; I've added a clustered index on a uniqueidentifier column on the table.&nbsp; This should leave the index in a very fragmented state once the data has been entered.&nbsp; This is useful because I want to simulate a "worst case" scenario so that the rebuild and reorganize routines have a lot of work to do.</p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">USE</span> <span style="color: teal;">RebuildReorg</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">GO</span> </span></p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">IF</span> <span style="color: fuchsia;">OBJECT_ID<span style="color: gray;">(<span style="color: red;">'dbo.Sales'<span style="color: gray;">)</span> <span style="color: gray;">IS</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL</span> <br /></span></span></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">DROP</span> <span style="color: blue;">TABLE</span> <span style="color: teal;">dbo<span style="color: gray;">.<span style="color: teal;">Sales</span> <br /></span></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">GO</span> </span></p>
<p>&nbsp;<span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">CREATE</span> <span style="color: blue;">TABLE</span> <span style="color: teal;">dbo<span style="color: gray;">.<span style="color: teal;">Sales</span> <br /></span></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: gray;">(</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">[SalesOrderID]</span> <span style="color: teal;">[int]</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL,</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">[SalesOrderDetailID]</span> <span style="color: teal;">[int]</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL,</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">[CarrierTrackingNumber]</span> <span style="color: teal;">[nvarchar]<span style="color: gray;">(</span>25<span style="color: gray;">)</span> <span style="color: gray;">NULL,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">[OrderQty]</span> <span style="color: teal;">[smallint]</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL,</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">[ProductID]</span> <span style="color: teal;">[int]</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL,</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">[SpecialOfferID]</span> <span style="color: teal;">[int]</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL,</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">[UnitPrice]</span> <span style="color: teal;">[money]</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL,</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">[UnitPriceDiscount]</span> <span style="color: teal;">[money]</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL,</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">[LineTotal]</span> <span style="color: blue;">AS <span style="color: gray;">(<span style="color: fuchsia;">isnull<span style="color: gray;">((<span style="color: teal;">[UnitPrice]<span style="color: gray;">*((</span>1.0<span style="color: gray;">)-<span style="color: teal;">[UnitPriceDiscount]<span style="color: gray;">))*<span style="color: teal;">[OrderQty]<span style="color: gray;">,(</span>0.0<span style="color: gray;">))),</span> <br /></span></span></span></span></span></span></span></span></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">[ModifiedDate]</span> <span style="color: teal;">[datetime]</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL,</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">GUIDCol</span> <span style="color: blue;">UNIQUEIDENTIFIER</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL</span> <span style="color: blue;">DEFAULT<span style="color: gray;">(<span style="color: fuchsia;">NEWID<span style="color: gray;">())</span> <br /></span></span></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: gray;">)</span> <span style="color: blue;">ON</span> <span style="color: teal;">[PRIMARY]</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">GO</span> </span></p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">CREATE</span> <span style="color: blue;">CLUSTERED</span> <span style="color: blue;">INDEX</span> <span style="color: teal;">cixSales</span> <span style="color: blue;">ON</span> <span style="color: teal;">dbo<span style="color: gray;">.<span style="color: teal;">Sales<span style="color: blue;"> <span style="color: gray;">(<span style="color: teal;">GUIDCol<span style="color: gray;">)</span> <br /></span></span></span></span></span></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">GO</span> </span></p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">INSERT</span> <span style="color: blue;">INTO</span> <span style="color: teal;">dbo<span style="color: gray;">.<span style="color: teal;">Sales</span> <br /></span></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: gray;">(</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">SalesOrderID<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">SalesOrderDetailID<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">CarrierTrackingNumber<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">OrderQty<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">ProductID<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">SpecialOfferID<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">UnitPrice<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">UnitPriceDiscount<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">ModifiedDate</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: gray;">)</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">SELECT</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">SalesOrderID<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">SalesOrderDetailID<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">CarrierTrackingNumber<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">OrderQty<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">ProductID<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">SpecialOfferID<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">UnitPrice<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">UnitPriceDiscount<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">ModifiedDate<br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">FROM</span> </span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">AdventureWorks2012<span style="color: gray;">.<span style="color: teal;">Sales<span style="color: gray;">.<span style="color: teal;">SalesOrderDetail</span> <br /></span></span></span></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">GO</span> 20 </span></p>
<p>I need to take a backup to start the recovery chain.&nbsp; I just created this database and while it is in the full recovery mode, the database will be treated as though it is in the simple recovery mode since I have not yet taken a full backup.&nbsp; In other words, the log will be truncated when a checkpoint occurs right now.&nbsp; As soon as I take a full backup, the log will only be truncated when a log backup occurs.&nbsp; In the full backup and log backups I am taking below, I am using the "NUL" backup device.&nbsp; I'm just using this because I don't really want to write the backup out to disk.&nbsp; I don't plan on using the backup in later operations.&nbsp; You really never want to use this in a production environment unless you are testing how long it takes to read the data from the data files for the backup or another special case which I won't go into here.</p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">BACKUP</span> <span style="color: blue;">DATABASE</span> <span style="color: teal;">RebuildReorg</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">TO</span> <span style="color: blue;">DISK</span> <span style="color: gray;">=</span> <span style="color: red;">'NUL'</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">GO</span> </span></p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">BACKUP</span> <span style="color: fuchsia;">LOG</span> <span style="color: teal;">RebuildReorg</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">TO</span> <span style="color: blue;">DISK</span> <span style="color: gray;">=</span> <span style="color: red;">'NUL'</span> <br /></span><span style="color: blue; font-family: Consolas; font-size: 12pt;">GO </span></p>
<p><br />Now rebuild the clustered index on the table.</p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">ALTER</span> <span style="color: blue;">INDEX</span> <span style="color: teal;">cixSales</span> <span style="color: blue;">ON</span> <span style="color: teal;">dbo<span style="color: gray;">.<span style="color: teal;">Sales</span> <br /></span></span></span><span style="color: blue; font-family: Consolas; font-size: 12pt;">REBUILD </span></p>
<p>I can use the system function sys.fn_dblog to find out how many log records were generated by the rebuild statement.&nbsp; Remember, I took a log backup before I performed the rebuild, so I was working with an "empty" transaction log when the rebuild occurred.</p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">SELECT</span> <span style="color: teal;">LogRecordsGenerated</span> <span style="color: gray;">=</span> <span style="color: fuchsia;">COUNT<span style="color: gray;">(*)</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">FROM</span> <span style="color: green;">sys<span style="color: gray;">.<span style="color: green;">fn_dblog<span style="color: gray;">(NULL,</span> <span style="color: gray;">NULL)</span> <br /></span></span></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">WHERE</span> <span style="color: teal;">AllocUnitName</span> <span style="color: gray;">=</span> <span style="color: red;">'dbo.Sales.cixSales' <br /></span></span></p>
<p>&nbsp;<a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/RebuildVReorg/6786.RebuildVReOrg1.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-RebuildVReorg/6786.RebuildVReOrg1.jpg"><img style="max-width: 550px;" alt="" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/RebuildVReorg/6786.RebuildVReOrg1.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-RebuildVReorg/6786.RebuildVReOrg1.jpg" border="0" /></a></p>
<p>So, it looks like rebuilding the index resulted in 35516 transaction log records.&nbsp; Cool.&nbsp; What kind of log generation will I be looking at if I would have performed a reorganize instead?&nbsp; To find out, I'll need to recreate the above scenario again.&nbsp;</p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">USE</span> <span style="color: teal;">RebuildReorg</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">GO</span> </span></p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">IF</span> <span style="color: fuchsia;">OBJECT_ID<span style="color: gray;">(<span style="color: red;">'dbo.Sales'<span style="color: gray;">)</span> <span style="color: gray;">IS</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL</span> <br /></span></span></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">DROP</span> <span style="color: blue;">TABLE</span> <span style="color: teal;">dbo<span style="color: gray;">.<span style="color: teal;">Sales</span> <br /></span></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">GO</span> </span></p>
<p>&nbsp;<span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">CREATE</span> <span style="color: blue;">TABLE</span> <span style="color: teal;">dbo<span style="color: gray;">.<span style="color: teal;">Sales</span> <br /></span></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: gray;">(</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">[SalesOrderID]</span> <span style="color: teal;">[int]</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL,</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">[SalesOrderDetailID]</span> <span style="color: teal;">[int]</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL,</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">[CarrierTrackingNumber]</span> <span style="color: teal;">[nvarchar]<span style="color: gray;">(</span>25<span style="color: gray;">)</span> <span style="color: gray;">NULL,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">[OrderQty]</span> <span style="color: teal;">[smallint]</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL,</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">[ProductID]</span> <span style="color: teal;">[int]</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL,</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">[SpecialOfferID]</span> <span style="color: teal;">[int]</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL,</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">[UnitPrice]</span> <span style="color: teal;">[money]</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL,</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">[UnitPriceDiscount]</span> <span style="color: teal;">[money]</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL,</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">[LineTotal]</span> <span style="color: blue;">AS <span style="color: gray;">(<span style="color: fuchsia;">isnull<span style="color: gray;">((<span style="color: teal;">[UnitPrice]<span style="color: gray;">*((</span>1.0<span style="color: gray;">)-<span style="color: teal;">[UnitPriceDiscount]<span style="color: gray;">))*<span style="color: teal;">[OrderQty]<span style="color: gray;">,(</span>0.0<span style="color: gray;">))),</span> <br /></span></span></span></span></span></span></span></span></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">[ModifiedDate]</span> <span style="color: teal;">[datetime]</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL,</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">GUIDCol</span> <span style="color: blue;">UNIQUEIDENTIFIER</span> <span style="color: gray;">NOT</span> <span style="color: gray;">NULL</span> <span style="color: blue;">DEFAULT<span style="color: gray;">(<span style="color: fuchsia;">NEWID<span style="color: gray;">())</span> <br /></span></span></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: gray;">)</span> <span style="color: blue;">ON</span> <span style="color: teal;">[PRIMARY]</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">GO</span> </span></p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">CREATE</span> <span style="color: blue;">CLUSTERED</span> <span style="color: blue;">INDEX</span> <span style="color: teal;">cixSales</span> <span style="color: blue;">ON</span> <span style="color: teal;">dbo<span style="color: gray;">.<span style="color: teal;">Sales<span style="color: blue;"> <span style="color: gray;">(<span style="color: teal;">GUIDCol<span style="color: gray;">)</span> <br /></span></span></span></span></span></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">GO</span> </span></p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">INSERT</span> <span style="color: blue;">INTO</span> <span style="color: teal;">dbo<span style="color: gray;">.<span style="color: teal;">Sales</span> <br /></span></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: gray;">(</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">SalesOrderID<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">SalesOrderDetailID<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">CarrierTrackingNumber<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">OrderQty<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">ProductID<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">SpecialOfferID<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">UnitPrice<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">UnitPriceDiscount<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">ModifiedDate</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: gray;">)</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">SELECT</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">SalesOrderID<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">SalesOrderDetailID<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">CarrierTrackingNumber<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">OrderQty<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">ProductID<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">SpecialOfferID<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">UnitPrice<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">UnitPriceDiscount<span style="color: gray;">,</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">ModifiedDate<br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">FROM</span> </span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: teal;">AdventureWorks2012<span style="color: gray;">.<span style="color: teal;">Sales<span style="color: gray;">.<span style="color: teal;">SalesOrderDetail</span> <br /></span></span></span></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">GO</span> 20 <br /></span><br />Make sure to backup the log again so that we can compare the two operations.</p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">BACKUP</span> <span style="color: fuchsia;">LOG</span> <span style="color: teal;">RebuildReorg</span> <br /></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">TO</span> <span style="color: blue;">DISK</span> <span style="color: gray;">=</span> <span style="color: red;">'NUL'</span> <br /></span><span style="color: blue; font-family: Consolas; font-size: 12pt;">GO </span></p>
<p><br />Now perform the reorganize.</p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">ALTER</span> <span style="color: blue;">INDEX</span> <span style="color: teal;">cixSales</span> <span style="color: blue;">ON</span> <span style="color: teal;">dbo<span style="color: gray;">.<span style="color: teal;">Sales<br /><span style="color: blue;">REORGANIZE</span> </span> </span></span></span></p>
<p>And run the query against the transaction log to see how much log was generated by the reorganize operation.</p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">SELECT</span> <span style="color: teal;">LogRecordsGenerated</span> <span style="color: gray;">=</span> <span style="color: fuchsia;">COUNT<span style="color: gray;">(*)</span> <br /></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">FROM</span> <span style="color: green;">sys<span style="color: gray;">.<span style="color: green;">fn_dblog<span style="color: gray;">(NULL,</span> <span style="color: gray;">NULL)</span> <br /></span></span></span></span><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">WHERE</span> <span style="color: teal;">AllocUnitName</span> <span style="color: gray;">=</span> <span style="color: red;">'dbo.Sales.cixSales' <br /></span></span></p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/RebuildVReorg/1586.RebuildVReOrg2.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-RebuildVReorg/1586.RebuildVReOrg2.jpg"><img style="max-width: 550px;" alt="" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/RebuildVReorg/1586.RebuildVReOrg2.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-RebuildVReorg/1586.RebuildVReOrg2.jpg" border="0" /></a></p>
<p>As you can see, from a log generation perspective the reorganize statement created a SIGNIFICANTLY (35516 vs 2.7M) higher number of transaction log records as compared to the rebuild.&nbsp; This is by design.&nbsp; For large, heavily fragmented indexes the reorganization operation is much less efficient than the index rebuild operation.&nbsp; This is important to know because if you're using a technology that relies on reading from the transaction log, such as mirroring, transactional replication/CDC then performing a reorg is going to generate a lot more activity that needs to be sorted through.&nbsp; Of course, a reorg does have its benefits, such as being a progressive operation.&nbsp; A reorg can be stopped while it is occurring and you won't lose the work it has already completed, as compared to a rebuild which is an atomic operation.&nbsp; Like everything in technology, there are benefits and drawbacks to both operations.&nbsp;</p>
<p>Hope this helps!</p>
<p>Tim</p>]]></content:encoded>
    </item>
    <item>
      <title>Optimizing ad-hoc workloads</title>
      <link>https://docs.microsoft.com/archive/blogs/timchapman/optimizing-ad-hoc-workloads</link>
      <pubDate>Mon, 10 Sep 2012 08:04:00 GMT</pubDate>
      <dc:creator><![CDATA[TimChapman]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/timchapman/2012/09/10/optimizing-ad-hoc-workloads/</guid>
      <description><![CDATA[Today's tip isn't necessarily low-level, but it is a setting that you should consider turning on for...]]></description>
      <content:encoded><![CDATA[<p>Today's tip isn't necessarily low-level, but it is a setting that you should consider turning on for your SQL Server instances (especially if you've got vendor applications that generate TSQL statements on the fly).&nbsp; The setting,&nbsp; "optimize for ad-hoc workloads", is configured using sp_configure and when enabled will generate a very small execution plan stub when a statement is first compiled.&nbsp; This plan stub, which is many times smaller than what the execution plan would normally be, ensures that the plan cache doesn't get very large from dynamically executed SQL that isn't likely to be resused.&nbsp; Any memory allocated for execution plans comes from the buffer pool, so the more plans you have then the smaller your buffer pool will be for data and index pages.&nbsp; If the same plan is reused, the plan stub goes away and a full plan stays in the plan cache.&nbsp;</p>
<p>Let's take a look at how significant the memory consumption savings can be when using this option.&nbsp; First, let's clear out the plan cache (remember to never do this on a production system).</p>
<p><span style="font-size: 12pt;"><span style="color: blue; font-family: Consolas;">DBCC</span><span style="font-family: Consolas;"> <span style="color: teal;">FREEPROCCACHE </span></span><span style="font-family: Times New Roman;"> </span></span></p>
<p>Now, let's look at what is returned for our cached plans that reference the table SalesOrderHeader (which is in the AdventureWorks database).&nbsp; This should return NULL since we just cleared out the plan cache.</p>
<p><span style="font-size: 12pt;"><span style="color: blue; font-family: Consolas;">SELECT</span><span style="font-family: Consolas;"> <span style="color: teal;">PlanSizeInMB</span> <span style="color: gray;">=</span> <span style="color: fuchsia;">SUM<span style="color: gray;">(<span style="color: teal;">size_in_bytes<span style="color: gray;">)/<span style="color: teal;">1024.0<span style="color: gray;">/<span style="color: teal;">1024.0 <br /><span style="color: blue;">FROM</span> <span style="color: green;">sys<span style="color: gray;">.<span style="color: green;">dm_exec_cached_plans<span style="color: gray;"> <br />CROSS</span> <span style="color: gray;">APPLY</span> sys<span style="color: gray;">.<span style="color: green;">dm_exec_sql_text<span style="color: gray;">(<span style="color: teal;">plan_handle<span style="color: gray;">)<span style="color: teal;"> <br /><span style="color: blue;">WHERE</span> <span style="color: blue;">text</span> <span style="color: gray;">LIKE</span> <span style="color: red;">'%SalesOrderHeader%'</span> <span style="color: gray;">AND</span> </span><br /><span style="color: blue;">text</span> NOT</span> <span style="color: gray;">LIKE</span> <span style="color: red;">'%dm_exec_cached_plans%' </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="font-family: Times New Roman;"> </span></span></p>
<p>I also want to make sure the "optimize for ad hoc workloads" option is turned off.<span style="color: red; font-family: Courier New; font-size: 15pt;"> </span><span style="font-family: Times New Roman; font-size: 12pt;"> </span></p>
<p><span style="font-size: 12pt;"><span style="color: blue; font-family: Consolas;">EXECUTE</span><span style="font-family: Consolas;"> <span style="color: maroon;">sp_configure<span style="color: blue;"> <span style="color: red;">'optimize for ad hoc workloads'<span style="color: gray;">,<span style="color: red;"> 0 <br /><span style="color: blue;">RECONFIGURE </span></span></span></span></span></span></span><span style="font-family: Times New Roman;"> </span></span></p>
<p>Now, let's run a load that is similar to an application that builds SQL strings on the fly and executes the statements in a non-parameterized fashion.&nbsp; This is a contrived example, but illustrates what can happen.&nbsp; I'm simply looping through the Sales.SalesOrderHeader table a record at a time and issuing a SELECT statement for each record encountered with an explicit value for the predicate.&nbsp; This will create an entry in the plan cache for every single SELECT statement.<span style="font-family: Times New Roman; font-size: 12pt;"> </span></p>
<p><span style="font-size: 12pt;"><span style="color: blue; font-family: Consolas;">DECLARE</span><span style="font-family: Consolas;"> <span style="color: teal;">@SalesOrderID</span> <span style="color: blue;">VARCHAR<span style="color: gray;">(<span style="color: blue;">20<span style="color: gray;">)<span style="color: blue;"> <br />DECLARE</span> <span style="color: teal;">@SQL</span> <span style="color: blue;">NVARCHAR<span style="color: gray;">(<span style="color: fuchsia;">MAX<span style="color: gray;">)<span style="color: fuchsia;"> </span></span></span></span></span></span></span></span></span></span><span style="font-family: Times New Roman;"> </span></span></p>
<p><span style="font-size: 12pt;"><span style="color: blue; font-family: Consolas;">DECLARE</span><span style="font-family: Consolas;"> <span style="color: teal;">@TableVar</span> <span style="color: blue;">TABLE <span style="color: gray;">(<span style="color: teal;">SalesOrderID<span style="color: gray;"> <span style="color: blue;">INT<span style="color: gray;">)<span style="color: blue;">&nbsp;</span></span></span></span></span></span></span></span><span style="font-family: Times New Roman;">&nbsp; </span></span></p>
<p><span style="font-size: 12pt;"><span style="color: blue; font-family: Consolas;">DECLARE</span><span style="font-family: Consolas;"> <span style="color: teal;">CursorExample</span> <span style="color: blue;">CURSOR</span> <span style="color: blue;">FOR</span> <br /><span style="color: blue;">SELECT</span> <span style="color: teal;">SalesOrderID</span> <br /><span style="color: blue;">FROM</span> <span style="color: teal;">Sales<span style="color: gray;">.<span style="color: teal;">SalesOrderHeader<span style="color: gray;"> </span></span></span></span></span><span style="font-family: Times New Roman;"> </span></span></p>
<p><span style="font-size: 12pt;"><span style="color: blue; font-family: Consolas;">OPEN</span><span style="font-family: Consolas;"> <span style="color: teal;">CursorExample</span> <br /><span style="color: blue;">FETCH</span> <span style="color: blue;">NEXT</span> <span style="color: blue;">FROM</span> <span style="color: teal;">CursorExample</span> <span style="color: blue;">INTO</span> <span style="color: teal;">@SalesOrderID</span> </span><span style="font-family: Times New Roman;"> </span></span></p>
<p><span style="font-size: 12pt;"><span style="color: blue; font-family: Consolas;">WHILE</span><span style="font-family: Consolas;"> <span style="color: fuchsia;">@@FETCH_STATUS</span> <span style="color: gray;">=</span> 0 <br /><span style="color: blue;">BEGIN</span> </span><span style="font-family: Times New Roman;"> </span></span></p>
<p><span style="font-size: 12pt;"><span style="color: blue; font-family: Consolas;">SET</span><span style="font-family: Consolas;"> <span style="color: teal;">@SQL</span> <span style="color: gray;">=</span> <span style="color: red;">'SELECT SalesOrderID</span> <br /><span style="color: red;">FROM Sales.SalesOrderHeader h</span> <br /><span style="color: red;">JOIN Sales.SalesPerson p on p.BusinessEntityID = h.SalesPersonID</span> <br /><span style="color: red;">WHERE SalesOrderID = '</span> <span style="color: gray;">+</span> <span style="color: teal;">@SalesOrderID</span> </span><span style="font-family: Times New Roman;"> </span></span></p>
<p><span style="font-size: 12pt;"><span style="color: blue; font-family: Consolas;">INSERT</span><span style="font-family: Consolas;"> <span style="color: blue;">INTO</span> <span style="color: teal;">@TableVar<span style="color: gray;">(<span style="color: teal;">SalesOrderID<span style="color: gray;">)<span style="color: teal;"> <br /><span style="color: blue;">EXECUTE <span style="color: gray;">(<span style="color: teal;">@SQL<span style="color: gray;">)<span style="color: teal;"> </span></span></span></span></span></span></span></span></span></span></span><span style="font-family: Times New Roman;"> </span></span></p>
<p><span style="font-size: 12pt;"><span style="color: blue; font-family: Consolas;">FETCH</span><span style="font-family: Consolas;"> <span style="color: blue;">NEXT</span> <span style="color: blue;">FROM</span> <span style="color: teal;">CursorExample</span> <span style="color: blue;">INTO</span> <span style="color: teal;">@SalesOrderID</span> <br /><span style="color: blue;">END</span> </span><span style="font-family: Times New Roman;"> </span></span></p>
<p><span style="font-size: 12pt;"><span style="color: blue; font-family: Consolas;">CLOSE</span><span style="font-family: Consolas;"> <span style="color: teal;">CursorExample</span> <br /><span style="color: blue;">DEALLOCATE</span> <span style="color: teal;">CursorExample </span></span><span style="font-family: Times New Roman;"> </span></span></p>
<p>When the "optimize for ad hoc workloads" option is not on, a full plan is created for every single SELECT statement.&nbsp; In this case, around 1259 MB of plan cache was used to store these simple plans.&nbsp; That is just over 1.2GB of plan cache used!&nbsp;</p>
<p><span style="font-size: 12pt;"><span style="color: blue; font-family: Consolas;">SELECT</span><span style="font-family: Consolas;"> <span style="color: teal;">PlanSizeInMB</span> <span style="color: gray;">=</span> <span style="color: fuchsia;">SUM<span style="color: gray;">(<span style="color: teal;">size_in_bytes<span style="color: gray;">)/<span style="color: teal;">1024.0<span style="color: gray;">/<span style="color: teal;">1024.0 <br /><span style="color: blue;">FROM</span> <span style="color: green;">sys<span style="color: gray;">.<span style="color: green;">dm_exec_cached_plans<span style="color: gray;"> <br />CROSS</span> <span style="color: gray;">APPLY</span> sys<span style="color: gray;">.<span style="color: green;">dm_exec_sql_text<span style="color: gray;">(<span style="color: teal;">plan_handle<span style="color: gray;">)<span style="color: teal;"> <br /><span style="color: blue;">WHERE</span> <span style="color: blue;">text</span> <span style="color: gray;">LIKE</span> <span style="color: red;">'%SalesOrderHeader%'</span> <span style="color: gray;">AND</span> </span><br /><span style="color: blue;">text</span> NOT</span> <span style="color: gray;">LIKE</span> <span style="color: red;">'%dm_exec_cached_plans%' </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="font-family: Times New Roman;"> </span></span></p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/AdHoc/5153.AdHoc1.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-AdHoc/5153.AdHoc1.jpg"><img style="max-width: 550px;" alt="" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/AdHoc/5153.AdHoc1.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-AdHoc/5153.AdHoc1.jpg" border="0" /></a>&nbsp; &nbsp;</p>
<p>So, what happens if I enable the 'optimize for ad hoc workloads' setting and rerun the test?&nbsp; First, I'll want to enable the setting and then clear the procedure cache again.</p>
<p><span style="font-size: 12pt;"><span style="color: blue; font-family: Consolas;">EXECUTE</span><span style="font-family: Consolas;"> <span style="color: maroon;">sp_configure<span style="color: blue;"> <span style="color: red;">'optimize for ad hoc workloads'<span style="color: gray;">,<span style="color: red;"> 1 <br /><span style="color: blue;">RECONFIGURE</span> </span><br /></span><br /><span style="color: blue;">DBCC</span> <span style="color: teal;">FREEPROCCACHE </span></span></span></span></span><span style="font-family: Times New Roman;"> </span></span></p>
<p>Rerun the same cursor code from earlier in this email.&nbsp; After you do, run the following statement to see the size of the plan cache:</p>
<p><span style="font-size: 12pt;"><span style="color: blue; font-family: Consolas;">SELECT</span><span style="font-family: Consolas;"> <span style="color: teal;">PlanSizeInMB</span> <span style="color: gray;">=</span> <span style="color: fuchsia;">SUM<span style="color: gray;">(<span style="color: teal;">size_in_bytes<span style="color: gray;">)/<span style="color: teal;">1024.0<span style="color: gray;">/<span style="color: teal;">1024.0 <br /><span style="color: blue;">FROM</span> <span style="color: green;">sys<span style="color: gray;">.<span style="color: green;">dm_exec_cached_plans<span style="color: gray;"> <br />CROSS</span> <span style="color: gray;">APPLY</span> sys<span style="color: gray;">.<span style="color: green;">dm_exec_sql_text<span style="color: gray;">(<span style="color: teal;">plan_handle<span style="color: gray;">)<span style="color: teal;"> <br /><span style="color: blue;">WHERE</span> <span style="color: blue;">text</span> <span style="color: gray;">LIKE</span> <span style="color: red;">'%SalesOrderHeader%'</span> <span style="color: gray;">AND</span> </span><br /><span style="color: blue;">text</span> NOT</span> <span style="color: gray;">LIKE</span> <span style="color: red;">'%dm_exec_cached_plans%'</span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="font-family: Times New Roman;"> </span></span></p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/AdHoc/0435.AdHoc2.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-AdHoc/0435.AdHoc2.jpg"><img style="max-width: 550px;" alt="" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/AdHoc/0435.AdHoc2.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-AdHoc/0435.AdHoc2.jpg" border="0" /></a>&nbsp; &nbsp;</p>
<p>Using the 'optimize for ad hoc workloads' option results in a plan cache that is right at 10MB (compared to a bloated cache of 1.2GB).&nbsp; If these plans are never used again, that is an incredible amount of memory you're saving for the buffer pool (and for other plans).&nbsp; The good thing is, there is really no reason to not have this setting on &ndash; the downside is negligible.&nbsp;</p>
<p>HTH!</p>
<p>Tim</p>]]></content:encoded>
    </item>
    <item>
      <title>The Joys of Filtered Indexes</title>
      <link>https://docs.microsoft.com/archive/blogs/timchapman/the-joys-of-filtered-indexes</link>
      <pubDate>Mon, 27 Aug 2012 13:30:00 GMT</pubDate>
      <dc:creator><![CDATA[TimChapman]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/timchapman/2012/08/27/the-joys-of-filtered-indexes/</guid>
      <description><![CDATA[A filtered index, introduced in SQL Server 2008, is a non-clustered index that contains only a...]]></description>
      <content:encoded><![CDATA[<p>A filtered index, introduced in SQL Server 2008, is a non-clustered index that contains only a subset of the number of rows contained in a table.&nbsp; Because there is a 1:1 ratio between the number of rows in a table and the number of rows in a regular non-clustered index, a filtered index can have performance and storage advantages.&nbsp; These indexes are typically much smaller than the full-table non clustered indexes.&nbsp; The statistics on these filtered indexes are filtered as well, which typically results in them being more accurate.&nbsp;</p>
<p>Filtered indexes are well-suited for scenarios where you frequently query a well-defined subset of data.&nbsp; This often includes querying ranges of specific values such as dates or monetary ranges, along with scenarios where you frequently have to filter out NULL values &ndash; such as Sparse columns or minimally updated columns.&nbsp; Another scenario where filtered indexes work really nicely is when you're dealing with heterogeneous data, such as frequent queries on highly correlated columns in a table for certain subsets of data.&nbsp; &nbsp;In this case, because of the frequency that you deal with the relationship between columns for a given data set, it may make sense to create a specific filtered index to meet your query needs rather than using a non-clustered index that contains the same number of rows as the base table.</p>
<p>Because the filtered index only needs to be maintained when there are rows inserted/updated/deleted that meet the index and filter definition, the overhead of maintaining the filtered index can be significantly less.&nbsp; There are also many cases where the Query Optimizer is more likely to choose a lower-cost plan based on the presence of the filtered index.</p>
<p>Let's take a look at a filtered index in action.&nbsp; First, I'll create a new table named Sales.NewSales, which is a copy of the AdventureWorks table Sales.SalesOrderDetail.</p>
<p><span style="font-size: 12pt;"><span style="color: blue; font-family: Consolas;">USE</span><span style="font-family: Consolas;"> <span style="color: teal;">AdventureWorks</span> </span><span style="font-family: Times New Roman;"> </span></span></p>
<p><span style="font-size: 12pt;"><span style="color: blue; font-family: Consolas;">GO</span><span style="font-family: Consolas;"> </span><span style="font-family: Times New Roman;"> </span></span></p>
<p><span style="font-size: 12pt;"><span style="color: blue; font-family: Consolas;">IF</span><span style="font-family: Consolas;"> <span style="color: fuchsia;">OBJECT_ID<span style="color: gray;">(<span style="color: red;">'Sales.NewSales'<span style="color: gray;">)<span style="color: red;"> <span style="color: gray;">IS<span style="color: red;"> <span style="color: gray;">NOT<span style="color: red;"> <span style="color: gray;">NULL<br /><span style="color: blue;">DROP</span> <span style="color: blue;">TABLE</span> <span style="color: teal;">Sales<span style="color: gray;">.<span style="color: teal;">NewSales<span style="color: gray;"> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span><span style="font-family: Times New Roman;"><br /></span><span style="color: blue; font-family: Consolas;">GO</span><span style="font-family: Consolas;"> </span><span style="font-family: Times New Roman;"> </span></span></p>
<p><span style="font-size: 12pt;"><span style="color: blue; font-family: Consolas;">SELECT</span><span style="font-family: Consolas;"> <span style="color: gray;">*</span> </span><span style="font-family: Times New Roman;"><br /></span><span style="color: blue; font-family: Consolas;">INTO</span><span style="font-family: Consolas;"> <span style="color: teal;">Sales<span style="color: gray;">.<span style="color: teal;">NewSales<span style="color: gray;"> </span></span></span></span></span><span style="font-family: Times New Roman;"><br /></span><span style="color: blue; font-family: Consolas;">FROM</span><span style="font-family: Consolas;"> <span style="color: teal;">Sales<span style="color: gray;">.<span style="color: teal;">SalesOrderDetail<span style="color: gray;"> </span></span></span></span></span><span style="font-family: Times New Roman;"><br /></span><span style="font-family: Consolas;"><span style="color: blue;">GO</span> </span></span></p>
<p>Create a new column in the Sales.NewsSales table named NewUnitPrice.</p>
<p><span style="font-size: 12pt;"><span style="color: blue; font-family: Consolas;">ALTER</span><span style="font-family: Consolas;"> <span style="color: blue;">TABLE</span> <span style="color: teal;">Sales<span style="color: gray;">.<span style="color: teal;">NewSales<span style="color: gray;"> <br /><span style="color: blue;">ADD</span> <span style="color: teal;">NewUnitPrice</span> <span style="color: blue;">MONEY</span> NULL</span> </span></span></span></span><span style="font-family: Times New Roman;"><br /></span><span style="font-family: Consolas;"><span style="color: blue;">GO</span> </span></span></p>
<p>Update every 10th record in Sales.NewSales to have a non-NULL value.</p>
<p><span style="font-size: 12pt;"><span style="color: fuchsia; font-family: Consolas;">UPDATE</span><span style="font-family: Consolas;"> <span style="color: teal;">Sales<span style="color: gray;">.<span style="color: teal;">NewSales<span style="color: gray;"> </span></span></span></span></span><span style="font-family: Times New Roman;"><br /></span><span style="color: blue; font-family: Consolas;">SET</span><span style="font-family: Consolas;"> <span style="color: teal;">NewUnitPrice</span> <span style="color: gray;">=</span> <br /><span style="color: blue;">CASE</span> </span><span style="font-family: Times New Roman;"><br /></span><span style="font-family: Consolas;"><span style="color: blue;">&nbsp;&nbsp;&nbsp; WHEN</span> <span style="color: teal;">SalesOrderDetailID</span> <span style="color: gray;">%</span> 10 <span style="color: gray;">=</span> 1 <br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: blue;">THEN</span> <span style="color: teal;">UnitPrice</span> <br />&nbsp;&nbsp;&nbsp; <span style="color: blue;">ELSE</span> <span style="color: gray;">NULL</span> <br /><span style="color: blue;">END</span> </span></span></p>
<p>Create a filtered index on the NewUnitPrice column to include any value that is NOT NULL.</p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">CREATE</span> <span style="color: blue;">NONCLUSTERED</span> <span style="color: blue;">INDEX</span> <span style="color: teal;">NewSalesIDX</span> <br /><span style="color: blue;">ON</span> <span style="color: teal;">Sales<span style="color: gray;">.<span style="color: teal;">NewSales<span style="color: gray;">(<span style="color: teal;">NewUnitPrice<span style="color: gray;">)<span style="color: teal;"> <br /><span style="color: blue;">WHERE<span style="color: gray;">(<span style="color: teal;">NewUnitPrice<span style="color: gray;"> IS<span style="color: blue;"> <span style="color: gray;">NOT<span style="color: blue;"> <span style="color: gray;">NULL)<span style="color: blue;"> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p>
<p>Create an index on the UnitPrice field for comparison purposes.</p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">CREATE</span> <span style="color: blue;">NONCLUSTERED</span> <span style="color: blue;">INDEX</span> <span style="color: teal;">UnitPriceIDX</span> <br /><span style="color: blue;">ON</span> <span style="color: teal;">Sales<span style="color: gray;">.<span style="color: teal;">NewSales<span style="color: gray;">(<span style="color: teal;">UnitPrice<span style="color: gray;">)<span style="color: teal;"> </span></span></span></span></span></span></span></span></p>
<p>Now, let's compare the number of pages between the two indexes.</p>
<p><span style="font-family: Consolas; font-size: 12pt;"><span style="color: blue;">SELECT</span> <br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: teal;">i<span style="color: gray;">.<span style="color: teal;">name<span style="color: gray;">,<span style="color: teal;"> d<span style="color: gray;">.<span style="color: teal;">index_type_desc<span style="color: gray;">,<span style="color: teal;"> <br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: teal;">d<span style="color: gray;">.<span style="color: teal;">index_depth<span style="color: gray;">,<span style="color: teal;"> d<span style="color: gray;">.<span style="color: teal;">avg_fragmentation_in_percent<span style="color: gray;">,<span style="color: teal;"> <br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: teal;">d<span style="color: gray;">.<span style="color: teal;">page_count<span style="color: gray;">,<span style="color: teal;"> <br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: teal;">IndexSizeMB</span> =<span style="color: blue;"> <span style="color: gray;">(<span style="color: teal;">page_count<span style="color: gray;"> *<span style="color: blue;"> 8192<span style="color: gray;">)/<span style="color: blue;">1024.0<span style="color: gray;">/<span style="color: blue;">1024.0 <br />FROM</span> <span style="color: green;">sys<span style="color: gray;">.<span style="color: green;">dm_db_index_physical_stats<span style="color: gray;"> <br /><span style="color: blue;">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: gray;">(<span style="color: blue;"> <span style="color: fuchsia;">DB_ID<span style="color: gray;">(),<span style="color: fuchsia;">&nbsp;<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: fuchsia;">OBJECT_ID<span style="color: gray;">(<span style="color: red;">'Sales.NewSales'<span style="color: gray;">),<span style="color: red;">&nbsp;<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NULL,NULL,</span> <span style="color: gray;">NULL</span> </span><br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: gray;">)</span> <span style="color: teal;">d</span> </span><br />JOIN</span> <span style="color: green;">sys<span style="color: gray;">.<span style="color: green;">indexes<span style="color: gray;"> <span style="color: teal;">i<span style="color: gray;"> <span style="color: blue;">ON<span style="color: gray;"> <br /><span style="color: teal;">d<span style="color: gray;">.<span style="color: teal;">index_id<span style="color: gray;"> =<span style="color: teal;"> i<span style="color: gray;">.<span style="color: teal;">index_id<span style="color: gray;"> AND<span style="color: teal;"> <br />d<span style="color: gray;">.<span style="color: fuchsia;">object_id<span style="color: gray;"> =<span style="color: teal;"> i<span style="color: gray;">.<span style="color: fuchsia;">object_id<span style="color: gray;"> <br /><span style="color: blue;">WHERE</span> </span><br /><span style="color: teal;">i<span style="color: gray;">.<span style="color: teal;">index_id<span style="color: gray;"> &gt;<span style="color: teal;"> 1 </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p>
<p>From the screenshot below, you can see that the filtered index has many fewer pages &ndash; which also means that it takes less disk space.</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/JoysOfFilteredIndexes/1106.JoysOfFilteredIndexes1.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-JoysOfFilteredIndexes/1106.JoysOfFilteredIndexes1.jpg"><img style="max-width: 550px;" alt="" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/JoysOfFilteredIndexes/1106.JoysOfFilteredIndexes1.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-JoysOfFilteredIndexes/1106.JoysOfFilteredIndexes1.jpg" border="0" /></a></p>
<p>So, how does performance compare between the two indexes?&nbsp; Running the following query you can see that the filtered index out performs the regular non-clustered index by a significant margin.<span style="font-family: Times New Roman; font-size: 12pt;"> </span></p>
<p><span style="font-size: 12pt;"><span style="color: blue; font-family: Consolas;">SELECT</span><span style="font-family: Consolas;"> <span style="color: teal;">NewUnitPrice</span> <br /><span style="color: blue;">FROM</span> <span style="color: teal;">Sales<span style="color: gray;">.<span style="color: teal;">NewSales<span style="color: gray;"> <br /><span style="color: blue;">WHERE</span> <span style="color: teal;">NewUnitPrice</span> =</span> 49.99 </span></span></span></span><span style="font-family: Times New Roman;"> </span></span></p>
<p><span style="font-size: 12pt;"><span style="color: blue; font-family: Consolas;">SELECT</span><span style="font-family: Consolas;"> <span style="color: teal;">UnitPrice</span> <br /><span style="color: blue;">FROM</span> <span style="color: teal;">Sales<span style="color: gray;">.<span style="color: teal;">NewSales<span style="color: gray;"> <br /><span style="color: blue;">WHERE</span> <span style="color: teal;">UnitPrice</span> =</span> 49.99</span> </span></span></span><span style="font-family: Times New Roman;"> </span></span></p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/JoysOfFilteredIndexes/6545.JoysOfFilteredIndexes2.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-JoysOfFilteredIndexes/6545.JoysOfFilteredIndexes2.jpg"><img style="max-width: 550px;" alt="" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/JoysOfFilteredIndexes/6545.JoysOfFilteredIndexes2.jpg" original-url="https://blogs.msdn.com/cfs-filesystemfile.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-JoysOfFilteredIndexes/6545.JoysOfFilteredIndexes2.jpg" border="0" /></a></p>
<p>For more information on the downside of filtered indexes, check out my post here:&nbsp; <a href="https://blogs.msdn.com/b/timchapman/archive/2012/08/27/the-drawback-of-using-filtered-indexes.aspx">https://blogs.msdn.com/b/timchapman/archive/2012/08/27/the-drawback-of-using-filtered-indexes.aspx</a></p>
<p>&nbsp;</p>
<p>HTH,</p>
<p>Tim</p>
<p><br />&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>The Pains of Filtered Indexes</title>
      <link>https://docs.microsoft.com/archive/blogs/timchapman/the-pains-of-filtered-indexes</link>
      <pubDate>Mon, 27 Aug 2012 08:50:00 GMT</pubDate>
      <dc:creator><![CDATA[TimChapman]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/timchapman/2012/08/27/the-pains-of-filtered-indexes/</guid>
      <description><![CDATA[In a previous post I briefly went over how useful filtered indexes can be.&nbsp; Filtered indexes...]]></description>
      <content:encoded><![CDATA[<p><span style="color: black;">In a <a href="https://blogs.msdn.com/b/timchapman/archive/2012/08/27/the-joys-of-filtered-indexes.aspx">previous post</a> I briefly went over how useful filtered indexes can be.&nbsp; Filtered indexes are a great feature that have numerous uses.&nbsp; However, like almost everything in computing, there are trade-offs, and filtered indexes are no exception.&nbsp; The challenge with filtered indexes is the decisions that the query optimizer can make regarding them.&nbsp; In some cases, the indexes will be used as you would expect.&nbsp; There are cases, unfortunately, where decisions can't be made as to if the filtered index can be used which ultimately leads to it not being considered. </span></p>
<p><span style="color: black;">Let's look at an example of what I am talking about.&nbsp; First, I'll create a filtered index to play around with. </span></p>
<p><span style="font-size: 11pt;"><span style="color: blue; font-family: Consolas;">CREATE</span><span style="font-family: Consolas;"> <span style="color: blue;">INDEX</span> <span style="color: teal;">FIDX_SalesOrderDetail_ProductID</span> </span><span style="font-family: Times New Roman;"><br /></span><span style="color: blue; font-family: Consolas;">ON</span><span style="font-family: Consolas;"> <span style="color: teal;">Sales<span style="color: gray;">.<span style="color: teal;">SalesOrderDetail<span style="color: blue;"> <span style="color: gray;">(<span style="color: teal;">ProductID<span style="color: gray;">)</span></span></span></span></span></span></span></span><span style="font-family: Times New Roman;"><br /></span><span style="color: blue; font-family: Consolas;">WHERE</span><span style="font-family: Consolas;"> <span style="color: teal;">ProductID</span> <span style="color: gray;">=</span> 870 </span></span></p>
<p><span style="color: black;">Run a quick query against the SalesOrderDetail table for ProductID 870. As you may expect, this should result in a SEEK of our newly created filtered index. </span></p>
<p><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/metablogapi/0537.082712_1150_ThePainsofF1.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-metablogapi/0537.082712_5F00_1150_5F00_ThePainsofF1.png" alt="" align="left" /></p>
<p><span style="color: black;">Great &ndash; that is exactly what we want. The new filtered index was used in the previous query. One thing to notice in the previous query is that I used an explicit value 870 in the predicate. SQL Server didn't have to make any guesses as to what value was being sought. However, what happens if I use a local variable? </span></p>
<p><span style="font-family: Consolas;"><span style="color: blue;">DECLARE</span> <span style="color: teal;">@ProductID</span> <span style="color: blue;">INT</span> <br /></span><span style="font-family: Consolas;"><span style="color: blue;">SET</span> <span style="color: teal;">@ProductID</span> <span style="color: gray;">=</span> 870&nbsp;</span>&nbsp;</p>
<p><span style="font-family: Consolas;"><span style="color: blue;">SELECT</span> <span style="color: teal;">ProductID</span> <br /></span><span style="font-family: Consolas;"><span style="color: blue;">FROM</span> <span style="color: teal;">Sales<span style="color: gray;">.<span style="color: teal;">SalesOrderDetail</span> <br /></span></span></span><span style="font-family: Consolas;"><span style="color: blue;">WHERE</span> <span style="color: teal;">ProductID</span> <span style="color: gray;">=</span> <span style="color: teal;">@ProductID</span> </span></p>
<p>&nbsp;<span style="color: black;"><br />In this case, SQL Server did NOT use the filtered index &ndash; even though the query was searching for the value 870. The reason for this is the same reason you want to try to avoid using local variables in predicates. The query optimizer did not what value was held in the local variable at compile time, so it had to make some assumptions based on the statistics it contained for the column being searched. Because SQL Server didn't know what value was in the variable, a filtered index cannot be considered.<img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/metablogapi/2678.082712_1150_ThePainsofF2.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-metablogapi/2678.082712_5F00_1150_5F00_ThePainsofF2.png" alt="" align="left" /> </span></p>
<p>&nbsp;</p>
<p><span style="color: black;">In many cases, a way to get around the local variable problem is to recompile the statement: </span></p>
<p><span style="font-family: Consolas;"><span style="color: blue;">DECLARE</span> <span style="color: teal;">@ProductID</span> <span style="color: blue;">INT<br /></span></span><span style="font-family: Consolas;"><span style="color: blue;">SET</span> <span style="color: teal;">@ProductID</span> <span style="color: gray;">=</span> 870&nbsp;</span>&nbsp;</p>
<p><span style="font-family: Consolas;"><span style="color: blue;">SELECT</span> <span style="color: teal;">ProductID</span> <br /></span><span style="font-family: Consolas;"><span style="color: blue;">FROM</span> <span style="color: teal;">Sales<span style="color: gray;">.<span style="color: teal;">SalesOrderDetail</span> <br /></span></span></span><span style="font-family: Consolas;"><span style="color: blue;">WHERE</span> <span style="color: teal;">ProductID</span> <span style="color: gray;">=</span> <span style="color: teal;">@ProductID</span> <br /></span><span style="color: blue; font-family: Consolas;">OPTION<span style="color: gray;">(<span style="color: blue;">RECOMPILE<span style="color: gray;">) </span></span></span></span></p>
<p><span style="color: black;">Unfortunately, in the case of the filtered index it does not work: <br /><span style="color: #ff0000;">Note:&nbsp; The fix for this behavior was fixed in SQL Server 2008 R2 SP1.&nbsp; Now an OPTION(RECOMPILE) <em>will </em>the actual local variable (or parameter) value.</span></span></p>
<p><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/metablogapi/2262.082712_1150_ThePainsofF3.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-metablogapi/2262.082712_5F00_1150_5F00_ThePainsofF3.png" alt="" align="left" /></p>
<p>&nbsp;</p>
<p><span style="color: black;">OK, at this point we know that using local variables presents a challenge. So, what about using parameterized SQL statements? Here I'll create a parameterized SQL statement using sp_executesql. </span></p>
<p><span style="font-family: Consolas;"><span style="color: blue;">DECLARE</span> <span style="color: teal;">@SQL</span> <span style="color: blue;">NVARCHAR<span style="color: gray;">(<span style="color: fuchsia;">MAX<span style="color: gray;">)</span> <br /></span></span></span></span><span style="font-family: Consolas;"><span style="color: blue;">SET</span> <span style="color: teal;">@SQL</span> <span style="color: gray;">=</span> <span style="color: red;">N'SELECT ProductID</span> <br /></span><span style="font-family: Consolas;"><span style="color: red;">FROM Sales.SalesOrderDetail</span> <br /></span><span style="font-family: Consolas;"><span style="color: red;">WHERE ProductID = @ProductID'</span> <br /></span><span style="font-family: Consolas;"><span style="color: blue;">EXECUTE</span> <span style="color: maroon;">sp_executesql<span style="color: blue;"> <span style="color: teal;">@SQL<span style="color: gray;">,</span> <span style="color: red;">N'@ProductID INT'<span style="color: gray;">,</span> <span style="color: teal;">@ProductID</span> <span style="color: gray;">=</span> 870 </span></span></span></span></span></p>
<p>&nbsp;</p>
<p><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/metablogapi/1602.082712_1150_ThePainsofF4.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-metablogapi/1602.082712_5F00_1150_5F00_ThePainsofF4.png" alt="" align="left" /><span style="color: black;"><span style="font-family: Courier New;"><br /></span></span></p>
<p>&nbsp;</p>
<p><span style="color: black;">Still no luck. Even those this statement was compiled using the ProductID of 870, the filtered index still isn't used. But, why not? Well, let's think back to what a filtered index actually is. A filtered index is a NC structure that contains values based on an explicit predicate. This means that the filtered index is only useful if the query is searching explicitly for the query defined in the filter definition for the index. If SQL Server were to create an execution plan that used that filtered index, it would ONLY be useful for reuse if subsequent statements passed in that specific criteria. Since SQL Server has no idea if that is going to happen, the filtered index isn't considered for reuse. </span></p>
<p><span style="color: black;">But what if you REALLY want SQL Server to use that specific filtered index? It's easy enough to try to force it using an index hint. </span></p>
<p><span style="font-family: Consolas;"><span style="color: blue;">DECLARE</span> <span style="color: teal;">@SQL</span> <span style="color: blue;">NVARCHAR<span style="color: gray;">(<span style="color: fuchsia;">MAX<span style="color: gray;">)</span> <br /></span></span></span></span><span style="font-family: Consolas;"><span style="color: blue;">SET</span> <span style="color: teal;">@SQL</span> <span style="color: gray;">=</span> <span style="color: red;">N'SELECT ProductID</span> <br /></span><span style="font-family: Consolas;"><span style="color: red;">FROM Sales.SalesOrderDetail WITH(INDEX=FIDX_SAlesOrderDetail_ProductID)</span> <br /></span><span style="font-family: Consolas;"><span style="color: red;">WHERE ProductID = @ProductID '</span> <br /></span><span style="font-family: Consolas;"><span style="color: blue;">EXECUTE</span> <span style="color: maroon;">sp_executesql<span style="color: blue;"> <span style="color: teal;">@SQL<span style="color: gray;">,</span> <span style="color: red;">N'@ProductID INT'<span style="color: gray;">,</span> <span style="color: teal;">@ProductID</span> <span style="color: gray;">=</span> 870 </span></span></span></span></span></p>
<p><span style="color: black;"><br />In this case, I actually get an error message telling me that my usage of the hint is not valid. This is due to the fact that the index I am forcing is filtered. </span></p>
<p><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/metablogapi/6567.082712_1150_ThePainsofF5.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-metablogapi/6567.082712_5F00_1150_5F00_ThePainsofF5.png" alt="" align="left" /><span style="font-family: Times New Roman; font-size: 12pt;">&nbsp;</span><span style="color: black;">&nbsp;&nbsp;<br /></span></p>
<p>&nbsp;</p>
<p><span style="color: black;">While incredibly useful, you can see that filtered indexes do have some drawbacks when it comes to how they can be applied. In most cases, the best way to take advantage of the filtered indexes you have is through the use of dynamic string execution. In this case I was lazy and just modified how I was using sp_executesql. The EXECUTE statement would have worked just as well. </span></p>
<p><span style="font-family: Consolas;"><span style="color: blue;">DECLARE</span> <span style="color: teal;">@SQL</span> <span style="color: blue;">NVARCHAR<span style="color: gray;">(<span style="color: fuchsia;">MAX<span style="color: gray;">),</span> <span style="color: teal;">@ProductID</span> <span style="color: blue;">INT</span> <br /></span></span></span></span><span style="font-family: Consolas;"><span style="color: blue;">SET</span> <span style="color: teal;">@ProductID</span> <span style="color: gray;">=</span> 870 <br /></span><span style="font-family: Consolas;"><span style="color: blue;">SET</span> <span style="color: teal;">@SQL</span> <span style="color: gray;">=</span> <span style="color: red;">N'SELECT ProductID</span> <br /></span><span style="font-family: Consolas;"><span style="color: red;">FROM Sales.SalesOrderDetail&nbsp;</span> <br /></span><span style="font-family: Consolas;"><span style="color: red;">WHERE ProductID = '</span> <span style="color: gray;">+</span> <span style="color: fuchsia;">CAST<span style="color: gray;">(<span style="color: teal;">@ProductID</span> <span style="color: blue;">AS</span> <span style="color: blue;">VARCHAR<span style="color: gray;">(</span>10<span style="color: gray;">))</span> <br /></span></span></span></span><span style="font-family: Consolas;"><span style="color: blue;">EXECUTE</span> <span style="color: maroon;">sp_executesql<span style="color: blue;"> <span style="color: teal;">@SQL </span></span></span></span></p>
<p><span style="color: black;">And, sure enough, we are able to take advantage of the filtered index because SQL Server is given an explicit value at compile time. </span></p>
<p><img src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/metablogapi/2656.082712_1150_ThePainsofF6.png" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22-metablogapi/2656.082712_5F00_1150_5F00_ThePainsofF6.png" alt="" align="left" /><span style="color: black;"><br /></span></p>
<p>&nbsp;</p>
<p><span style="color: black;">Now for some cleanup: </span></p>
<p><span style="font-family: Consolas;"><span style="color: blue;">DROP</span> <span style="color: blue;">INDEX</span> <span style="color: teal;">FIDX_SalesOrderDetail_ProductID</span> <br /></span><span style="font-family: Consolas;"><span style="color: blue;">ON</span> <span style="color: teal;">Sales<span style="color: gray;">.<span style="color: teal;">SalesOrderDetail</span> </span></span></span></p>
<p>&nbsp;</p>
<p><span style="color: black;">HTH, <br /></span><span style="color: black;">Tim&nbsp;</span><span style="font-family: Times New Roman; font-size: 12pt;">&nbsp;&nbsp;&nbsp;</span>&nbsp;</p>]]></content:encoded>
    </item>
    <item>
      <title>How to Parse DBCC MEMORYSTATUS via Powershell</title>
      <link>https://docs.microsoft.com/archive/blogs/timchapman/how-to-parse-dbcc-memorystatus-via-powershell</link>
      <pubDate>Thu, 16 Aug 2012 08:17:00 GMT</pubDate>
      <dc:creator><![CDATA[TimChapman]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/timchapman/2012/08/16/how-to-parse-dbcc-memorystatus-via-powershell/</guid>
      <description><![CDATA[SQL Server has many ways to dig deep into diagnosing memory related problems.&nbsp; Today it is...]]></description>
      <content:encoded><![CDATA[<p>SQL Server has many ways to dig deep into diagnosing memory related problems.&nbsp; Today it is common to use Dynamic Management Objects (views/functions) to expose a large portion of this information.&nbsp; However, some useful data isn't consumable by DMVs, so we must use DBCC MEMORYSTATUS to get what we need.&nbsp; The roadblock output from DBCC MEMORYSTATUS isn't easily consumable and query-able.&nbsp; So, my goal of this blog post is to create a Powershell script to load the output from the DBCC command into a local database table so that you can query/report off of it.&nbsp;</p>
<p>Note:&nbsp; While you CAN dump the contents from DBCC MEMORYSTATUS into a temp table on the server using INSERT...EXEC, an important aspect of the data isn't inserted into the temp table.&nbsp;</p>
<p>The script accepts 3 different parameters:</p>
<p>$FileLocation - if you have the DBCC MEMORYSTATUS output in a file, specify the location here.&nbsp; Otherwise the script will grab the output from DBCC MEMORYSTATUS from the instance you connect to.<br />$SQLServer - this is the SQL instance to connect and load the output from DBCC MEMORYSTATUS.<br />$DBName - this is the name of the database you want the DBCC MEMORYSTATUS output stored.</p>
<p>I've attached the file instead of pasting the output here as I couldn't get the formatting how I wanted it to be.&nbsp; If I get it corrected I'll make the change a later.</p>
<p>Once you've executed the script, you'll have output very similar to the following, which will allow you to run any reports as needed.</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/3644.DBCCMemoryStatusOutput.jpg" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22/3644.DBCCMemoryStatusOutput.jpg"><img alt="" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/01/46/22/3644.DBCCMemoryStatusOutput.jpg" original-url="https://blogs.msdn.com/resized-image.ashx/__size/550x0/__key/communityserver-blogs-components-weblogfiles/00-00-01-46-22/3644.DBCCMemoryStatusOutput.jpg" border="0" /></a></p>
<p>I hope you enjoy, and I'd love to hear comments as to how to improve the script!</p>
<p>Thanks,</p>
<p>Tim</p>
<p><span style="color: #8b0000; font-family: Lucida Console; font-size: xx-small;" face="Lucida Console" color="#8b0000" size="1"><span style="color: #8b0000; font-family: Lucida Console; font-size: xx-small;" face="Lucida Console" color="#8b0000" size="1"><span style="color: #8b0000; font-family: Lucida Console; font-size: xx-small;" face="Lucida Console" color="#8b0000" size="1"></span></span></span>&nbsp;</p>
<p><a href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/10/34/05/44/DBCCMemoryStatus.ps1" original-url="https://blogs.msdn.com/cfs-file.ashx/__key/communityserver-components-postattachments/00-10-34-05-44/DBCCMemoryStatus.ps1">DBCCMemoryStatus.ps1</a></p>]]></content:encoded>
    </item>
  </channel>
</rss>