<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tags 'Performance', 'Internals', and 'Seeks'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=Performance,Internals,Seeks&amp;orTags=0</link><description>Search results matching tags 'Performance', 'Internals', and 'Seeks'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>SQL Server, Seeks, and Binary Search</title><link>http://sqlblog.com/blogs/paul_white/archive/2011/08/08/sql-server-seeks-and-binary-search.aspx</link><pubDate>Mon, 08 Aug 2011 20:10:27 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:37622</guid><dc:creator>Paul White</dc:creator><description>&lt;p&gt;The following table summarizes the results from my last &lt;a href="http://sqlblog.com/blogs/paul_white/archive/2011/08/04/avoiding-uniqueness-for-performance.aspx"&gt;two&lt;/a&gt; blog entries, showing the CPU time used when performing 5 million clustered index seeks:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_2AB2C931.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="Clustered Index Seek Test Results" border="0" alt="Clustered Index Seek Test Results" src="http://sqlblog.com/blogs/paul_white/image_thumb_41FDCDA2.png" width="315" height="64" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;In test 1, making the clustered index unique improved performance by around 40%. In test 2, making the same change reduced performance by around 70% (on 64-bit systems – more on that later).&amp;#160; As a reminder, both tests use nested loops to join a single-column BIGINT table of numbers to itself:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_15749DBC.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="Query and Plans" border="0" alt="Query and Plans" src="http://sqlblog.com/blogs/paul_white/image_thumb_52491F8E.png" width="647" height="453" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;In test 1, the table contains numbers from 1 to 5 million inclusive; for test 2, the table contains the even numbers from 2 to 10 million (still 5 million rows).&amp;#160; In case you were wondering, the test 2 results are the same if we populate the table with odd numbers from 1 to 9,999,999 instead of even numbers.&amp;#160; How can we explain these results?&lt;/p&gt;  &lt;h3&gt;Performing an Index Seek&lt;/h3&gt;  &lt;p&gt;Perhaps we need to look a bit deeper into how SQL Server performs an index seek.&amp;#160; Most people know that SQL Server indexes are a &lt;a href="http://en.wikipedia.org/wiki/B%2B_tree"&gt;B+ tree&lt;/a&gt;, so let’s work through an example of locating the row containing the value 1 million, when the table contains values from 1 to 5 million, with a unique clustered index.&lt;/p&gt;  &lt;p&gt;Index seeks start at the index root page, which we can find in a number of ways (e.g. &lt;a href="http://msdn.microsoft.com/en-us/library/ms189051.aspx"&gt;sys.system_internals_allocation_units&lt;/a&gt;).&amp;#160; I happened to use &lt;a href="http://blogs.msdn.com/b/sqlserverstorageengine/archive/2006/12/13/more-undocumented-fun_3a00_-dbcc-ind_2c00_-dbcc-page_2c00_-and-off_2d00_row-columns.aspx"&gt;DBCC IND&lt;/a&gt; to determine that the root of the index was page &lt;strong&gt;117482&lt;/strong&gt; in file 1 (in Denali, we can use a new DMF: &lt;a href="http://sqlgeek.pl/2011/07/26/pl-denali-ctp3-nowe-lepsze-dbcc-ind/"&gt;sys.dm_db_database_page_allocations&lt;/a&gt;).&amp;#160; Anyway, whichever way you find it, we can use &lt;a href="http://support.microsoft.com/kb/83065"&gt;DBCC PAGE&lt;/a&gt; to look at the contents of the root page:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/SNAGHTMLd8089b_0F1DA161.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="Index Root Page Contents" border="0" alt="Index Root Page Contents" src="http://sqlblog.com/blogs/paul_white/SNAGHTMLd8089b_thumb_36946DCB.png" width="489" height="213" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The col1 (key) column shows the lowest key value that can possibly be present in the lower-level page specified by Child Page Id.&amp;#160; We can see that the lowest possible key value on page 117485 is 906305, and the next lowest key value possible is 1132881 on page 117486.&amp;#160; So, if a row containing the value 1 million exists, the next page to look at is &lt;strong&gt;117485&lt;/strong&gt;:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/SNAGHTMLdb085d_41DD8208.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="Level 1 Index Contents" border="0" alt="Level 1 Index Contents" src="http://sqlblog.com/blogs/paul_white/SNAGHTMLdb085d_thumb_383513D2.png" width="486" height="232" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;We are now at level 1 of the index, and have to scroll down the output a bit further to see that the next page to look at is &lt;strong&gt;119580&lt;/strong&gt;.&amp;#160; This page is at the leaf level of the index, and we find our target value at position 399:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_07525C67.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="Leaf level index record" border="0" alt="Leaf level index record" src="http://sqlblog.com/blogs/paul_white/image_thumb_5AC92C80.png" width="429" height="72" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;h3&gt;Binary Search&lt;/h3&gt;  &lt;p&gt;When performing an index seek, SQL Server obviously doesn’t run DBCC PAGE and scroll down the output window to find the next page to look at like we just did.&amp;#160; You may know that SQL Server can use binary search to locate index entries, but it’s worth taking a closer look at the index pages to see how this works in practice:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_6B61276E.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="SQL Server Page Structure Diagram" border="0" alt="SQL Server Page Structure Diagram" src="http://sqlblog.com/blogs/paul_white/image_thumb_61B8B938.png" width="486" height="478" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The slot array contains a series of two-byte offsets pointing to the index records.&amp;#160; The index records are not necessarily stored in any particular order on the page; only the slot array entries are guaranteed to be in sorted order.&amp;#160; Note that the sorted slot array entries do not contain the index keys themselves, just offsets.&lt;/p&gt;  &lt;h4&gt;Performance&lt;/h4&gt;  &lt;p&gt;When SQL Server uses binary search to locate an index entry, it starts by checking the middle element of the slot array (shown in red in the example above).&amp;#160; It then follows that offset pointer to the index record, and compares the value it finds there with the sought value.&amp;#160; Assuming the value is not the one we are looking for, the sorted nature of the slot array allows SQL Server to narrow the search range in half after this first comparison.&lt;/p&gt;  &lt;p&gt;If the red index record happens to contain a value &lt;em&gt;higher&lt;/em&gt; than the sought value, we know the value we are looking for is in that half of the slot array that sorts lower than the red entry.&amp;#160; This process of cutting the search space in half continues until we find the record we are looking for, or until it becomes clear that the value does not exist.&lt;/p&gt;  &lt;p&gt;The average and worst case performance of binary search is very close to log&lt;sub&gt;2&lt;/sub&gt;N comparisons to find the item of interest in a sorted list of N items.&amp;#160; For the 476 index entries found in our test example index structure at the leaf level, that means at most 9 comparison operations.&amp;#160; That compares well to a linear search of the sorted slot array, which might require as many as 476 comparisons if we are unlucky, 238 on average.&lt;/p&gt;  &lt;p&gt;None of this offers any particular insight into why a binary search into a unique indexes containing only even (or odd!) numbers should be so slow.&amp;#160; One of the advantages of binary search is that it expects to perform around the same amount of work, regardless of the distribution of the values in the index.&amp;#160; Luckily, binary search is not the only game in town.&lt;/p&gt;  &lt;h3&gt;Linear Interpolation&lt;/h3&gt;  &lt;p&gt;As efficient as binary search is in the general case, in some cases we can do better.&lt;/p&gt;  &lt;p&gt;When searching for the value 1 million, we know from level 1 of the index that the lowest key value that can appear on the leaf level page we are interested in is 999,601.&amp;#160; We also know that the lowest key value on the next page is 1,000,077.&amp;#160; From the header information on our target page, we also know there are 476 entries in the slot array on that page.&amp;#160; Given these facts, we can make an immediate guess at which slot the value 1,000,000 ought to appear in:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;expected slot = (1000077 - 999601) / 476 * (1000000 - 999601) = 399&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;This simple calculation takes us immediately to slot 399.&amp;#160; As we saw earlier, slot 399 does indeed contain the value 1,000,000:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_5EC35485.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="Slot 399" border="0" alt="Slot 399" src="http://sqlblog.com/blogs/paul_white/image_thumb_323A249F.png" width="429" height="72" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Naturally, the quality of the linear interpolation guess depends on how evenly distributed the values are within the page.&amp;#160; To a lesser extent, it also depends on how closely the minimum key value information at level 1 of the index matches reality.&amp;#160; In both tests shown in earlier blog entries, the values are precisely evenly distributed and the level 1 index key information is spot on.&lt;/p&gt;  &lt;h4&gt;Performance&lt;/h4&gt;  &lt;p&gt;Linear interpolation has the potential to be more efficient even than binary search, finding the target value in one comparison in this example, compared to nine comparisons for binary search.&amp;#160; Even where the data is not perfectly distributed, there is scope for linear interpolation to be superior, simply by applying the technique repeatedly on successively narrower ranges.&lt;/p&gt;  &lt;p&gt;A reasonable practical compromise might be to try linear interpolation two or three times, before falling back to linear or binary search on the remaining range.&amp;#160; The advantage of linear interpolation may not seem much, but consider that SQL Server makes these comparisons for every index seek operation – when doing many millions of seeks, the difference can soon add up.&amp;#160; It is probably not coincidence that the OLTP TPC benchmark tests perform a very great number of singleton index seeks.&lt;/p&gt;  &lt;h3&gt;Linear Regression&lt;/h3&gt;  &lt;p&gt;A natural extension to the idea of linear interpolation is to apply a linear regression (line of best fit) instead:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_42D21F8D.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="Linear Regression Diagram" border="0" alt="Linear Regression Diagram" src="http://sqlblog.com/blogs/paul_white/image_thumb_6717040F.png" width="646" height="461" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;In the diagram, the blue dotted line represents a linear interpolation based on the values of the smallest and highest values only.&amp;#160; The black line is obtained by a linear regression of &lt;em&gt;all&lt;/em&gt; the data values, and results in a much better fit.&lt;/p&gt;  &lt;p&gt;Since every straight line can be represented by a formula of the form y = mx + b, we can completely specify it by recording the values of the ‘m’ (slope) and ‘b’ (y-axis intercept) parameters.&amp;#160; The R&lt;sup&gt;2&lt;/sup&gt; value gives an idea of how good a fit the linear regression line is to the data.&amp;#160; In the present context, the ‘y’ value represents the indexed value, and the ‘x’ value is the slot position where that value can be found.&amp;#160; It’s easy to see that once we have a linear regression line, we can estimate the slot position for a particular sought index value by solving x = (y – b) / m.&lt;/p&gt;  &lt;p&gt;To use linear regression in a database, we might imagine storing the ‘b’ and ‘m’ parameter values somewhere (perhaps in a compact format in the page header), and deciding whether to apply the technique based on the R&lt;sup&gt;2&lt;/sup&gt; value.&lt;/p&gt;  &lt;h3&gt;What Does SQL Server Do?&lt;/h3&gt;  &lt;p&gt;All this is fine in theory, but does SQL Server &lt;em&gt;really&lt;/em&gt; ever use anything except binary search?&amp;#160; To see, I ran tests with a debugger attached to the SQL Server process, and stepped through the calls made while the seek test queries were running.&amp;#160; The call stack below was obtained when running 64-bit SQL Server:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/SNAGHTML385906e_329631C7.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="64-bit Call Stack" border="0" alt="64-bit Call Stack" src="http://sqlblog.com/blogs/paul_white/SNAGHTML385906e_thumb_28EDC391.png" width="450" height="225" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;By contrast, the following call stack comes from 32-bit SQL Server:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/SNAGHTML38c1c77_25F85EDE.png"&gt;&lt;img style="background-image:none;border-right-width:0px;padding-left:0px;padding-right:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;padding-top:0px;" title="32-bit Call Stack" border="0" alt="32-bit Call Stack" src="http://sqlblog.com/blogs/paul_white/SNAGHTML38c1c77_thumb_1C4FF0A8.png" width="459" height="218" /&gt;&lt;/a&gt;&lt;/p&gt;    &lt;p&gt;Interestingly, SQL Server follows the same code paths (at least at the call stack level, which is the best we can do without source code) regardless of whether the index is defined as unique or not, and for all the numeric data types I tested (INT, BIGINT, REAL, FLOAT, DECIMAL with a zero scale).&lt;/p&gt;  &lt;p&gt;The performance problem on x64 servers appears to be most pronounced when the key values have a small fixed gap between them.&amp;#160; As we have seen, performance is &lt;em&gt;very&lt;/em&gt; much worse on x64 servers compared with x86 versions when the table contains just even or odd numbers (i.e. with a gap of 1 between index entries).&amp;#160; If we change the test to multiply the original entries by a factor of ten (to produce a sequence of 10, 20, 30, 40…) the performance penalty all but disappears, and the x64 unique index test is around 30% faster than the non-unique test.&lt;/p&gt;  &lt;h3&gt;Conclusions and Further Reading&lt;/h3&gt;  &lt;p&gt;From the evidence available, my best guess is that 64-bit SQL Server does use linear interpolation and/or linear regression instead of binary search under suitable conditions, but that the implementation has edge-case poor performance where the index keys are separated by a small fixed offset.&amp;#160; It seems that we can achieve best performance by using a unique index where possible, and ensuring that keys are, as far as practicable, sequential and contiguous.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://academic.research.microsoft.com/Publication/2202072"&gt;B-tree Indexes, Interpolation Search, and Skew&lt;/a&gt; – Microsoft Research     &lt;br /&gt;&lt;a href="http://im.ufba.br/pub/MAT152/SemestreLetivo20052/intepolationSearch.pdf"&gt;Interpolation Search – a log log N Search&lt;/a&gt; (PDF)&lt;/p&gt;  &lt;p&gt;© 2011 Paul White&lt;/p&gt;  &lt;p&gt;email: &lt;a href="mailto:SQLkiwi@gmail.com"&gt;SQLkiwi@gmail.com&lt;/a&gt;     &lt;br /&gt;twitter: &lt;a href="http://twitter.com/SQL_Kiwi"&gt;@SQL_Kiwi&lt;/a&gt;&lt;/p&gt;</description></item></channel></rss>