<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SQL-R-Us &#187; Optimizer</title>
	<atom:link href="http://sqlrus.com/tag/optimizer/feed" rel="self" type="application/rss+xml" />
	<link>http://sqlrus.com</link>
	<description></description>
	<lastBuildDate>Mon, 15 Apr 2013 14:21:03 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Throw more wood on the fire!</title>
		<link>http://sqlrus.com/2011/02/throw-more-wood-on-the-fire/</link>
		<comments>http://sqlrus.com/2011/02/throw-more-wood-on-the-fire/#comments</comments>
		<pubDate>Tue, 01 Feb 2011 15:55:55 +0000</pubDate>
		<dc:creator>John Morehouse</dc:creator>
				<category><![CDATA[Indexes]]></category>
		<category><![CDATA[Optimizer]]></category>

		<guid isPermaLink="false">http://sqlrus.com/?p=149</guid>
		<description><![CDATA[A brief thought as to why adding more rows to a table gave me my expected results in the execution plan.  Namely, a key lookup.   <a href="http://sqlrus.com/2011/02/throw-more-wood-on-the-fire/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Think that you have enough rows for that index?</p>
<p>Maybe not.  Recently I was working on some demo scripts for a future presentation which included things to look for in an execution plan.  Namely, RID/Key look ups.  Naturally, I had built out a table, applied a clustered and non-clustered index, and started to run my queries.  Based on my queries, I fully expected a nice look up to happen, which would prove that my scripts were working as I expected.</p>
<pre class="brush: sql">CREATE TABLE Beaker (id INT IDENTITY(1,1)
, fname VARCHAR(100)
, lname NVARCHAR(100)
, age tinyint
, bankbalance bigint  --19 digits
)
GO
INSERT dbo.Beaker VALUES ('fname', 'lname', 34, 9223372036854775807)
GO 15000
CREATE CLUSTERED INDEX IX_ClustedIndex ON dbo.Beaker (ID)
GO
CREATE NONCLUSTERED INDEX IX_NC_Index ON dbo.Beaker (fname)
GO
-- updated some values just because
UPDATE dbo.Beaker
SET fname = 'bob'
WHERE ID between 100 and 250
GO
-- key look up
SELECT ID, age
FROM dbo.Beaker
WHERE fname = 'bob'
GO</pre>
<p>Nope.  I was wrong.</p>
<p>I couldn&#8217;t believe it.  I know that I had coded things up correctly.  I started to surf the net to see where I had screwed up.  I still couldn&#8217;t believe it!</p>
<p>I then ran across a posting from Pinal Dave (<a href="http://sqlauthority.com">Blog</a>|<a href="http://twitter.com/#!/pinaldave">Twitter)</a> talking about the same thing that I was in my presentation.  Of course, his demo scripts are much nicer than mine!  I hope, with practice and time, I&#8217;ll get there.  =) Anyway, I happen to notice that he had used a record set that was 100k rows where mine was only 15k.  I thought, would that make a difference?  I&#8217;m not sure why it would? So I tried it.</p>
<p>I truncated my table and completely reloaded the data, only this time it was set for 150k rows vs only 15k.  Once done, I re-ran my query and presto chango, the key look up is there!</p>
<div id="attachment_162" class="wp-caption aligncenter" style="width: 476px"><a href="http://sqlrus.com/sql/wp/wp-content/uploads/2011/01/beaker_key_lookup1.png"><img class="size-full wp-image-162" title="beaker_key_lookup" src="http://sqlrus.com/sql/wp/wp-content/uploads/2011/01/beaker_key_lookup1.png" alt="A key lookup" width="466" height="235" /></a><p class="wp-caption-text">Thar she blows! Key lookup off port bow!</p></div>
<p>This made me start to think, why would the optimizer do that?  The only thing that I can think of (and it&#8217;s late) is that the optimizer when evaluating the best execution plan, must decided that doing a table (IE: clustered index) scan is faster than doing a key lookup from a non-clustered index simply because of the size of the data itself.</p>
<p>This makes sense right?  Thus, when I bumped the row count to 150k thousand rows, now the optimizer has a lot more to play with and decides that doing a key look up is the best route.  I did some quick google searches however I wasn&#8217;t able to (quickly) confirm my answers but I&#8217;ll continue to look around.</p>
<p>If you think I&#8217;m wrong, please feel free to correct me.  I&#8217;m here to learn.  =)</p>
<p>I guess the moral of the story is that if you expect for the optimizer to throw back a particular type of plan operator and it doesn&#8217;t, you might want to give it more &#8220;wood&#8221; to play with.  It might surprise you.  Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://sqlrus.com/2011/02/throw-more-wood-on-the-fire/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
