<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://geeks.netindonesia.net/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>SQL Server knowledge center</title><link>http://geeks.netindonesia.net/blogs/kasim.wirama/default.aspx</link><description>everything about SQL Server</description><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>Parallelism in Index Scan</title><link>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/26/parallelism-in-index-scan.aspx</link><pubDate>Thu, 26 Jun 2008 08:51:00 GMT</pubDate><guid isPermaLink="false">5cc3a90d-ac9a-472a-8983-30514957434c:53981</guid><dc:creator>Kasim.Wirama</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://geeks.netindonesia.net/blogs/kasim.wirama/rsscomments.aspx?PostID=53981</wfw:commentRss><comments>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/26/parallelism-in-index-scan.aspx#comments</comments><description>&lt;h3 style="MARGIN:12pt 0cm 3pt;TEXT-ALIGN:center;" align="center"&gt;Parallelism in Index Scan&lt;/h3&gt;
&lt;h3 style="MARGIN:12pt 0cm 3pt;TEXT-ALIGN:center;" align="center"&gt;By : Kasim Wirama, MCDBA&lt;/h3&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;TEXT-ALIGN:justify;"&gt;&lt;font face="Times New Roman" size="3"&gt;Previous articles, I post some information about internal of parallelism. Now take a look parallelism in index scan. Is that bad if you have parallelism in index scan? Not really, I think. If the query needs to return thousands of records, this index scan will be beneficial to query performance when it is compensated by fast I/O subsystem and SQL Server has some available threads to process data pages. The more process threads and supported by fast I/O, the faster execution time will be.&lt;/font&gt;&lt;/p&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;TEXT-ALIGN:justify;"&gt;&lt;font face="Times New Roman" size="3"&gt;Now focus on parallelism in index scan. The good thing parallelism in index scan is that SQL Server is able to do load balancing among threads when those threads has different speed in processing rows, so it is expected that slow threads would not dominate total execution time.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post: &lt;/strong&gt;&lt;a href="mailto:?body=Thought you might like this: http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/26/parallelism-in-index-scan.aspx&amp;amp;subject=Parallelism in Index Scan" target="_blank" title="Send via email"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Mail.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.facebook.com/sharer.php?u=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/26/parallelism-in-index-scan.aspx&amp;amp;t=Parallelism+in+Index+Scan" target="_blank" title="Submit Parallelism in Index Scan to DotNetKicks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Facebook.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://del.icio.us/post?url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/26/parallelism-in-index-scan.aspx&amp;amp;title=Parallelism+in+Index+Scan" target="_blank" title="Submit Parallelism in Index Scan to del.icio.us"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Delicious.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.digg.com/submit?url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/26/parallelism-in-index-scan.aspx&amp;amp;phase=2" target="_blank" title="Submit Parallelism in Index Scan to digg.com"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Digg.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/26/parallelism-in-index-scan.aspx&amp;amp;title=Parallelism+in+Index+Scan" target="_blank" title="Add Parallelism in Index Scan to Live Bookmarks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Live.16.gif" border="0"&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://geeks.netindonesia.net/aggbug.aspx?PostID=53981" width="1" height="1"&gt;</description><category domain="http://geeks.netindonesia.net/blogs/kasim.wirama/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Internal of Parallelism</title><link>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/26/internal-of-parallelism.aspx</link><pubDate>Thu, 26 Jun 2008 08:50:00 GMT</pubDate><guid isPermaLink="false">5cc3a90d-ac9a-472a-8983-30514957434c:53980</guid><dc:creator>Kasim.Wirama</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://geeks.netindonesia.net/blogs/kasim.wirama/rsscomments.aspx?PostID=53980</wfw:commentRss><comments>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/26/internal-of-parallelism.aspx#comments</comments><description>&lt;h3 style="MARGIN:12pt 0cm 3pt;TEXT-ALIGN:center;" align="center"&gt;Internal of Parallelism&lt;/h3&gt;
&lt;h3 style="MARGIN:12pt 0cm 3pt;TEXT-ALIGN:center;" align="center"&gt;By : Kasim Wirama, MCDBA&lt;/h3&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;TEXT-ALIGN:justify;"&gt;&lt;font face="Times New Roman" size="3"&gt;If you notice in execution plan, probably you would find out parallelism in some operators of an execution plan. Let’s look inside parallelism behaviour in general.&lt;/font&gt;&lt;/p&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;TEXT-ALIGN:justify;"&gt;&lt;font face="Times New Roman" size="3"&gt;Parallelism of an operator is made up at least 2 threads. For example here, there is 2 threads, thread 1 and thread 2. Thread 1 itself is made up from thread producer and thread consumer, so does thread 2. Data from producer is pushed into consumer, so it provides load balancing when some consumers runs slowly, so producer can choose available producers to process next data. The way data exchange between consumers and producers is called partitioning. &lt;/font&gt;&lt;/p&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;TEXT-ALIGN:justify;"&gt;&lt;font face="Times New Roman" size="3"&gt;So I can say that parallelism has a set of consumer and also a set of producer. Based on number of consumer and producer, there are 3 kinds of parallelisms:&lt;/font&gt;&lt;/p&gt;
&lt;ol style="MARGIN-TOP:0cm;"&gt;
&lt;li class="MsoNormal" style="MARGIN:0cm 0cm 0pt;TEXT-ALIGN:justify;tab-stops:list 36.0pt;mso-list:l0 level1 lfo1;"&gt;&lt;font face="Times New Roman" size="3"&gt;gather streams parallelism&lt;/font&gt;&lt;/li&gt;&lt;/ol&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 36pt;TEXT-ALIGN:justify;"&gt;&lt;font face="Times New Roman" size="3"&gt;It consists of a number of threads at producer and only one consumer thread.&lt;/font&gt;&lt;/p&gt;
&lt;ol style="MARGIN-TOP:0cm;" start="2"&gt;
&lt;li class="MsoNormal" style="MARGIN:0cm 0cm 0pt;TEXT-ALIGN:justify;tab-stops:list 36.0pt;mso-list:l0 level1 lfo1;"&gt;&lt;font face="Times New Roman" size="3"&gt;repartition streams parallelism&lt;/font&gt;&lt;/li&gt;&lt;/ol&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 36pt;TEXT-ALIGN:justify;"&gt;&lt;font face="Times New Roman" size="3"&gt;It consists of a number of threads at producer and a number of threads at consumer.&lt;/font&gt;&lt;/p&gt;
&lt;ol style="MARGIN-TOP:0cm;" start="3"&gt;
&lt;li class="MsoNormal" style="MARGIN:0cm 0cm 0pt;TEXT-ALIGN:justify;tab-stops:list 36.0pt;mso-list:l0 level1 lfo1;"&gt;&lt;font face="Times New Roman" size="3"&gt;distribute streams parallelism&lt;/font&gt;&lt;/li&gt;&lt;/ol&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 36pt;TEXT-ALIGN:justify;"&gt;&lt;font face="Times New Roman" size="3"&gt;It consists of a number of threads at consumer and only one producer thread.&lt;/font&gt;&lt;/p&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;TEXT-ALIGN:justify;"&gt;&lt;font face="Times New Roman" size="3"&gt;Data is partitioned during parallelism process taking place, but it is valid only for repartition and distribute streams kind of parallelism. There are 4 types of way how parallelism partitions data into multiple consumers:&lt;/font&gt;&lt;/p&gt;
&lt;ol style="MARGIN-TOP:0cm;"&gt;
&lt;li class="MsoNormal" style="MARGIN:0cm 0cm 0pt;TEXT-ALIGN:justify;tab-stops:list 36.0pt;mso-list:l1 level1 lfo2;"&gt;&lt;font face="Times New Roman" size="3"&gt;broadcast partitioning&lt;/font&gt;&lt;/li&gt;&lt;/ol&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 36pt;TEXT-ALIGN:justify;"&gt;&lt;font face="Times New Roman" size="3"&gt;This type of partitioning is called when producer send all data to all consumers.&lt;/font&gt;&lt;/p&gt;
&lt;ol style="MARGIN-TOP:0cm;" start="2"&gt;
&lt;li class="MsoNormal" style="MARGIN:0cm 0cm 0pt;TEXT-ALIGN:justify;tab-stops:list 36.0pt;mso-list:l1 level1 lfo2;"&gt;&lt;font face="Times New Roman" size="3"&gt;hash partitioning&lt;/font&gt;&lt;/li&gt;&lt;/ol&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 36pt;TEXT-ALIGN:justify;"&gt;&lt;font face="Times New Roman" size="3"&gt;This type of partitioning is called when determining hash value for row that would be sent to particular consumers.&lt;/font&gt;&lt;/p&gt;
&lt;ol style="MARGIN-TOP:0cm;" start="3"&gt;
&lt;li class="MsoNormal" style="MARGIN:0cm 0cm 0pt;TEXT-ALIGN:justify;tab-stops:list 36.0pt;mso-list:l1 level1 lfo2;"&gt;&lt;font face="Times New Roman" size="3"&gt;round robin partitioning&lt;/font&gt;&lt;/li&gt;&lt;/ol&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 36pt;TEXT-ALIGN:justify;"&gt;&lt;font face="Times New Roman" size="3"&gt;This type will send data to consumer in sequence manner.&lt;/font&gt;&lt;/p&gt;
&lt;ol style="MARGIN-TOP:0cm;" start="4"&gt;
&lt;li class="MsoNormal" style="MARGIN:0cm 0cm 0pt;TEXT-ALIGN:justify;tab-stops:list 36.0pt;mso-list:l1 level1 lfo2;"&gt;&lt;font face="Times New Roman" size="3"&gt;demand partitioning&lt;/font&gt;&lt;/li&gt;&lt;/ol&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 36pt;TEXT-ALIGN:justify;"&gt;&lt;font face="Times New Roman" size="3"&gt;This type is pull type partitioning because producers pull data from consumers. It happens on distributed partitioned id on partitioned table.&lt;/font&gt;&lt;/p&gt;
&lt;ol style="MARGIN-TOP:0cm;" start="5"&gt;
&lt;li class="MsoNormal" style="MARGIN:0cm 0cm 0pt;TEXT-ALIGN:justify;tab-stops:list 36.0pt;mso-list:l1 level1 lfo2;"&gt;&lt;font face="Times New Roman" size="3"&gt;range partitioning&lt;/font&gt;&lt;/li&gt;&lt;/ol&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 36pt;TEXT-ALIGN:justify;"&gt;&lt;font face="Times New Roman" size="3"&gt;This type will partition data by evaluating value of a column of a row with range function to determine which producer will receive the data. &lt;/font&gt;&lt;/p&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;TEXT-ALIGN:justify;"&gt;&lt;font face="Times New Roman" size="3"&gt;Regarding to parallelism, one of issue that might happen is parallel deadlock. Potential query that has parallel deadlock is on order-preserving query. Example of order preserving query is ORDER BY, or if you check of text based execution plan, you will find out parallelism operator along with its attribute like this : “Parallelism(Gather Stream, &lt;b style="mso-bidi-font-weight:normal;"&gt;ORDER BY&lt;/b&gt;:&amp;lt;some table&amp;gt;.&amp;lt;one or more column&amp;gt;)”. Words that is marked as bold, it is the property of parallelism that is order-preserving. You can check whether parallel deadlock might exist at your SQL Server by querying sys.dm_os_waiting_tasks (dynamic management view), notice at wait_type for same session. If you find entry CXPACKETS, it gives you indication that parallel deadlock exists at your SQL Server. Solution for removing CXPACKETS is to reduce max DOP.&lt;/font&gt;&lt;/p&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;TEXT-ALIGN:justify;"&gt;&lt;font face="Times New Roman" size="3"&gt;Final thing that parallelism might happen is about the number of rows in a table. When a table size is huge, for example containing 300,000 rows, parallelism will more potentially happen compared with table with size 10,000 rows.&lt;/font&gt;&lt;/p&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post: &lt;/strong&gt;&lt;a href="mailto:?body=Thought you might like this: http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/26/internal-of-parallelism.aspx&amp;amp;subject=Internal of Parallelism" target="_blank" title="Send via email"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Mail.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.facebook.com/sharer.php?u=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/26/internal-of-parallelism.aspx&amp;amp;t=Internal+of+Parallelism" target="_blank" title="Submit Internal of Parallelism to DotNetKicks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Facebook.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://del.icio.us/post?url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/26/internal-of-parallelism.aspx&amp;amp;title=Internal+of+Parallelism" target="_blank" title="Submit Internal of Parallelism to del.icio.us"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Delicious.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.digg.com/submit?url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/26/internal-of-parallelism.aspx&amp;amp;phase=2" target="_blank" title="Submit Internal of Parallelism to digg.com"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Digg.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/26/internal-of-parallelism.aspx&amp;amp;title=Internal+of+Parallelism" target="_blank" title="Add Internal of Parallelism to Live Bookmarks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Live.16.gif" border="0"&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://geeks.netindonesia.net/aggbug.aspx?PostID=53980" width="1" height="1"&gt;</description><category domain="http://geeks.netindonesia.net/blogs/kasim.wirama/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>SQL Internal Viewer Tool</title><link>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/sql-internal-viewer-tool.aspx</link><pubDate>Sun, 22 Jun 2008 02:10:00 GMT</pubDate><guid isPermaLink="false">5cc3a90d-ac9a-472a-8983-30514957434c:53313</guid><dc:creator>Kasim.Wirama</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://geeks.netindonesia.net/blogs/kasim.wirama/rsscomments.aspx?PostID=53313</wfw:commentRss><comments>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/sql-internal-viewer-tool.aspx#comments</comments><description>&lt;p&gt;You would like to know internal structure of your SQL Server? Now, you can view how fragmented your database visually or other information regarding to allocation map through freeware tool for SQL Server, namely SQL Internal Viewer (&lt;a href="http://www.sqlinternalsviewer.com/"&gt;http://www.sqlinternalsviewer.com/&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;&amp;nbsp;In this website, you can also learn from blogs published by the author.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;I recommend this tool for anyone who are interested with internal structure of SQL Server.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;Enjoy!&lt;/p&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post: &lt;/strong&gt;&lt;a href="mailto:?body=Thought you might like this: http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/sql-internal-viewer-tool.aspx&amp;amp;subject=SQL Internal Viewer Tool" target="_blank" title="Send via email"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Mail.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.facebook.com/sharer.php?u=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/sql-internal-viewer-tool.aspx&amp;amp;t=SQL+Internal+Viewer+Tool" target="_blank" title="Submit SQL Internal Viewer Tool to DotNetKicks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Facebook.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://del.icio.us/post?url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/sql-internal-viewer-tool.aspx&amp;amp;title=SQL+Internal+Viewer+Tool" target="_blank" title="Submit SQL Internal Viewer Tool to del.icio.us"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Delicious.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.digg.com/submit?url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/sql-internal-viewer-tool.aspx&amp;amp;phase=2" target="_blank" title="Submit SQL Internal Viewer Tool to digg.com"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Digg.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/sql-internal-viewer-tool.aspx&amp;amp;title=SQL+Internal+Viewer+Tool" target="_blank" title="Add SQL Internal Viewer Tool to Live Bookmarks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Live.16.gif" border="0"&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://geeks.netindonesia.net/aggbug.aspx?PostID=53313" width="1" height="1"&gt;</description><category domain="http://geeks.netindonesia.net/blogs/kasim.wirama/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Table Spool in Equality Correlated Subquery</title><link>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/table-spool-in-equality-correlated-subquery.aspx</link><pubDate>Sun, 22 Jun 2008 02:00:00 GMT</pubDate><guid isPermaLink="false">5cc3a90d-ac9a-472a-8983-30514957434c:53311</guid><dc:creator>Kasim.Wirama</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://geeks.netindonesia.net/blogs/kasim.wirama/rsscomments.aspx?PostID=53311</wfw:commentRss><comments>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/table-spool-in-equality-correlated-subquery.aspx#comments</comments><description>&lt;div class="Section1"&gt;
&lt;h2 style="TEXT-ALIGN:center;" align="center"&gt;Table Spool in Equality Correlated &lt;span class="SpellE"&gt;Subquery&lt;/span&gt;&lt;/h2&gt;
&lt;h3 style="TEXT-ALIGN:center;" align="center"&gt;&lt;span class="GramE"&gt;By :&lt;/span&gt; Kasim Wirama, MCDBA&lt;/h3&gt;&amp;nbsp; 
&lt;p class="MsoNormal" style="LINE-HEIGHT:150%;TEXT-ALIGN:justify;"&gt;Recent posting, I examine spooling characteristic in &lt;span class="SpellE"&gt;nonequality&lt;/span&gt; correlated &lt;span class="SpellE"&gt;subquery&lt;/span&gt;. In &lt;span class="SpellE"&gt;nonequality&lt;/span&gt; correlated &lt;span class="SpellE"&gt;subquery&lt;/span&gt;, optimizer generate index spool which created index on the fly spool operation and cache execution result from immediately previous execution. They are called eager index spool and lazy index spool. Let’s take a look spooling behavior in equijoin correlated &lt;span class="SpellE"&gt;subquery&lt;/span&gt;.&lt;/p&gt;
&lt;p class="MsoNormal"&gt;Given the example of equijoin correlated &lt;span class="SpellE"&gt;subquery&lt;/span&gt; &lt;span class="GramE"&gt;below :&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN-BOTTOM:0pt;LINE-HEIGHT:normal;"&gt;&lt;span class="GramE"&gt;select&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN-BOTTOM:0pt;LINE-HEIGHT:normal;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;o1.OrderID, o1.Freight&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN-BOTTOM:0pt;LINE-HEIGHT:normal;"&gt;&lt;span class="GramE"&gt;from&lt;/span&gt; &lt;span class="SpellE"&gt;dbo.Orders&lt;/span&gt; as o1&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN-BOTTOM:0pt;LINE-HEIGHT:normal;"&gt;&lt;span class="GramE"&gt;where&lt;/span&gt; o1.Freight &amp;gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN-BOTTOM:0pt;LINE-HEIGHT:normal;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;(&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN-BOTTOM:0pt;LINE-HEIGHT:normal;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;select&lt;/span&gt; AVG(o2.freight)&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN-BOTTOM:0pt;LINE-HEIGHT:normal;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;from&lt;/span&gt; &lt;span class="SpellE"&gt;dbo.Orders&lt;/span&gt; as o2&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN-BOTTOM:0pt;LINE-HEIGHT:normal;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="GramE"&gt;where&lt;/span&gt; o2.CustomerID = o1.CustomerID&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN-BOTTOM:0pt;LINE-HEIGHT:normal;tab-stops:94.5pt;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;);&lt;span style="mso-tab-count:1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/p&gt;&amp;nbsp; 
&lt;p class="MsoNormal" style="MARGIN-BOTTOM:0pt;LINE-HEIGHT:150%;TEXT-ALIGN:justify;tab-stops:94.5pt;"&gt;The query displays order information for each &lt;span class="GramE"&gt;customers&lt;/span&gt; whose freight cost is above average for themselves. Notice that predicate in inner query uses equality (=) instead of &lt;span class="SpellE"&gt;nonequality&lt;/span&gt; in my recent posting. Execution plan for this correlated &lt;span class="SpellE"&gt;subquery&lt;/span&gt; &lt;span class="GramE"&gt;is :&lt;/span&gt;&lt;/p&gt;&amp;nbsp; 
&lt;p class="MsoNormal" style="MARGIN-BOTTOM:0pt;LINE-HEIGHT:normal;tab-stops:94.5pt;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;|--Nested &lt;span class="GramE"&gt;Loops(&lt;/span&gt;Inner Join)&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN-BOTTOM:0pt;LINE-HEIGHT:normal;tab-stops:94.5pt;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--&lt;b style="mso-bidi-font-weight:normal;"&gt;Table Spool&lt;/b&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN-BOTTOM:0pt;LINE-HEIGHT:normal;tab-stops:94.5pt;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Segment&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN-BOTTOM:0pt;LINE-HEIGHT:normal;tab-stops:94.5pt;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--&lt;span class="GramE"&gt;Sort(&lt;/span&gt;ORDER BY:([o1].[&lt;span class="SpellE"&gt;CustomerID&lt;/span&gt;] ASC))&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 0pt 1in;TEXT-INDENT:-1in;LINE-HEIGHT:normal;tab-stops:94.5pt;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Clustered Index Scan(OBJECT:([&lt;span class="SpellE"&gt;Northwind&lt;/span&gt;].[&lt;span class="SpellE"&gt;dbo&lt;/span&gt;].[Orders].[&lt;span class="SpellE"&gt;PK_Orders&lt;/span&gt;] AS [o1]), &lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/span&gt;WHERE:([&lt;span class="SpellE"&gt;Northwind&lt;/span&gt;].[&lt;span class="SpellE"&gt;dbo&lt;/span&gt;].[Orders].[&lt;span class="SpellE"&gt;CustomerID&lt;/span&gt;] as [o1].[&lt;span class="SpellE"&gt;CustomerID&lt;/span&gt;] IS NOT NULL))&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 0pt 0.5in;TEXT-INDENT:-0.5in;LINE-HEIGHT:normal;tab-stops:94.5pt;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Nested &lt;span class="GramE"&gt;Loops(&lt;/span&gt;Inner Join, WHERE:([&lt;span class="SpellE"&gt;Northwind&lt;/span&gt;].[&lt;span class="SpellE"&gt;dbo&lt;/span&gt;].[Orders].[Freight] as [o1].[Freight]&amp;gt;[Expr1004]))&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 0pt 40.5pt;TEXT-INDENT:-40.5pt;LINE-HEIGHT:normal;tab-stops:94.5pt;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Compute &lt;span class="GramE"&gt;Scalar(&lt;/span&gt;DEFINE:([Expr1004]=CASE WHEN [Expr1012]=(0) THEN NULL ELSE [Expr1013]/CONVERT_IMPLICIT(money,[Expr1012],0) END))&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 0pt 0.75in;TEXT-INDENT:-0.75in;LINE-HEIGHT:normal;tab-stops:94.5pt;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Stream Aggregate(DEFINE:([Expr1012]=COUNT_BIG([Northwind].[dbo].[Orders].[Freight] as [o1].[Freight]), [Expr1013]=SUM([&lt;span class="SpellE"&gt;Northwind&lt;/span&gt;].[&lt;span class="SpellE"&gt;dbo&lt;/span&gt;].[Orders].[Freight] as [o1].[Freight])))&lt;/p&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--&lt;b style="mso-bidi-font-weight:normal;"&gt;Table Spool&lt;/b&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--&lt;b style="mso-bidi-font-weight:normal;"&gt;Table Spool&lt;/b&gt;&lt;b style="mso-bidi-font-weight:normal;"&gt;&amp;nbsp;&lt;/b&gt; 
&lt;p class="MsoNormal" style="MARGIN-BOTTOM:0pt;LINE-HEIGHT:150%;TEXT-ALIGN:justify;tab-stops:94.5pt;"&gt;First optimizer does scanning all records in Orders table, then sorts the rows based on &lt;span class="SpellE"&gt;CustomerID&lt;/span&gt;. Next operator is Segment operator. Segment operator will break all records into groups partitioned by &lt;span class="SpellE"&gt;CustomerID&lt;/span&gt; column. For each &lt;span class="GramE"&gt;groups&lt;/span&gt; will be insert into &lt;b style="mso-bidi-font-weight:normal;"&gt;Table Spool&lt;/b&gt; operator. Output of table spool is one row which will be processed by topmost Nested Loops operator. Now optimizer will count average freight from second Table Spool operator. Average function is &lt;span class="SpellE"&gt;representated&lt;/span&gt; by Compute Scalar operator and Stream Aggregate operator, so the function return &lt;span class="GramE"&gt;1 average freight&lt;/span&gt; for current &lt;span class="SpellE"&gt;customerid&lt;/span&gt; and it compares each rows of &lt;span class="SpellE"&gt;customerid&lt;/span&gt; from third Table Spool. After the current group of &lt;span class="SpellE"&gt;customerid&lt;/span&gt; has been processed, table spool is truncated and next group of &lt;span class="SpellE"&gt;customerid&lt;/span&gt; will be inserted into table spool operator. Do second and third table spool operators refer to first table spool operator? The answer is yes, at graphical execution plan you can check Primary Node ID property for second and third one is same as Node ID property for first Table Spool operator.&lt;/p&gt;&amp;nbsp; 
&lt;p class="MsoNormal" style="MARGIN-BOTTOM:0pt;LINE-HEIGHT:150%;TEXT-ALIGN:justify;tab-stops:94.5pt;"&gt;If you compare in what extents rewinds between table spool and index spool. Rewinds rate of index spool is higher than that of table spool. So for data that contains many duplicates, index spool performs better than table spool.&lt;/p&gt;&lt;/div&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post: &lt;/strong&gt;&lt;a href="mailto:?body=Thought you might like this: http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/table-spool-in-equality-correlated-subquery.aspx&amp;amp;subject=Table Spool in Equality Correlated Subquery" target="_blank" title="Send via email"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Mail.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.facebook.com/sharer.php?u=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/table-spool-in-equality-correlated-subquery.aspx&amp;amp;t=Table+Spool+in+Equality+Correlated+Subquery" target="_blank" title="Submit Table Spool in Equality Correlated Subquery to DotNetKicks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Facebook.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://del.icio.us/post?url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/table-spool-in-equality-correlated-subquery.aspx&amp;amp;title=Table+Spool+in+Equality+Correlated+Subquery" target="_blank" title="Submit Table Spool in Equality Correlated Subquery to del.icio.us"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Delicious.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.digg.com/submit?url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/table-spool-in-equality-correlated-subquery.aspx&amp;amp;phase=2" target="_blank" title="Submit Table Spool in Equality Correlated Subquery to digg.com"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Digg.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/table-spool-in-equality-correlated-subquery.aspx&amp;amp;title=Table+Spool+in+Equality+Correlated+Subquery" target="_blank" title="Add Table Spool in Equality Correlated Subquery to Live Bookmarks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Live.16.gif" border="0"&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://geeks.netindonesia.net/aggbug.aspx?PostID=53311" width="1" height="1"&gt;</description><category domain="http://geeks.netindonesia.net/blogs/kasim.wirama/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Parallelism Overview</title><link>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/parallelism-overview.aspx</link><pubDate>Sun, 22 Jun 2008 01:59:00 GMT</pubDate><guid isPermaLink="false">5cc3a90d-ac9a-472a-8983-30514957434c:53309</guid><dc:creator>Kasim.Wirama</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://geeks.netindonesia.net/blogs/kasim.wirama/rsscomments.aspx?PostID=53309</wfw:commentRss><comments>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/parallelism-overview.aspx#comments</comments><description>&lt;div class="Section1"&gt;
&lt;h2 style="TEXT-ALIGN:center;" align="center"&gt;Parallelism Overview&lt;/h2&gt;
&lt;h4 style="TEXT-ALIGN:center;" align="center"&gt;&lt;span class="GramE"&gt;By :&lt;/span&gt; Kasim Wirama, MCDBA&lt;/h4&gt;&amp;nbsp; 
&lt;p class="MsoNormal" style="LINE-HEIGHT:150%;TEXT-ALIGN:justify;"&gt;When you observe execution plan for some query that processes many rows, you will find operator with small arrow at graphical execution plan. For example, you have Index Scan operator with double small arrow icon at the icon. It shows that the operator has been parallelized in its scanning. Parallelism would potentially happen in &lt;span class="SpellE"&gt;hyperthreading&lt;/span&gt;, multi core, or multi processor environment.&lt;/p&gt;
&lt;p class="MsoNormal" style="LINE-HEIGHT:150%;TEXT-ALIGN:justify;"&gt;Parallelism usually is used to process query with many rows (bigger query) with same amount of time as smaller query does.&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;Though the response time is maintained short with parallelism, but it gives overhead to CPU, thus it is not preferable in high concurrent OLTP system because it will lower down database throughput.&lt;/p&gt;
&lt;p class="MsoNormal" style="LINE-HEIGHT:150%;TEXT-ALIGN:justify;"&gt;In lower level, optimizer will partition input data into required and available CPU to output the result as quick as possible. Each CPU gives one thread and each thread will execute same aggregate function, join, &lt;span class="GramE"&gt;etc .&lt;/span&gt; They executes independently among another.&lt;/p&gt;
&lt;p class="MsoNormal" style="LINE-HEIGHT:150%;TEXT-ALIGN:justify;"&gt;&lt;span class="GramE"&gt;Optimizer choose&lt;/span&gt; between serial and parallel plan based on cheaper cost.&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;Here are configuration settings that acts as cost guideline for &lt;span class="GramE"&gt;optimizer :&lt;/span&gt; &lt;/p&gt;
&lt;p class="MsoListParagraphCxSpFirst" style="TEXT-INDENT:-0.25in;LINE-HEIGHT:150%;TEXT-ALIGN:justify;mso-list:l0 level1 lfo1;"&gt;&lt;span style="mso-bidi-font-family:Calibri;mso-bidi-theme-font:minor-latin;"&gt;&lt;span style="mso-list:Ignore;"&gt;1.&lt;span style="FONT:7pt &amp;#39;Times New Roman&amp;#39;;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;b style="mso-bidi-font-weight:normal;"&gt;Affinity Mask&lt;/b&gt; setting that allows optimizer to use multi processor. By default the value is 0, it means optimizer can use all available processor.&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpMiddle" style="TEXT-INDENT:-0.25in;LINE-HEIGHT:150%;TEXT-ALIGN:justify;mso-list:l0 level1 lfo1;"&gt;&lt;span style="mso-bidi-font-family:Calibri;mso-bidi-theme-font:minor-latin;"&gt;&lt;span style="mso-list:Ignore;"&gt;2.&lt;span style="FONT:7pt &amp;#39;Times New Roman&amp;#39;;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;b style="mso-bidi-font-weight:normal;"&gt;Max degree of parallelism&lt;/b&gt; setting. By default the value is zero or more than one, it means optimizer can generate parallelism. If the value is one, only serial plan could be generated by optimizer.&lt;/p&gt;
&lt;p class="MsoListParagraphCxSpLast" style="TEXT-INDENT:-0.25in;LINE-HEIGHT:150%;TEXT-ALIGN:justify;mso-list:l0 level1 lfo1;"&gt;&lt;span style="mso-bidi-font-family:Calibri;mso-bidi-theme-font:minor-latin;"&gt;&lt;span style="mso-list:Ignore;"&gt;3.&lt;span style="FONT:7pt &amp;#39;Times New Roman&amp;#39;;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;b style="mso-bidi-font-weight:normal;"&gt;Cost threshold for parallelism&lt;/b&gt; setting. The higher the value, the less likely optimizer will choose parallel plan if the serial plan has cost less than cost threshold.&lt;/p&gt;
&lt;p class="MsoNormal" style="LINE-HEIGHT:150%;TEXT-ALIGN:justify;"&gt;You can find these settings by running this SQL command at SQL Server Management &lt;span class="GramE"&gt;Studio :&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="LINE-HEIGHT:150%;"&gt;EXEC &lt;span class="SpellE"&gt;sp_configure&lt;/span&gt; ‘show advanced options’, 1; &lt;br /&gt;RECONFIGURE;&lt;/p&gt;&lt;/div&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post: &lt;/strong&gt;&lt;a href="mailto:?body=Thought you might like this: http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/parallelism-overview.aspx&amp;amp;subject=Parallelism Overview" target="_blank" title="Send via email"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Mail.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.facebook.com/sharer.php?u=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/parallelism-overview.aspx&amp;amp;t=Parallelism+Overview" target="_blank" title="Submit Parallelism Overview to DotNetKicks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Facebook.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://del.icio.us/post?url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/parallelism-overview.aspx&amp;amp;title=Parallelism+Overview" target="_blank" title="Submit Parallelism Overview to del.icio.us"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Delicious.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.digg.com/submit?url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/parallelism-overview.aspx&amp;amp;phase=2" target="_blank" title="Submit Parallelism Overview to digg.com"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Digg.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/parallelism-overview.aspx&amp;amp;title=Parallelism+Overview" target="_blank" title="Add Parallelism Overview to Live Bookmarks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Live.16.gif" border="0"&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://geeks.netindonesia.net/aggbug.aspx?PostID=53309" width="1" height="1"&gt;</description><category domain="http://geeks.netindonesia.net/blogs/kasim.wirama/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Degree of Parallelism, Max Degree of Parallelism, Affinity Mask and Thread Allocation</title><link>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/degree-of-parallelism-max-degree-of-parallelism-affinity-mask-and-thread-allocation.aspx</link><pubDate>Sun, 22 Jun 2008 01:59:00 GMT</pubDate><guid isPermaLink="false">5cc3a90d-ac9a-472a-8983-30514957434c:53310</guid><dc:creator>Kasim.Wirama</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://geeks.netindonesia.net/blogs/kasim.wirama/rsscomments.aspx?PostID=53310</wfw:commentRss><comments>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/degree-of-parallelism-max-degree-of-parallelism-affinity-mask-and-thread-allocation.aspx#comments</comments><description>&lt;div class="Section1"&gt;
&lt;h2 style="TEXT-ALIGN:center;" align="center"&gt;Degree of Parallelism, Max Degree of Parallelism, &lt;/h2&gt;
&lt;h2 style="TEXT-ALIGN:center;" align="center"&gt;Affinity Mask and Thread Allocation&lt;/h2&gt;
&lt;h4 style="TEXT-ALIGN:center;tab-stops:171.75pt;" align="center"&gt;&lt;span class="GramE"&gt;By :&lt;/span&gt; Kasim Wirama, MCDBA&lt;/h4&gt;&amp;nbsp; 
&lt;p class="MsoNormal" style="TEXT-ALIGN:justify;"&gt;You can view parallelism in your execution plan by viewing either graphically or textually. Optimizer decides between parallelism and serial plan on cost-based. Parallelism itself is determined by SQL Server advanced configuration setting and number of processor of SQL Server resides. One of advanced configuration item relating to parallelism is Max Degree of Parallelism and Affinity Mask.&lt;/p&gt;
&lt;p class="MsoNormal" style="TEXT-ALIGN:justify;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="TEXT-ALIGN:justify;"&gt;If you don’t specify MAXDOP N query hints, by default max degree of parallelism is equal to number of processor that is allowed to give the thread to run corresponding parallelism operator. The number of allowed processor is determined through Affinity Mask in advanced configuration setting. If you specify MAXDOP N, it will used N DOP, but if you put MAXDOP 0, the max DOP will be equal to number of allowed processor in Affinity Mask.&lt;/p&gt;
&lt;p class="MsoNormal" style="TEXT-ALIGN:justify;"&gt;&amp;nbsp;&lt;/p&gt;
&lt;p class="MsoNormal" style="TEXT-ALIGN:justify;"&gt;DOP reflects number of thread that will execute on an operator. Even though you have max DOP more than allowed processor, it doesn’t mean that you can have all thread on the max DOP. It depends on the available thread which is specified in Max Worker Thread (found in advanced configuration setting). Max Worker Thread itself depends on number of processor and kinds of platform (32 bit or 64 bit). You can view how much query request takes number of thread through querying to &lt;span class="SpellE"&gt;Sys.dm_os_tasks&lt;/span&gt;. The information difference between &lt;span class="SpellE"&gt;sys.dm_os_tasks&lt;/span&gt; and parallelism in execution plan is that &lt;span class="SpellE"&gt;sys.dm_os_tasks&lt;/span&gt; gives information all threads in a query while parallelism in execution plan gives information about how much thread taken by each &lt;span class="GramE"&gt;operators&lt;/span&gt;. Generally parallelism among operators could be executed at the same time, but exception applies here is that, if you found that there is blocking operator(s), the execution between operator before and after blocking operator is not done at the same time, so those operators can share resources such as memory and threads.&lt;/p&gt;&lt;/div&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post: &lt;/strong&gt;&lt;a href="mailto:?body=Thought you might like this: http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/degree-of-parallelism-max-degree-of-parallelism-affinity-mask-and-thread-allocation.aspx&amp;amp;subject=Degree of Parallelism, Max Degree of Parallelism, Affinity Mask and Thread Allocation" target="_blank" title="Send via email"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Mail.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.facebook.com/sharer.php?u=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/degree-of-parallelism-max-degree-of-parallelism-affinity-mask-and-thread-allocation.aspx&amp;amp;t=Degree+of+Parallelism%2c+Max+Degree+of+Parallelism%2c+Affinity+Mask+and+Thread+Allocation" target="_blank" title="Submit Degree of Parallelism, Max Degree of Parallelism, Affinity Mask and Thread Allocation to DotNetKicks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Facebook.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://del.icio.us/post?url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/degree-of-parallelism-max-degree-of-parallelism-affinity-mask-and-thread-allocation.aspx&amp;amp;title=Degree+of+Parallelism%2c+Max+Degree+of+Parallelism%2c+Affinity+Mask+and+Thread+Allocation" target="_blank" title="Submit Degree of Parallelism, Max Degree of Parallelism, Affinity Mask and Thread Allocation to del.icio.us"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Delicious.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.digg.com/submit?url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/degree-of-parallelism-max-degree-of-parallelism-affinity-mask-and-thread-allocation.aspx&amp;amp;phase=2" target="_blank" title="Submit Degree of Parallelism, Max Degree of Parallelism, Affinity Mask and Thread Allocation to digg.com"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Digg.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/degree-of-parallelism-max-degree-of-parallelism-affinity-mask-and-thread-allocation.aspx&amp;amp;title=Degree+of+Parallelism%2c+Max+Degree+of+Parallelism%2c+Affinity+Mask+and+Thread+Allocation" target="_blank" title="Add Degree of Parallelism, Max Degree of Parallelism, Affinity Mask and Thread Allocation to Live Bookmarks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Live.16.gif" border="0"&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://geeks.netindonesia.net/aggbug.aspx?PostID=53310" width="1" height="1"&gt;</description><category domain="http://geeks.netindonesia.net/blogs/kasim.wirama/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Decorrelating Correlated Subquery</title><link>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/decorrelating-correlated-subquery.aspx</link><pubDate>Sun, 22 Jun 2008 01:57:00 GMT</pubDate><guid isPermaLink="false">5cc3a90d-ac9a-472a-8983-30514957434c:53308</guid><dc:creator>Kasim.Wirama</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://geeks.netindonesia.net/blogs/kasim.wirama/rsscomments.aspx?PostID=53308</wfw:commentRss><comments>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/decorrelating-correlated-subquery.aspx#comments</comments><description>&lt;h2 style="MARGIN:10pt 0cm 0pt;TEXT-ALIGN:center;" align="center"&gt;&lt;span&gt;&lt;font face="Cambria" color="#4f81bd" size="4"&gt;Decorrelating Correlated Subquery&lt;/font&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;h4 style="MARGIN:10pt 0cm 0pt;TEXT-ALIGN:center;" align="center"&gt;&lt;span&gt;&lt;em&gt;&lt;font face="Cambria" color="#4f81bd"&gt;By : Kasim Wirama, MCDBA&lt;/font&gt;&lt;/em&gt;&lt;/span&gt;&lt;/h4&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt; 
&lt;p class="MsoNormal" style="TEXT-JUSTIFY:inter-ideograph;MARGIN:0cm 0cm 10pt;TEXT-ALIGN:justify;"&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;I have written posting about correlated and noncorrelated subquery. Now let’s take a look correlated subquery but optimizer generated noncorrelated query-like execution plan.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="TEXT-JUSTIFY:inter-ideograph;MARGIN:0cm 0cm 10pt;TEXT-ALIGN:justify;"&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;Here is query to display order for each customer who lives in Madrid&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;"&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;select orderid&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;"&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;from orders as o&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;"&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;where exists&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;select *&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;from Customers as c&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;where c.CustomerID = o.CustomerID&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;and c.City = &amp;#39;Madrid&amp;#39;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;)&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt; 
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;"&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;Execution plan for the correlated subquery is :&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;|--Nested Loops(Inner Join, OUTER REFERENCES:(&lt;img src="http://geeks.netindonesia.net/emoticons/emotion-44.gif" alt="Coffee" /&gt;.[CustomerID]))&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 27pt;TEXT-INDENT:-27pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Index Seek(OBJECT:([Northwind].[dbo].[Customers].[City] AS &lt;img src="http://geeks.netindonesia.net/emoticons/emotion-44.gif" alt="Coffee" /&gt;), SEEK:(&lt;img src="http://geeks.netindonesia.net/emoticons/emotion-44.gif" alt="Coffee" /&gt;.[City]=N&amp;#39;Madrid&amp;#39;) ORDERED FORWARD)&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 27pt;TEXT-INDENT:-27pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Index Seek(OBJECT:([Northwind].[dbo].[Orders].[CustomerID] AS &lt;img src="http://geeks.netindonesia.net/emoticons/emotion-31.gif" alt="Time" /&gt;), SEEK:(&lt;img src="http://geeks.netindonesia.net/emoticons/emotion-31.gif" alt="Time" /&gt;.[CustomerID]=[Northwind].[dbo].[Customers].[CustomerID] as &lt;img src="http://geeks.netindonesia.net/emoticons/emotion-44.gif" alt="Coffee" /&gt;.[CustomerID]) ORDERED FORWARD)&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt; 
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 27pt;TEXT-INDENT:-27pt;"&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;The execution above is same as execution plan for the following query :&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 27pt;TEXT-INDENT:-27pt;"&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;select orderid&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 27pt;TEXT-INDENT:-27pt;"&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;from orders as o&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 27pt;TEXT-INDENT:-27pt;"&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;join Customers as c&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 27pt;TEXT-INDENT:-27pt;"&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;on o.CustomerID = c.CustomerID&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 27pt;TEXT-INDENT:-27pt;"&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;where c.City = &amp;#39;Madrid&amp;#39;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt; 
&lt;p class="MsoNormal" style="TEXT-JUSTIFY:inter-ideograph;MARGIN:0cm 0cm 0pt 27pt;TEXT-INDENT:-27pt;TEXT-ALIGN:justify;"&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;Second example here is query returning freight and its average from all orders within same customer.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt; 
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 27pt;TEXT-INDENT:-27pt;"&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;select o1.OrderID, o1.Freight,&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 27pt;TEXT-INDENT:-27pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;(select AVG(freight) from orders as o2&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 27pt;TEXT-INDENT:-27pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;where o2.CustomerID = o1.CustomerID&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 27pt;TEXT-INDENT:-27pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;) as AverageFreight&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 27pt;TEXT-INDENT:-27pt;"&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;from dbo.orders as o1&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt; 
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 27pt;TEXT-INDENT:-27pt;"&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;Its execution plan is :&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt; 
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 27pt;TEXT-INDENT:-27pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;|--Compute Scalar(DEFINE:([Expr1006]=[Expr1004]))&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 27pt;TEXT-INDENT:-27pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Hash Match(Right Outer Join, HASH:([o2].[CustomerID])=([o1].[CustomerID]), RESIDUAL:([Northwind].[dbo].[Orders].[CustomerID] as [o2].[CustomerID]=[Northwind].[dbo].[Orders].[CustomerID] as [o1].[CustomerID]))&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 40.5pt;TEXT-INDENT:-40.5pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Compute Scalar(DEFINE:([Expr1004]=CASE WHEN [Expr1013]=(0) THEN NULL ELSE [Expr1014]/CONVERT_IMPLICIT(money,[Expr1013],0) END))&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 54pt;TEXT-INDENT:-54pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Stream Aggregate(GROUP BY:([o2].[CustomerID]) DEFINE:([Expr1013]=COUNT_BIG([Northwind].[dbo].[Orders].[Freight] as [o2].[Freight]), [Expr1014]=SUM([Northwind].[dbo].[Orders].[Freight] as [o2].[Freight])))&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 27pt;TEXT-INDENT:-27pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Sort(ORDER BY:([o2].[CustomerID] ASC))&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 27pt;TEXT-INDENT:-27pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Clustered Index Scan(OBJECT:([Northwind].[dbo].[Orders].[PK_Orders] AS [o2]))&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 27pt;TEXT-INDENT:-27pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Clustered Index Scan(OBJECT:([Northwind].[dbo].[Orders].[PK_Orders] AS [o1]))&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt; 
&lt;p class="MsoNormal" style="TEXT-JUSTIFY:inter-ideograph;MARGIN:0cm 0cm 0pt;LINE-HEIGHT:150%;TEXT-ALIGN:justify;"&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;The item immediately below Hash Match operator is scanning all orders and sort by customerid before they are computed for its average freight with grouping by CustomerId. To get freight column, optimizer decides to do clustered index scan, if you remove freight column, optimizer will choose index scan rather than clustered index scan, which is more efficient. There is possible that one customer may have more than one orders. For example, a customer has 5 orders. In this case optimizer will calculate average freight once for the customer, not 5 times for the customer. It is reflected with calculation of aggregation first before matched with the inner execution of clustered index scan of alias O1. The more orders for each customers, the more efficient execution plan, because it just calculates average value once for each customers.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt; 
&lt;p class="MsoNormal" style="TEXT-JUSTIFY:inter-ideograph;MARGIN:0cm 0cm 0pt;LINE-HEIGHT:150%;TEXT-ALIGN:justify;"&gt;&lt;span&gt;&lt;font face="Calibri" size="3"&gt;Most cases, noncorrelated subquery is more efficient than correlated subquery. If you have options to change correlated subquery into non correlated subquery, it is better to do so.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post: &lt;/strong&gt;&lt;a href="mailto:?body=Thought you might like this: http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/decorrelating-correlated-subquery.aspx&amp;amp;subject=Decorrelating Correlated Subquery" target="_blank" title="Send via email"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Mail.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.facebook.com/sharer.php?u=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/decorrelating-correlated-subquery.aspx&amp;amp;t=Decorrelating+Correlated+Subquery" target="_blank" title="Submit Decorrelating Correlated Subquery to DotNetKicks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Facebook.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://del.icio.us/post?url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/decorrelating-correlated-subquery.aspx&amp;amp;title=Decorrelating+Correlated+Subquery" target="_blank" title="Submit Decorrelating Correlated Subquery to del.icio.us"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Delicious.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.digg.com/submit?url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/decorrelating-correlated-subquery.aspx&amp;amp;phase=2" target="_blank" title="Submit Decorrelating Correlated Subquery to digg.com"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Digg.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/22/decorrelating-correlated-subquery.aspx&amp;amp;title=Decorrelating+Correlated+Subquery" target="_blank" title="Add Decorrelating Correlated Subquery to Live Bookmarks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Live.16.gif" border="0"&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://geeks.netindonesia.net/aggbug.aspx?PostID=53308" width="1" height="1"&gt;</description><category domain="http://geeks.netindonesia.net/blogs/kasim.wirama/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Skip Stored Procedure Recompilation</title><link>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/19/skip-stored-procedure-recompilation.aspx</link><pubDate>Thu, 19 Jun 2008 00:07:00 GMT</pubDate><guid isPermaLink="false">5cc3a90d-ac9a-472a-8983-30514957434c:52637</guid><dc:creator>Kasim.Wirama</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://geeks.netindonesia.net/blogs/kasim.wirama/rsscomments.aspx?PostID=52637</wfw:commentRss><comments>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/19/skip-stored-procedure-recompilation.aspx#comments</comments><description>&lt;h3 style="MARGIN:12pt 0cm 3pt;TEXT-ALIGN:center;" align="center"&gt;Skip Stored Procedure Recompilation&lt;/h3&gt;
&lt;h3 style="MARGIN:12pt 0cm 3pt;TEXT-ALIGN:center;" align="center"&gt;By : Kasim Wirama, MCDBA&lt;/h3&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;TEXT-ALIGN:justify;"&gt;&lt;font face="Times New Roman" size="3"&gt;Stored procedure (and also function) is preferable to be implemented in SQL Server because its compilation code is cached, so next execution will take the compilation form in cache. Imagine that you have a complex stored procedure and you found that it recompile for each time it is called through SP:Recompile and SQL:StmtRecompile. In this case SQL Server have some options that you might to consider to avoid the problematic stored procedure from being recompiled.&lt;/font&gt;&lt;/p&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;TEXT-ALIGN:justify;"&gt;&lt;font face="Times New Roman" size="3"&gt;If you want to make your query got cached in cache store, there is some ways to achieve it :&lt;/font&gt;&lt;/p&gt;
&lt;ol style="MARGIN-TOP:0cm;"&gt;
&lt;li class="MsoNormal" style="MARGIN:0cm 0cm 0pt;TEXT-ALIGN:justify;tab-stops:list 36.0pt;mso-list:l0 level1 lfo1;"&gt;&lt;font face="Times New Roman" size="3"&gt;make your tables which are involved in query to read only.&lt;/font&gt;&lt;/li&gt;
&lt;li class="MsoNormal" style="MARGIN:0cm 0cm 0pt;TEXT-ALIGN:justify;tab-stops:list 36.0pt;mso-list:l0 level1 lfo1;"&gt;&lt;font face="Times New Roman" size="3"&gt;turn off automatic update statistics, it is applicable when there is little database update activity that impacted on index update. But it is recommended that you turn on auto update statistics.&lt;/font&gt;&lt;/li&gt;
&lt;li class="MsoNormal" style="MARGIN:0cm 0cm 0pt;TEXT-ALIGN:justify;tab-stops:list 36.0pt;mso-list:l0 level1 lfo1;"&gt;&lt;font face="Times New Roman" size="3"&gt;if your column is unique, make it unique constraint or unique index and your select query use the column in WHERE clause.&lt;/font&gt;&lt;/li&gt;
&lt;li class="MsoNormal" style="MARGIN:0cm 0cm 0pt;TEXT-ALIGN:justify;tab-stops:list 36.0pt;mso-list:l0 level1 lfo1;"&gt;&lt;font face="Times New Roman" size="3"&gt;if your column is not unique, you can consider use query hints : Option (Keepfixed Plan), it is available on SQL Server 2005.&lt;/font&gt;&lt;/li&gt;&lt;/ol&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;TEXT-ALIGN:justify;"&gt;&lt;font face="Times New Roman" size="3"&gt;For overall recompilation monitoring for SQL Server, performance monitor give 2 relevant counters under performance object : MSSQLServer : SQL Statisctics, i.e. SQL Re-compilation/sec and SQL:Batch Requests/sec. If ratio between them is high, your SQL Server has recompilation issue that might degrade database performance.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post: &lt;/strong&gt;&lt;a href="mailto:?body=Thought you might like this: http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/19/skip-stored-procedure-recompilation.aspx&amp;amp;subject=Skip Stored Procedure Recompilation" target="_blank" title="Send via email"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Mail.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.facebook.com/sharer.php?u=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/19/skip-stored-procedure-recompilation.aspx&amp;amp;t=Skip+Stored+Procedure+Recompilation" target="_blank" title="Submit Skip Stored Procedure Recompilation to DotNetKicks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Facebook.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://del.icio.us/post?url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/19/skip-stored-procedure-recompilation.aspx&amp;amp;title=Skip+Stored+Procedure+Recompilation" target="_blank" title="Submit Skip Stored Procedure Recompilation to del.icio.us"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Delicious.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.digg.com/submit?url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/19/skip-stored-procedure-recompilation.aspx&amp;amp;phase=2" target="_blank" title="Submit Skip Stored Procedure Recompilation to digg.com"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Digg.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/19/skip-stored-procedure-recompilation.aspx&amp;amp;title=Skip+Stored+Procedure+Recompilation" target="_blank" title="Add Skip Stored Procedure Recompilation to Live Bookmarks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Live.16.gif" border="0"&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://geeks.netindonesia.net/aggbug.aspx?PostID=52637" width="1" height="1"&gt;</description><category domain="http://geeks.netindonesia.net/blogs/kasim.wirama/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Rebind and Rewind on Non Equal Correlated Subquery</title><link>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/17/rebind-and-rewind-on-non-equal-correlated-subquery.aspx</link><pubDate>Tue, 17 Jun 2008 09:42:00 GMT</pubDate><guid isPermaLink="false">5cc3a90d-ac9a-472a-8983-30514957434c:52429</guid><dc:creator>Kasim.Wirama</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://geeks.netindonesia.net/blogs/kasim.wirama/rsscomments.aspx?PostID=52429</wfw:commentRss><comments>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/17/rebind-and-rewind-on-non-equal-correlated-subquery.aspx#comments</comments><description>&lt;h3 style="MARGIN:12pt 0cm 3pt;TEXT-ALIGN:center;" align="center"&gt;Rebind and Rewind on Non Equal Correlated Subquery&lt;/h3&gt;
&lt;h3 style="MARGIN:12pt 0cm 3pt;TEXT-ALIGN:center;" align="center"&gt;By : Kasim Wirama, MCDBA&lt;/h3&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;TEXT-ALIGN:justify;"&gt;&lt;font face="Times New Roman" size="3"&gt;This posting, I would show other execution plan operator that has caching capability regarding to non correlated subquery that involved non equality relationship between inner and outer query.&lt;/font&gt;&lt;/p&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;TEXT-ALIGN:justify;"&gt;&lt;font face="Times New Roman" size="3"&gt;What is the behaviour difference for optimizer to generate plan between correlated and noncorrelated subquery? In noncorrelated subquery, inner query doesn’t depend on outer query thus optimizer compute inner query first then outer query, whereas in correlated subquery, optimizer compute every row in outer query then to inner query because inner query depends on value of outer query.&lt;/font&gt;&lt;/p&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;Example of nonequality-correlated subquery is :&lt;/font&gt;&lt;/p&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;SELECT&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;orderid&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;FROM orders AS o1&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;WHERE freight &amp;lt; &lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;(&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;"&gt;&lt;font size="3"&gt;&lt;font face="Times New Roman"&gt;&lt;span style="mso-tab-count:1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SELECT avg(freight)&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;"&gt;&lt;font size="3"&gt;&lt;font face="Times New Roman"&gt;&lt;span style="mso-tab-count:1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;FROM orders AS o2&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;"&gt;&lt;font size="3"&gt;&lt;font face="Times New Roman"&gt;&lt;span style="mso-tab-count:1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;WHERE o2.orderdate &amp;lt; o1.orderdate&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;);&lt;/font&gt;&lt;/p&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;"&gt;&lt;font face="Times New Roman" size="3"&gt;Execution plan for this query is :&lt;/font&gt;&lt;/p&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 18pt;TEXT-INDENT:-18pt;"&gt;&lt;font size="3"&gt;&lt;font face="Times New Roman"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;|--Filter(WHERE:([Northwind].[dbo].[Orders].[Freight] as [o1].[Freight]&amp;lt;[Expr1004]))&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt;"&gt;&lt;font size="3"&gt;&lt;font face="Times New Roman"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Nested Loops(Inner Join, OUTER REFERENCES:([o1].[OrderDate]))&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 45pt;TEXT-INDENT:-45pt;"&gt;&lt;font size="3"&gt;&lt;font face="Times New Roman"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Clustered Index Scan(OBJECT:([Northwind].[dbo].[Orders].[PK_Orders] AS [o1]))&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 45pt;TEXT-INDENT:-45pt;"&gt;&lt;font size="3"&gt;&lt;font face="Times New Roman"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--&lt;b style="mso-bidi-font-weight:normal;"&gt;Index Spool&lt;/b&gt; (SEEK:([o1].[OrderDate]=[Northwind].[dbo].[Orders]. [OrderDate] as [o1].[OrderDate]))&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 63pt;TEXT-INDENT:-63pt;"&gt;&lt;font size="3"&gt;&lt;font face="Times New Roman"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Compute Scalar(DEFINE:([Expr1004]=CASE WHEN [Expr1011]=(0) THEN NULL ELSE [Expr1012]/CONVERT_IMPLICIT(money,[Expr1011],0) END))&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 81pt;TEXT-INDENT:-81pt;"&gt;&lt;font size="3"&gt;&lt;font face="Times New Roman"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Stream Aggregate (DEFINE:([Expr1011]=COUNT_BIG ([Northwind].[dbo].[Orders].[Freight] as [o2].[Freight]), [Expr1012]=SUM([Northwind].[dbo].[Orders].[Freight] as [o2].[Freight])))&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 99pt;TEXT-INDENT:-99pt;"&gt;&lt;font size="3"&gt;&lt;font face="Times New Roman"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--&lt;b style="mso-bidi-font-weight:normal;"&gt;Index Spool&lt;/b&gt;(SEEK:([o2].[OrderDate] &amp;lt; [Northwind].[dbo]. [Orders].[OrderDate] as [o1].[OrderDate]))&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0cm 0cm 0pt 108pt;TEXT-INDENT:-108pt;"&gt;&lt;font size="3"&gt;&lt;font face="Times New Roman"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Clustered Index Scan (OBJECT:([Northwind].[dbo]. [Orders].[PK_Orders] AS [o2]))&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="TEXT-JUSTIFY:inter-ideograph;MARGIN:0in 0in 0pt;TEXT-ALIGN:justify;"&gt;&lt;span&gt;&lt;font face="Times New Roman" size="3"&gt;First index spool is lazy spool and second one is eager spool (look at logicalOp column). For first access into inner query, optimizer scan orders table then create eager index spool (second spool). Eager index spool is also called as index on the fly spool because it eagerly build temporary index on entire outer input on first access. On subsequent access from outer query, average aggregation is handled by computer scalar and stream aggregate operator and then it is cached by first index spool (lazy index spool). It is called lazy index spool, because it cache result from immediate previous execution. When next input row has the same orderdate, then the result is taken from lazy index spool only. This behaviour is called rewind. But if next input row has different orderdate, then the result is taken from accumulated value in lazy index spool or if it is not in lazy index spool, the operator request input from stream aggregate and computer scalar operator. Such behaviour is called rebind because it rebinds new value into lazy index spool. &lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;span&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt; 
&lt;p class="MsoNormal" style="TEXT-JUSTIFY:inter-ideograph;MARGIN:0in 0in 0pt;TEXT-ALIGN:justify;"&gt;&lt;span&gt;&lt;font face="Times New Roman" size="3"&gt;If you check number of rows of rebind and rewind at property of lazy index spool, their total will be equal to total number of rows in orders table.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;span&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt; 
&lt;p class="MsoNormal" style="TEXT-JUSTIFY:inter-ideograph;MARGIN:0in 0in 0pt;TEXT-ALIGN:justify;"&gt;&lt;span&gt;&lt;font face="Times New Roman" size="3"&gt;Optimizer tends to create index spool operator when there are many duplicate value on orderdate column and there are a lot of input rows from outer query. So if you reduce the number of input column, index spool operator will be disappear. Look at this example and its execution plan below :&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;span&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font face="Times New Roman" size="3"&gt;SELECT&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font face="Times New Roman" size="3"&gt;orderid&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font face="Times New Roman" size="3"&gt;FROM orders AS o1&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font face="Times New Roman" size="3"&gt;WHERE freight &amp;lt; &lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font face="Times New Roman" size="3"&gt;(&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Times New Roman"&gt;&lt;span style="mso-tab-count:1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SELECT avg(freight)&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Times New Roman"&gt;&lt;span style="mso-tab-count:1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;FROM orders AS o2&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Times New Roman"&gt;&lt;span style="mso-tab-count:1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;WHERE o2.orderdate &amp;lt; o1.orderdate&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font face="Times New Roman" size="3"&gt;)&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font face="Times New Roman" size="3"&gt;AND shipcity = &amp;#39;Caracas&amp;#39;;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;span&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font face="Times New Roman" size="3"&gt;Execution plan of the query is :&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;span&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 0pt 13.5pt;TEXT-INDENT:-13.5pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Times New Roman"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;|--Filter(WHERE:([Northwind].[dbo].[Orders].[Freight] as [o1].[Freight]&amp;lt;[Expr1004]))&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 0pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Times New Roman"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Nested Loops(Inner Join, OUTER REFERENCES:([o1].[OrderDate]))&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 0pt 45pt;TEXT-INDENT:-45pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Times New Roman"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Clustered Index Scan(OBJECT:([Northwind].[dbo].[Orders].[PK_Orders] AS [o1]), WHERE:([Northwind].[dbo].[Orders].[ShipCity] as [o1].[ShipCity]=N&amp;#39;Caracas&amp;#39;))&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 0pt 49.5pt;TEXT-INDENT:-49.5pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Times New Roman"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Compute Scalar(DEFINE:([Expr1004]=CASE WHEN [Expr1011]=(0) THEN NULL ELSE [Expr1012]/CONVERT_IMPLICIT(money,[Expr1011],0) END))&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 0pt 63pt;TEXT-INDENT:-63pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Times New Roman"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Stream Aggregate(DEFINE:([Expr1011]=COUNT_BIG([Northwind].[dbo].[Orders].[Freight] as [o2].[Freight]), [Expr1012]=SUM([Northwind].[dbo].[Orders].[Freight] as [o2].[Freight])))&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 0pt 76.5pt;TEXT-INDENT:-76.5pt;"&gt;&lt;span&gt;&lt;font size="3"&gt;&lt;font face="Times New Roman"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Clustered Index Scan(OBJECT:([Northwind].[dbo].[Orders].[PK_Orders] AS [o2]), WHERE:([Northwind].[dbo].[Orders].[OrderDate] as [o2].[OrderDate]&amp;lt;[Northwind].[dbo].[Orders].[OrderDate] as [o1].[OrderDate]))&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;&lt;span&gt;&lt;font face="Times New Roman" size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;/span&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 0pt 76.5pt;TEXT-INDENT:-76.5pt;"&gt;&lt;span&gt;&lt;font face="Times New Roman" size="3"&gt;Observe that there is no index spool operator.&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post: &lt;/strong&gt;&lt;a href="mailto:?body=Thought you might like this: http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/17/rebind-and-rewind-on-non-equal-correlated-subquery.aspx&amp;amp;subject=Rebind and Rewind on Non Equal Correlated Subquery" target="_blank" title="Send via email"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Mail.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.facebook.com/sharer.php?u=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/17/rebind-and-rewind-on-non-equal-correlated-subquery.aspx&amp;amp;t=Rebind+and+Rewind+on+Non+Equal+Correlated+Subquery" target="_blank" title="Submit Rebind and Rewind on Non Equal Correlated Subquery to DotNetKicks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Facebook.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://del.icio.us/post?url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/17/rebind-and-rewind-on-non-equal-correlated-subquery.aspx&amp;amp;title=Rebind+and+Rewind+on+Non+Equal+Correlated+Subquery" target="_blank" title="Submit Rebind and Rewind on Non Equal Correlated Subquery to del.icio.us"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Delicious.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.digg.com/submit?url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/17/rebind-and-rewind-on-non-equal-correlated-subquery.aspx&amp;amp;phase=2" target="_blank" title="Submit Rebind and Rewind on Non Equal Correlated Subquery to digg.com"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Digg.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/17/rebind-and-rewind-on-non-equal-correlated-subquery.aspx&amp;amp;title=Rebind+and+Rewind+on+Non+Equal+Correlated+Subquery" target="_blank" title="Add Rebind and Rewind on Non Equal Correlated Subquery to Live Bookmarks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Live.16.gif" border="0"&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://geeks.netindonesia.net/aggbug.aspx?PostID=52429" width="1" height="1"&gt;</description><category domain="http://geeks.netindonesia.net/blogs/kasim.wirama/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Execution Plan for Noncorrelated Query</title><link>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/17/execution-plan-for-noncorrelated-query.aspx</link><pubDate>Tue, 17 Jun 2008 09:01:00 GMT</pubDate><guid isPermaLink="false">5cc3a90d-ac9a-472a-8983-30514957434c:52428</guid><dc:creator>Kasim.Wirama</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://geeks.netindonesia.net/blogs/kasim.wirama/rsscomments.aspx?PostID=52428</wfw:commentRss><comments>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/17/execution-plan-for-noncorrelated-query.aspx#comments</comments><description>&lt;div class="Section1"&gt;
&lt;h3 style="TEXT-ALIGN:center;" align="center"&gt;Execution Plan for &lt;span class="SpellE"&gt;Noncorrelated&lt;/span&gt; Query&lt;/h3&gt;
&lt;h3 style="TEXT-ALIGN:center;" align="center"&gt;&lt;span class="GramE"&gt;By :&lt;/span&gt; &lt;span class="SpellE"&gt;Kasim&lt;/span&gt; &lt;span class="SpellE"&gt;Wirama&lt;/span&gt;, MCDBA&lt;/h3&gt;&amp;nbsp; 
&lt;p class="MsoNormal" style="TEXT-JUSTIFY:inter-ideograph;TEXT-ALIGN:justify;"&gt;This posting, I would like to show various possible execution &lt;span class="GramE"&gt;plan&lt;/span&gt; for non correlated query and possible action to rewrite query for better execution plan. Let’s run the query at &lt;span class="SpellE"&gt;Northwind&lt;/span&gt; database that displays list of &lt;span class="SpellE"&gt;orderid&lt;/span&gt; whose freight cost is under average freight cost of all orders&lt;/p&gt;&amp;nbsp; 
&lt;p class="MsoNormal"&gt;SELECT &lt;span class="SpellE"&gt;orderid&lt;/span&gt; FROM &lt;span class="SpellE"&gt;dbo.orders&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;WHERE freight &amp;lt; (SELECT &lt;span class="GramE"&gt;AVG(&lt;/span&gt;freight) FROM &lt;span class="SpellE"&gt;dbo.orders&lt;/span&gt;)&lt;/p&gt;&amp;nbsp; 
&lt;p class="MsoNormal"&gt;Execution plan for the non correlated and scalar query &lt;span class="GramE"&gt;is :&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;|--Nested &lt;span class="GramE"&gt;Loops(&lt;/span&gt;Inner Join, WHERE:([&lt;span class="SpellE"&gt;Northwind&lt;/span&gt;].[&lt;span class="SpellE"&gt;dbo&lt;/span&gt;].[Orders].[Freight]&amp;lt;[Expr1006]))&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Compute &lt;span class="GramE"&gt;Scalar(&lt;/span&gt;DEFINE:([Expr1006]=CASE WHEN [Expr1013]=(0)&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Stream &lt;span class="GramE"&gt;Aggregate(&lt;/span&gt;DEFINE:([Expr1013]=COUNT_BIG([|&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN-LEFT:36pt;TEXT-INDENT:36pt;"&gt;|--Clustered Index Scan (&lt;span class="GramE"&gt;OBJECT:&lt;/span&gt;([&lt;span class="SpellE"&gt;Northwind&lt;/span&gt;].[&lt;span class="SpellE"&gt;dbo&lt;/span&gt;]. &lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Clustered Index &lt;span class="GramE"&gt;Scan(&lt;/span&gt;OBJECT:([&lt;span class="SpellE"&gt;Northwind&lt;/span&gt;].[&lt;span class="SpellE"&gt;dbo&lt;/span&gt;].[Orders].[&lt;span class="SpellE"&gt;PK_Orders&lt;/span&gt;]))&lt;/p&gt;&amp;nbsp; 
&lt;p class="MsoNormal" style="TEXT-JUSTIFY:inter-ideograph;TEXT-ALIGN:justify;"&gt;The above execution plan calculate average value of freight and then verify all records in orders database which has freight value below average value of freight. It’s straightforward description from the execution plan. Now let’s look into another &lt;span class="SpellE"&gt;noncorrelated&lt;/span&gt; &lt;span class="SpellE"&gt;subquery&lt;/span&gt;&lt;/p&gt;&amp;nbsp; 
&lt;p class="MsoNormal"&gt;SELECT&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count:1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;orderid&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;FROM orders&lt;/p&gt;
&lt;p class="MsoNormal"&gt;WHERE &lt;span class="SpellE"&gt;customerid&lt;/span&gt; = &lt;/p&gt;
&lt;p class="MsoNormal"&gt;(&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count:1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SELECT &lt;span class="SpellE"&gt;customerid&lt;/span&gt; &lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count:1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;FROM customers &lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count:1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;WHERE &lt;span class="SpellE"&gt;contactname&lt;/span&gt; = &amp;#39;Antonio Moreno&amp;#39;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;);&lt;/p&gt;&amp;nbsp; 
&lt;p class="MsoNormal"&gt;Execution plan for the query &lt;span class="GramE"&gt;is :&lt;/span&gt;&lt;/p&gt;&amp;nbsp; 
&lt;p class="MsoNormal"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;|--Nested &lt;span class="GramE"&gt;Loops(&lt;/span&gt;Inner Join, OUTER REFERENCES:([Expr1008]))&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--&lt;span class="GramE"&gt;Assert(&lt;/span&gt;WHERE:(CASE WHEN [Expr1007]&amp;gt;(1) THEN (0) &lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN-LEFT:45pt;TEXT-INDENT:-45pt;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Stream Aggregate(DEFINE:([Expr1007]=Count(*), [Expr1008]=&lt;b style="mso-bidi-font-weight:normal;"&gt;ANY&lt;/b&gt;([&lt;span class="SpellE"&gt;Northwind&lt;/span&gt;].[&lt;span class="SpellE"&gt;dbo&lt;/span&gt;].[Customers].[&lt;span class="SpellE"&gt;CustomerID&lt;/span&gt;])))&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN-LEFT:63pt;TEXT-INDENT:-63pt;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Clustered Index Scan(OBJECT:([&lt;span class="SpellE"&gt;Northwind&lt;/span&gt;].[&lt;span class="SpellE"&gt;dbo&lt;/span&gt;].[Customer WHERE:([&lt;span class="SpellE"&gt;Northwind&lt;/span&gt;].[&lt;span class="SpellE"&gt;dbo&lt;/span&gt;].[Customers].[&lt;span class="SpellE"&gt;ContactName&lt;/span&gt;]=&lt;span class="SpellE"&gt;N&amp;#39;Antonio&lt;/span&gt; Moreno&amp;#39;))&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN-LEFT:36pt;TEXT-INDENT:-36pt;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Index Seek(OBJECT:([&lt;span class="SpellE"&gt;Northwind&lt;/span&gt;].[&lt;span class="SpellE"&gt;dbo&lt;/span&gt;].[Orders].[&lt;span class="SpellE"&gt;CustomerID&lt;/span&gt;]),&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SEEK:([&lt;span class="SpellE"&gt;Northwind&lt;/span&gt;].[&lt;span class="SpellE"&gt;dbo&lt;/span&gt;].[Orders].[&lt;span class="SpellE"&gt;CustomerID&lt;/span&gt;]=[Expr1008]) ORDERED FORWARD)&lt;/p&gt;&amp;nbsp; 
&lt;p class="MsoNormal" style="TEXT-JUSTIFY:inter-ideograph;TEXT-ALIGN:justify;"&gt;The query above could probably returns error because inner query might return non scalar value (more than 1 row) and &lt;span class="SpellE"&gt;contactname&lt;/span&gt; is not unique, so probably 1 &lt;span class="SpellE"&gt;contactname&lt;/span&gt; serves several &lt;span class="SpellE"&gt;customerid&lt;/span&gt;. The way optimizer checking whether inner query returns more than 1 &lt;span class="GramE"&gt;rows&lt;/span&gt; is by adding ASSERT operator. If it is true, then the query raises error.&lt;span style="mso-tab-count:1;"&gt; &lt;/span&gt;&lt;/p&gt;&amp;nbsp; 
&lt;p class="MsoNormal" style="TEXT-JUSTIFY:inter-ideograph;TEXT-ALIGN:justify;"&gt;Another noticeable operator is &lt;b style="mso-bidi-font-weight:normal;"&gt;ANY&lt;/b&gt; operator. It is internal operator that counts the number of record for each &lt;span class="SpellE"&gt;customerID&lt;/span&gt;. But because Stream Aggregate expects &lt;span class="SpellE"&gt;customerid&lt;/span&gt; to be aggregated or to be put in GROUP BY clause, so it requires &lt;b style="mso-bidi-font-weight:normal;"&gt;ANY &lt;/b&gt;as internal aggregate operator, so the query :&lt;/p&gt;&amp;nbsp; 
&lt;p class="MsoNormal"&gt;SELECT &lt;span class="GramE"&gt;COUNT(&lt;/span&gt;*), &lt;span class="SpellE"&gt;customerid&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;FROM customers&lt;/p&gt;
&lt;p class="MsoNormal"&gt;WHERE &lt;span class="SpellE"&gt;contactname&lt;/span&gt; = &amp;#39;&lt;span class="SpellE"&gt;antonio&lt;/span&gt; &lt;span class="SpellE"&gt;moreno&lt;/span&gt;&amp;#39;&lt;/p&gt;&amp;nbsp; 
&lt;p class="MsoNormal"&gt;&lt;span class="GramE"&gt;is&lt;/span&gt; not parsed by optimizer except you define GROUP BY for &lt;span class="SpellE"&gt;customerid&lt;/span&gt;.&lt;/p&gt;&amp;nbsp; 
&lt;p class="MsoNormal"&gt;As long as, you are aware that &lt;span class="SpellE"&gt;contactname&lt;/span&gt; could have more than one &lt;span class="SpellE"&gt;customerid&lt;/span&gt;, the following query is valid and it is considered valid to previous non correlated &lt;span class="GramE"&gt;query :&lt;/span&gt;&lt;/p&gt;&amp;nbsp; 
&lt;p class="MsoNormal"&gt;SELECT&lt;/p&gt;
&lt;p class="MsoNormal"&gt;&lt;span style="mso-tab-count:1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span class="SpellE"&gt;&lt;span class="GramE"&gt;orderid&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;FROM orders JOIN customers&lt;/p&gt;
&lt;p class="MsoNormal"&gt;ON &lt;span class="SpellE"&gt;orders.customerid&lt;/span&gt; = &lt;span class="SpellE"&gt;customers.customerid&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal"&gt;WHERE &lt;span class="SpellE"&gt;customers.contactname&lt;/span&gt; = &amp;#39;Antonio Moreno&amp;#39;&lt;/p&gt;&amp;nbsp; 
&lt;p class="MsoNormal"&gt;Execution plan &lt;span class="GramE"&gt;is :&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN-LEFT:27pt;TEXT-INDENT:-27pt;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;|--Nested &lt;span class="GramE"&gt;Loops(&lt;/span&gt;Inner Join, OUTER REFERENCES:([&lt;span class="SpellE"&gt;Northwind&lt;/span&gt;].[&lt;span class="SpellE"&gt;dbo&lt;/span&gt;].[Customers].[&lt;span class="SpellE"&gt;CustomerID&lt;/span&gt;]))&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN-LEFT:36pt;TEXT-INDENT:-36pt;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Clustered Index Scan(OBJECT:([&lt;span class="SpellE"&gt;Northwind&lt;/span&gt;].[&lt;span class="SpellE"&gt;dbo&lt;/span&gt;].[Customers].[&lt;span class="SpellE"&gt;PK_Customers&lt;/span&gt;]), WHERE:([&lt;span class="SpellE"&gt;Northwind&lt;/span&gt;].[&lt;span class="SpellE"&gt;dbo&lt;/span&gt;].[Customers].[&lt;span class="SpellE"&gt;ContactName&lt;/span&gt;]=&lt;span class="SpellE"&gt;N&amp;#39;Antonio&lt;/span&gt; Moreno&amp;#39;))&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN-LEFT:36pt;TEXT-INDENT:-36pt;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Index Seek(OBJECT:([&lt;span class="SpellE"&gt;Northwind&lt;/span&gt;].[&lt;span class="SpellE"&gt;dbo&lt;/span&gt;].[Orders].[&lt;span class="SpellE"&gt;CustomerID&lt;/span&gt;]), SEEK:([&lt;span class="SpellE"&gt;Northwind&lt;/span&gt;].[&lt;span class="SpellE"&gt;dbo&lt;/span&gt;].[Orders].[&lt;span class="SpellE"&gt;CustomerID&lt;/span&gt;]=[&lt;span class="SpellE"&gt;Northwind&lt;/span&gt;].[&lt;span class="SpellE"&gt;dbo&lt;/span&gt;].[Customers].[&lt;span class="SpellE"&gt;CustomerID&lt;/span&gt;]) ORDERED FORWARD)&lt;/p&gt;&amp;nbsp; 
&lt;p class="MsoNormal" style="MARGIN-LEFT:36pt;TEXT-INDENT:-36pt;"&gt;This execution plan is simpler than previous one.&lt;/p&gt;&amp;nbsp; 
&lt;p class="MsoNormal" style="TEXT-JUSTIFY:inter-ideograph;TEXT-ALIGN:justify;"&gt;Now create unique index on &lt;span class="SpellE"&gt;contactname&lt;/span&gt; column at customers table and execute previous non correlated &lt;span class="SpellE"&gt;subquery&lt;/span&gt;, the execution plan &lt;span class="GramE"&gt;is :&lt;/span&gt;&lt;/p&gt;&amp;nbsp; 
&lt;p class="MsoNormal" style="MARGIN-LEFT:18pt;TEXT-INDENT:-18pt;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;|--Nested &lt;span class="GramE"&gt;Loops(&lt;/span&gt;Inner Join, OUTER REFERENCES:([&lt;span class="SpellE"&gt;Northwind&lt;/span&gt;].[&lt;span class="SpellE"&gt;dbo&lt;/span&gt;].[Customers].[&lt;span class="SpellE"&gt;CustomerID&lt;/span&gt;]))&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN-LEFT:36pt;TEXT-INDENT:-36pt;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Index Seek(OBJECT:([&lt;span class="SpellE"&gt;Northwind&lt;/span&gt;].[&lt;span class="SpellE"&gt;dbo&lt;/span&gt;].[Customers].[ix_1]), SEEK:([&lt;span class="SpellE"&gt;Northwind&lt;/span&gt;].[&lt;span class="SpellE"&gt;dbo&lt;/span&gt;].[Customers].[&lt;span class="SpellE"&gt;ContactName&lt;/span&gt;]=&lt;span class="SpellE"&gt;N&amp;#39;Antonio&lt;/span&gt; Moreno&amp;#39;) ORDERED FORWARD)&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN-LEFT:36pt;TEXT-INDENT:-36pt;"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Index Seek(OBJECT:([&lt;span class="SpellE"&gt;Northwind&lt;/span&gt;].[&lt;span class="SpellE"&gt;dbo&lt;/span&gt;].[Orders].[&lt;span class="SpellE"&gt;CustomerID&lt;/span&gt;]), SEEK:([&lt;span class="SpellE"&gt;Northwind&lt;/span&gt;].[&lt;span class="SpellE"&gt;dbo&lt;/span&gt;].[Orders].[&lt;span class="SpellE"&gt;CustomerID&lt;/span&gt;]=[&lt;span class="SpellE"&gt;Northwind&lt;/span&gt;].[&lt;span class="SpellE"&gt;dbo&lt;/span&gt;].[Customers].[&lt;span class="SpellE"&gt;CustomerID&lt;/span&gt;]) ORDERED FORWARD)&lt;/p&gt;&amp;nbsp; 
&lt;p class="MsoNormal" style="TEXT-JUSTIFY:inter-ideograph;TEXT-ALIGN:justify;"&gt;For this case, proper index selection will remove some operator and execution plan will be simpler and more efficient because I tell optimizer that there is only one &lt;span class="SpellE"&gt;contactname&lt;/span&gt; for each &lt;span class="SpellE"&gt;customerid&lt;/span&gt;, so optimizer decides to choose index seek to get customer record with &lt;span class="SpellE"&gt;contactName&lt;/span&gt; = ‘Antonio Moreno’ and optimizer stops searching to other &lt;span class="SpellE"&gt;customerid&lt;/span&gt; with same &lt;span class="SpellE"&gt;contactName&lt;/span&gt;.&lt;/p&gt;&lt;/div&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post: &lt;/strong&gt;&lt;a href="mailto:?body=Thought you might like this: http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/17/execution-plan-for-noncorrelated-query.aspx&amp;amp;subject=Execution Plan for Noncorrelated Query" target="_blank" title="Send via email"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Mail.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.facebook.com/sharer.php?u=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/17/execution-plan-for-noncorrelated-query.aspx&amp;amp;t=Execution+Plan+for+Noncorrelated+Query" target="_blank" title="Submit Execution Plan for Noncorrelated Query to DotNetKicks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Facebook.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://del.icio.us/post?url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/17/execution-plan-for-noncorrelated-query.aspx&amp;amp;title=Execution+Plan+for+Noncorrelated+Query" target="_blank" title="Submit Execution Plan for Noncorrelated Query to del.icio.us"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Delicious.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.digg.com/submit?url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/17/execution-plan-for-noncorrelated-query.aspx&amp;amp;phase=2" target="_blank" title="Submit Execution Plan for Noncorrelated Query to digg.com"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Digg.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/17/execution-plan-for-noncorrelated-query.aspx&amp;amp;title=Execution+Plan+for+Noncorrelated+Query" target="_blank" title="Add Execution Plan for Noncorrelated Query to Live Bookmarks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Live.16.gif" border="0"&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://geeks.netindonesia.net/aggbug.aspx?PostID=52428" width="1" height="1"&gt;</description><category domain="http://geeks.netindonesia.net/blogs/kasim.wirama/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Index Union Analysis</title><link>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/15/index-union-analysis.aspx</link><pubDate>Sun, 15 Jun 2008 12:22:00 GMT</pubDate><guid isPermaLink="false">5cc3a90d-ac9a-472a-8983-30514957434c:52265</guid><dc:creator>Kasim.Wirama</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://geeks.netindonesia.net/blogs/kasim.wirama/rsscomments.aspx?PostID=52265</wfw:commentRss><comments>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/15/index-union-analysis.aspx#comments</comments><description>&lt;h2 style="MARGIN:10pt 0in 0pt;TEXT-ALIGN:center;" align="center"&gt;&lt;font face="Cambria" color="#4f81bd" size="4"&gt;Index Union Analysis&lt;/font&gt;&lt;/h2&gt;
&lt;h4 style="MARGIN:10pt 0in 0pt;TEXT-ALIGN:center;" align="center"&gt;&lt;em&gt;&lt;font face="Cambria" color="#4f81bd"&gt;By : Kasim Wirama, MCDBA&lt;/font&gt;&lt;/em&gt;&lt;/h4&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;When there are 2 or more where criteria which are joined with OR, each columns is different among others, they are indexed and search selectivity is high, optimizer could consider multiple indexes and join the end result with concatenation or merge join concatenation operator. Such a pattern of execution plan is called index union. Let’s explore what possibilities of execution plan generated from index union of a query statement.&lt;/font&gt;&lt;/p&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;Here is the query to get any ordered which is shipped or created between 1 January 2007 and 7 January 2007.&lt;/font&gt;&lt;/p&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;SELECT orderid&lt;br /&gt;FROM orders&lt;br /&gt;WHERE shippeddate BETWEEN &amp;#39;19970101&amp;#39; AND &amp;#39;19970107&amp;#39;&lt;br /&gt;OR orderdate BETWEEN &amp;#39;19970101&amp;#39; AND &amp;#39;19970107&amp;#39;;&lt;/font&gt;&lt;/p&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;Because there is index for shippeddate and orderdate column respectively, optimizer decides to use each indexes to get result from shippeddate BETWEEN &amp;#39;19970101&amp;#39; AND &amp;#39;19970107&amp;#39; and orderdate BETWEEN &amp;#39;19970101&amp;#39; AND &amp;#39;19970107&amp;#39;. Execution plan for the query is :&lt;/font&gt;&lt;/p&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;|--&lt;b style="mso-bidi-font-weight:normal;"&gt;Sort&lt;/b&gt;(DISTINCT ORDER BY:([Northwind].[dbo].[Orders].[OrderID] ASC))&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--&lt;b style="mso-bidi-font-weight:normal;"&gt;Concatenation&lt;/b&gt;&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--&lt;b style="mso-bidi-font-weight:normal;"&gt;Index Seek&lt;/b&gt;(OBJECT:([Northwind].[dbo].[Orders].[ShippedDate]), SEEK:([Northwind].[dbo].[Orders].[ShippedDate] &amp;gt;= 1997-01-01 00:00:00.000&amp;#39; AND [Northwind].[dbo].[Orders].[ShippedDate] &amp;lt;= 1997-01-07 00:00:00.000&amp;#39;) ORDERED FORWARD)&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--&lt;b style="mso-bidi-font-weight:normal;"&gt;Index Seek&lt;/b&gt;(OBJECT:([Northwind].[dbo].[Orders].[OrderDate]), SEEK:([Northwind].[dbo].[Orders].[OrderDate] &amp;gt;= 1997-01-01 00:00:00.000&amp;#39; AND [Northwind].[dbo].[Orders].[OrderDate] &amp;lt;= 1997-01-07 00:00:00.000&amp;#39;) ORDERED FORWARD)&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;Notice that result from 2 index seeks is joined with concatenation operator. Because the result might be duplicate, optimizer implement SORT DISTINCT operator to remove duplication. The query above could be rewritten as :&lt;/font&gt;&lt;/p&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;SELECT ordered&lt;br /&gt;FROM orders&lt;br /&gt;WHERE shippeddate BETWEEN &amp;#39;19970101&amp;#39; AND &amp;#39;19970107&amp;#39;&lt;br /&gt;UNION&lt;br /&gt;SELECT ordered&lt;br /&gt;FROM orders&lt;br /&gt;WHERE orderdate BETWEEN &amp;#39;19970101&amp;#39; AND &amp;#39;19970107&amp;#39;;&lt;/font&gt;&lt;/p&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;Query rewrite above is valid because orderid is primary key which is also considered as unique key.&lt;/font&gt;&lt;/p&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;Now consider another query below :&lt;/font&gt;&lt;/p&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;SELECT orderid&lt;br /&gt;FROM orders&lt;br /&gt;WHERE shippeddate = &amp;#39;19970101&amp;#39; OR orderdate = &amp;#39;19970101&amp;#39;;&lt;/font&gt;&lt;/p&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;Query plan for the query is :&lt;/font&gt;&lt;/p&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;|--Nested Loops(Inner Join, OUTER REFERENCES:([Northwind].[dbo].[Orders].[OrderID]))&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--&lt;b style="mso-bidi-font-weight:normal;"&gt;Stream Aggregate&lt;/b&gt;(GROUP BY:([Northwind].[dbo].[Orders].[OrderID]))&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--&lt;b style="mso-bidi-font-weight:normal;"&gt;Merge Join(Concatenation)&lt;/b&gt;&lt;/font&gt;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Index Seek(OBJECT:([Northwind].[dbo].[Orders].[ShippedDate]), SEEK:([Northwind].[dbo].[Orders].[ShippedDate]=&amp;#39;1997-01-01 00:00:00.000&amp;#39;) ORDERED FORWARD)&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Index Seek(OBJECT:([Northwind].[dbo].[Orders].[OrderDate]), SEEK:([Northwind].[dbo].[Orders].[OrderDate]=&amp;#39;1997-01-01 00:00:00.000&amp;#39;) ORDERED FORWARD)&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Clustered Index Seek(OBJECT:([Northwind].[dbo].[Orders].[PK_Orders]), SEEK:([Northwind].[dbo].[Orders].[OrderID]=[Northwind].[dbo].[Orders].[OrderID]) LOOKUP ORDERED FORWARD)&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;Notice that stream aggregate and merge join concatenation appears instead of concatenation and SORT DISTINCT operator. Why does optimizer choose merge join and stream aggregate? It’s because that the query uses equality instead of inequality. Recall that merge operator will probably appear when a predicate contains at least one equality operator. From efficiency of resource utilization perspective, merge join and stream aggregate is better than sort operator because they do not require memory rather than SORT operator that might introduce data spill out to tempdb database when memory is under pressure.&lt;/font&gt;&lt;/p&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;The query above could be written as :&lt;/font&gt;&lt;/p&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;SELECT orderid &lt;br /&gt;FROM orders&lt;br /&gt;WHERE shippeddate = &amp;#39;19970101&amp;#39; &lt;br /&gt;UNION&lt;br /&gt;SELECT orderid &lt;br /&gt;FROM orders&lt;br /&gt;WHERE orderdate = &amp;#39;19970101&amp;#39;;&lt;/font&gt;&lt;/p&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;Query plan for this query is :&lt;/font&gt;&lt;/p&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp; &lt;/span&gt;|--&lt;b style="mso-bidi-font-weight:normal;"&gt;Merge Join(Union)&lt;/b&gt;&lt;/font&gt;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Index Seek(OBJECT:([Northwind].[dbo].[Orders].[ShippedDate]), SEEK:([Northwind].[dbo].[Orders].[ShippedDate]=&amp;#39;1997-01-01 00:00:00.000&amp;#39;) ORDERED FORWARD)&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font size="3"&gt;&lt;font face="Calibri"&gt;&lt;span style="mso-spacerun:yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;|--Index Seek(OBJECT:([Northwind].[dbo].[Orders].[OrderDate]), SEEK:([Northwind].[dbo].[Orders].[OrderDate]=&amp;#39;1997-01-01 00:00:00.000&amp;#39;) ORDERED FORWARD)&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;Merge join (Union) operator replaces Stream Aggregate and Merge join (concatenation).&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;"&gt;&lt;font face="Calibri" size="3"&gt;Query rewriting is always generated from query plan analysis, and the process is fun and rewarding.&lt;/font&gt;&lt;/p&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post: &lt;/strong&gt;&lt;a href="mailto:?body=Thought you might like this: http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/15/index-union-analysis.aspx&amp;amp;subject=Index Union Analysis" target="_blank" title="Send via email"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Mail.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.facebook.com/sharer.php?u=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/15/index-union-analysis.aspx&amp;amp;t=Index+Union+Analysis" target="_blank" title="Submit Index Union Analysis to DotNetKicks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Facebook.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://del.icio.us/post?url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/15/index-union-analysis.aspx&amp;amp;title=Index+Union+Analysis" target="_blank" title="Submit Index Union Analysis to del.icio.us"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Delicious.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.digg.com/submit?url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/15/index-union-analysis.aspx&amp;amp;phase=2" target="_blank" title="Submit Index Union Analysis to digg.com"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Digg.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/15/index-union-analysis.aspx&amp;amp;title=Index+Union+Analysis" target="_blank" title="Add Index Union Analysis to Live Bookmarks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Live.16.gif" border="0"&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://geeks.netindonesia.net/aggbug.aspx?PostID=52265" width="1" height="1"&gt;</description><category domain="http://geeks.netindonesia.net/blogs/kasim.wirama/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Peer-to-Peer Replication in SQL Server 2008</title><link>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/15/peer-to-peer-replication-in-sql-server-2008.aspx</link><pubDate>Sun, 15 Jun 2008 11:23:00 GMT</pubDate><guid isPermaLink="false">5cc3a90d-ac9a-472a-8983-30514957434c:52261</guid><dc:creator>Kasim.Wirama</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://geeks.netindonesia.net/blogs/kasim.wirama/rsscomments.aspx?PostID=52261</wfw:commentRss><comments>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/15/peer-to-peer-replication-in-sql-server-2008.aspx#comments</comments><description>&lt;h2 style="MARGIN:10pt 0in 0pt;TEXT-ALIGN:center;" align="center"&gt;&lt;font face="Cambria" color="#4f81bd" size="4"&gt;Peer-to-Peer Replication in SQL Server 2008&lt;/font&gt;&lt;/h2&gt;
&lt;h4 style="MARGIN:10pt 0in 0pt;TEXT-ALIGN:center;" align="center"&gt;&lt;em&gt;&lt;font face="Cambria" color="#4f81bd"&gt;By : Kasim Wirama, MCDBA&lt;/font&gt;&lt;/em&gt;&lt;/h4&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;TEXT-ALIGN:justify;"&gt;&lt;font face="Calibri" size="3"&gt;One of interesting enhancements made in SQL Server 2008 February CTP is in area of replication, peer to peer replication particularly. Now with SQL Server 2008 February CTP, a SQL Server 2008 new node could join to existing peer-to-peer existing topology without having to stop database activity on the topology, and also visual designer is introduced in this upcoming SQL Server version. With visual designer, I just only to visually design, implement and review peer-to-peer easily especially when I have more than 3 nodes to be implemented, which are more complex peer-to-peer topology.&lt;/font&gt;&lt;/p&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;TEXT-ALIGN:justify;"&gt;&lt;font face="Calibri" size="3"&gt;Peer-to-peer replication is located under transactional replication. By default, transactional replication turns off peer-to-peer replication. After you have set up publication with transactional replication, you need to open the publication property, and set value to true for enable peer-to-peer replication. Once you set it to true, you couldn’t revert back to transactional replication (by setting the value from true to false), the only way is to re-create the publication. Not like with other type of replication, peer-to-peer replication doesn’t need snapshot publication, uniqueidentifier column and trigger created on underlying table of an article. It doesn’t support re-initialization and timestamp type column because timestamp column is generated automatically by SQL Server so it couldn’t replicate to its peer. Another restriction of peer-to-peer replication is that it doesn’t support horizontal and vertical filtering. Besides that, peer-to-peer replication is not designed to handle conflict resolution even though it is able to able to detect conflict by turning on conflict detection. When conflict happens, data would not replicate to its peer node, thus manual intervention is needed to manually resolve conflict resolution. If you would like to automate conflict resolution with possible sophisticated scenario, merge transactional replication is the best replication option rather than peer-to-peer replication.&lt;/font&gt;&lt;/p&gt;&lt;font face="Calibri" size="3"&gt;&amp;nbsp;&lt;/font&gt; 
&lt;p class="MsoNormal" style="MARGIN:0in 0in 10pt;TEXT-ALIGN:justify;"&gt;&lt;font face="Calibri" size="3"&gt;The least performance impact is belonged to peer-to-peer replication, and for high availability, better fault tolerance, and read performance enhancement in OLTP scenario, it is one of the viable options besides log shipping, and database mirroring.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post: &lt;/strong&gt;&lt;a href="mailto:?body=Thought you might like this: http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/15/peer-to-peer-replication-in-sql-server-2008.aspx&amp;amp;subject=Peer-to-Peer Replication in SQL Server 2008" target="_blank" title="Send via email"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Mail.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.facebook.com/sharer.php?u=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/15/peer-to-peer-replication-in-sql-server-2008.aspx&amp;amp;t=Peer-to-Peer+Replication+in+SQL+Server+2008" target="_blank" title="Submit Peer-to-Peer Replication in SQL Server 2008 to DotNetKicks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Facebook.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://del.icio.us/post?url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/15/peer-to-peer-replication-in-sql-server-2008.aspx&amp;amp;title=Peer-to-Peer+Replication+in+SQL+Server+2008" target="_blank" title="Submit Peer-to-Peer Replication in SQL Server 2008 to del.icio.us"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Delicious.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="http://www.digg.com/submit?url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/15/peer-to-peer-replication-in-sql-server-2008.aspx&amp;amp;phase=2" target="_blank" title="Submit Peer-to-Peer Replication in SQL Server 2008 to digg.com"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Digg.16.gif" border="0"&gt;&lt;/a&gt; | &lt;a href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/15/peer-to-peer-replication-in-sql-server-2008.aspx&amp;amp;title=Peer-to-Peer+Replication+in+SQL+Server+2008" target="_blank" title="Add Peer-to-Peer Replication in SQL Server 2008 to Live Bookmarks"&gt;&lt;img src="http://geeks.netindonesia.net/Images/Share.Live.16.gif" border="0"&gt;&lt;/a&gt;&lt;/div&gt;&lt;img src="http://geeks.netindonesia.net/aggbug.aspx?PostID=52261" width="1" height="1"&gt;</description><category domain="http://geeks.netindonesia.net/blogs/kasim.wirama/archive/tags/SQL+Server/default.aspx">SQL Server</category></item><item><title>Dynamic Index Seek Review</title><link>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/15/dynamic-index-seek-review.aspx</link><pubDate>Sun, 15 Jun 2008 10:39:00 GMT</pubDate><guid isPermaLink="false">5cc3a90d-ac9a-472a-8983-30514957434c:52259</guid><dc:creator>Kasim.Wirama</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://geeks.netindonesia.net/blogs/kasim.wirama/rsscomments.aspx?PostID=52259</wfw:commentRss><comments>http://geeks.netindonesia.net/blogs/kasim.wirama/archive/2008/06/15/dynamic-index-seek-review.aspx#comments</comments><description>












&lt;div class="Section1"&gt;

&lt;h2 align="center" style="text-align:center;"&gt;Dynamic Index Seek Review&lt;/h2&gt;

&lt;h4 align="center" style="text-align:center;"&gt;By : Kasim Wirama, MCDBA&lt;/h4&gt;

&lt;p class="MsoNormal"&gt;&amp;nbsp;&lt;/p&gt;

&lt;p class="MsoNormal" style="text-align:justify;"&gt;Other execution plan pattern that
is noticeable is dynamic index seek. A plan is called dynamic index seek when
the optimizer doesn’t know variable values at compile time, and the plan
generates index seek operator and merge interval operator. Merge interval
operator? It sounds like uncommon operator. I would like to describe more about
the operator with following query example.&lt;/p&gt;

&lt;p class="MsoNormal"&gt;See the execution plan for this query at Northwind database
:&lt;/p&gt;

&lt;p class="MsoNormal"&gt;SELECT ordered&lt;br /&gt;
FROM orders&lt;br /&gt;
WHERE shippostalcode IN (&amp;#39;02389-673&amp;#39;,&amp;#39;01307&amp;#39;)&lt;/p&gt;

&lt;p class="MsoNormal"&gt;Its execution plan is :&lt;/p&gt;

&lt;p class="MsoNormal"&gt;SELECT orderid  FROM orders  WHERE shippostalcode IN
(&amp;#39;02389-673&amp;#39;,&amp;#39;01307&amp;#39;)&lt;br /&gt;
  |--Index Seek(OBJECT:([Northwind].[dbo].[Orders].[ShipPostalCode]),
SEEK:([Northwind].[dbo].[Orders].[ShipPostalCode]=N&amp;#39;01307&amp;#39; OR
[Northwind].[dbo].[Orders].[ShipPostalCode]=N&amp;#39;02389-673&amp;#39;) ORDERED FORWARD)&lt;/p&gt;

&lt;p class="MsoNormal"&gt;Because the index is highly selective, optimizer choose
index seek. Now compare execution on the similar query but parameterized one below
:&lt;/p&gt;

&lt;p class="MsoNormal"&gt;DECLARE @var1 nvarchar(20), @var2 nvarchar(20);&lt;br /&gt;
SELECT @var1=&amp;#39;02389-673&amp;#39;, @var2=&amp;#39;01307&amp;#39;;&lt;br /&gt;
SELECT ordered&lt;br /&gt;
FROM orders&lt;br /&gt;
WHERE shippostalcode IN (@var1,@var2);&lt;/p&gt;

&lt;p class="MsoNormal"&gt;The execution plan is :&lt;/p&gt;

&lt;p class="MsoNormal"&gt;SELECT orderid  FROM orders  WHERE shippostalcode IN
(@var1,@var2);&lt;/p&gt;

&lt;p class="MsoNormal"&gt;  |--Nested Loops(Inner Join, OUTER REFERENCES:([Expr1009],
[Expr1010], [Expr1011]))&lt;/p&gt;

&lt;p class="MsoNormal"&gt;       |--&lt;b&gt;Merge Interval&lt;/b&gt;&lt;/p&gt;

&lt;p class="MsoNormal"&gt;       |    |--Sort(TOP 2, ORDER BY:([Expr1012] DESC,
[Expr1013] ASC, [Expr1009] ASC, [Expr1014] DESC))&lt;/p&gt;

&lt;p class="MsoNormal"&gt;       |         |--Compute
Scalar(DEFINE:([Expr1012]=((4)&amp;amp;[Expr1011]) = (4) AND NULL = [Expr1009],
[Expr1013]=(4)&amp;amp;[Expr1011], [Expr1014]=(16)&amp;amp;[Expr1011]))&lt;/p&gt;

&lt;p class="MsoNormal"&gt;       |              |--Concatenation&lt;/p&gt;

&lt;p class="MsoNormal"&gt;       |                   |--Compute
Scalar(DEFINE:([@var2]=[@var2], [@var2]=[@var2], [Expr1003]=(62)))&lt;/p&gt;

&lt;p class="MsoNormal"&gt;       |                   |    |--Constant Scan&lt;/p&gt;