<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="https://www.w3.org/2005/Atom" xmlns:sy="https://purl.org/rss/1.0/modules/syndication/" xmlns:dc="https://purl.org/dc/elements/1.1/" xmlns:content="https://purl.org/rss/1.0/modules/content/" xmlns:wfw="https://wellformedweb.org/CommentAPI/" xmlns:slash="https://purl.org/rss/1.0/modules/slash/"
  version="2.0">
  <channel>
    <title>SQL With A Plan</title>
    <atom:link
      href="https://docs.microsoft.com/archive/blogs/mikecha/feed.xml"
      rel="self"
      type="application/rss+xml" />
    <link>https://docs.microsoft.com/archive/blogs/mikecha/feed.xml</link>
    <description />
    <lastBuildDate>Wed, 17 Apr 2019 03:42:54 GMT</lastBuildDate>
    <language>en-US</language>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>1</sy:updateFrequency>
    <item>
      <title>Improving Replication Performance By Removing Unused Indexes</title>
      <link>https://docs.microsoft.com/archive/blogs/mikecha/improving-replication-performance-by-removing-unused-indexes</link>
      <pubDate>Tue, 15 Dec 2009 16:06:00 GMT</pubDate>
      <dc:creator><![CDATA[mikecha]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/mikecha/2009/12/15/improving-replication-performance-by-removing-unused-indexes/</guid>
      <description><![CDATA[Replication performance can be improved by addressing different aspects of SQL server, such as disk,...]]></description>
      <content:encoded><![CDATA[<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">Replication performance can be improved by addressing different aspects of SQL server, such as disk, memory, CPU, DB design and implementation.&nbsp; A great whitepaper covers all these can found at <A href="https://technet.microsoft.com/en-us/library/cc966539.aspx" mce_href="https://technet.microsoft.com/en-us/library/cc966539.aspx"><FONT color=#0000ff>https://technet.microsoft.com/en-us/library/cc966539.aspx</FONT></A>.&nbsp; However, on a system with a less optimal index strategy or on a production environment with legacy indexes, some "unused" indexes can&nbsp;tax on the performance&nbsp;of SQL server as well as&nbsp;increasing the latency of replication.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">When you have&nbsp;indexes that are never used for&nbsp;any&nbsp;SELECT queries, the obvious issue is the wasted disk space to store them.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>The less obvious is the penalty you pay to maintain the indexes if their underlying data is subject to inserts, updates or deletes.&nbsp; Whenever&nbsp;the data changes, SQL may need to&nbsp;rearrange&nbsp;and maintain the index trees.<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">Consider in a replication&nbsp;environment where&nbsp;the updates of&nbsp;tables from&nbsp;the publisher&nbsp;get trickled down to the subscriber.&nbsp; In the profiler trace against the subscriber, you only see replication calls to insert, update or delete the&nbsp;tables on the subscriber but implicitly&nbsp;all the related indexes of the modified tables are&nbsp;also changing.&nbsp; By identifying what indexes are not used and removing them,&nbsp;both the&nbsp;publisher and subscriber can benefit from less indexes to maintain.<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">To find out the index usage, use&nbsp;DMV <SPAN style="COLOR: green">sys</SPAN><SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: green">dm_db_index_usage_stats </SPAN>along with system views <SPAN style="COLOR: green">sys</SPAN><SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: green">indexes</SPAN>&nbsp;, <SPAN style="COLOR: green">sys</SPAN><SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: green">objects </SPAN>and&nbsp;<SPAN style="COLOR: green">sys</SPAN><SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: green">schemas</SPAN>&nbsp;like the script below:<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: blue; FONT-SIZE: 10pt">select</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"> sysschema<SPAN style="COLOR: gray">.</SPAN>name <SPAN style="COLOR: red">'schema_name'<o:p></o:p></SPAN></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">,</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">sysobj<SPAN style="COLOR: gray">.</SPAN>name <SPAN style="COLOR: red">'table_name'<o:p></o:p></SPAN></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">,</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: fuchsia; FONT-SIZE: 10pt">ISNULL</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">(</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">sysidx<SPAN style="COLOR: gray">.</SPAN>name<SPAN style="COLOR: gray">,</SPAN><SPAN style="COLOR: red">'HEAP'</SPAN><SPAN style="COLOR: gray">)</SPAN> <SPAN style="COLOR: red">'index_name'<o:p></o:p></SPAN></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">,</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">sysidx<SPAN style="COLOR: gray">.</SPAN>index_id<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">,</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: red; FONT-SIZE: 10pt">'is_unused_index'</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"> <SPAN style="COLOR: gray">=</SPAN> <SPAN style="COLOR: blue">CASE</SPAN> <SPAN style="COLOR: fuchsia">ISNULL</SPAN><SPAN style="COLOR: gray">(</SPAN>idxstats<SPAN style="COLOR: gray">.</SPAN>user_seeks<SPAN style="COLOR: gray">,</SPAN>0<SPAN style="COLOR: gray">)</SPAN> <SPAN style="COLOR: gray">+ </SPAN><SPAN style="COLOR: fuchsia">ISNULL</SPAN><SPAN style="COLOR: gray">(</SPAN>idxstats<SPAN style="COLOR: gray">.</SPAN>user_scans<SPAN style="COLOR: gray">,</SPAN>0<SPAN style="COLOR: gray">)</SPAN> <SPAN style="COLOR: gray">+ </SPAN><SPAN style="COLOR: fuchsia">ISNULL</SPAN><SPAN style="COLOR: gray">(</SPAN>idxstats<SPAN style="COLOR: gray">.</SPAN>user_lookups<SPAN style="COLOR: gray">,</SPAN>0<SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: blue; FONT-SIZE: 10pt">WHEN</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"> 0 <SPAN style="COLOR: blue">THEN</SPAN> 1 <SPAN style="COLOR: blue">ELSE</SPAN> 0 <SPAN style="COLOR: blue">END</SPAN> <o:p></o:p></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">,</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">sysidx<SPAN style="COLOR: gray">.</SPAN>is_unique<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">,</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">sysidx<SPAN style="COLOR: gray">.</SPAN>is_unique_constraint<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">,</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: fuchsia; FONT-SIZE: 10pt">ISNULL</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">(</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">idxstats<SPAN style="COLOR: gray">.</SPAN>user_updates<SPAN style="COLOR: gray">,</SPAN>0<SPAN style="COLOR: gray">)</SPAN> <SPAN style="COLOR: red">'user_updates'<o:p></o:p></SPAN></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">,</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: fuchsia; FONT-SIZE: 10pt">ISNULL</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">(</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">idxstats<SPAN style="COLOR: gray">.</SPAN>user_seeks<SPAN style="COLOR: gray">,</SPAN>0<SPAN style="COLOR: gray">)</SPAN> <SPAN style="COLOR: red">'user_seeks'<o:p></o:p></SPAN></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">,</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: fuchsia; FONT-SIZE: 10pt">ISNULL</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">(</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">idxstats<SPAN style="COLOR: gray">.</SPAN>user_scans<SPAN style="COLOR: gray">,</SPAN>0<SPAN style="COLOR: gray">)</SPAN> <SPAN style="COLOR: red">'user_scans'<o:p></o:p></SPAN></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">,</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: fuchsia; FONT-SIZE: 10pt">ISNULL</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">(</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">idxstats<SPAN style="COLOR: gray">.</SPAN>user_lookups<SPAN style="COLOR: gray">,</SPAN>0<SPAN style="COLOR: gray">)</SPAN> <SPAN style="COLOR: red">'user_lookups'</SPAN><SPAN style="COLOR: green"><o:p></o:p></SPAN></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">,</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"> DropScript <SPAN style="COLOR: gray">=</SPAN> <SPAN style="COLOR: blue">CASE</SPAN> <SPAN style="COLOR: fuchsia">ISNULL</SPAN><SPAN style="COLOR: gray">(</SPAN>idxstats<SPAN style="COLOR: gray">.</SPAN>user_seeks<SPAN style="COLOR: gray">,</SPAN>0<SPAN style="COLOR: gray">)</SPAN> <SPAN style="COLOR: gray">+ </SPAN><SPAN style="COLOR: fuchsia">ISNULL</SPAN><SPAN style="COLOR: gray">(</SPAN>idxstats<SPAN style="COLOR: gray">.</SPAN>user_scans<SPAN style="COLOR: gray">,</SPAN>0<SPAN style="COLOR: gray">)</SPAN> <SPAN style="COLOR: gray">+ </SPAN><SPAN style="COLOR: fuchsia">ISNULL</SPAN><SPAN style="COLOR: gray">(</SPAN>idxstats<SPAN style="COLOR: gray">.</SPAN>user_lookups<SPAN style="COLOR: gray">,</SPAN>0<SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: blue; FONT-SIZE: 10pt">WHEN</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"> 0 <SPAN style="COLOR: blue">THEN</SPAN> <SPAN style="COLOR: red">''</SPAN> <SPAN style="COLOR: blue">ELSE</SPAN> <SPAN style="COLOR: gray">null</SPAN> <SPAN style="COLOR: blue">END</SPAN> <o:p></o:p></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">+</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"> <SPAN style="COLOR: red">'IF EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N''['</SPAN> <SPAN style="COLOR: gray">+</SPAN> sysschema<SPAN style="COLOR: gray">.</SPAN>name <SPAN style="COLOR: gray">+</SPAN> <SPAN style="COLOR: red">'].['<o:p></o:p></SPAN></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">+</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"> sysobj<SPAN style="COLOR: gray">.</SPAN>name <SPAN style="COLOR: gray">+</SPAN> <SPAN style="COLOR: red">']'') AND name = N'''</SPAN> <SPAN style="COLOR: gray">+</SPAN> sysidx<SPAN style="COLOR: gray">.</SPAN>name <SPAN style="COLOR: gray">+</SPAN> <SPAN style="COLOR: red">''') '<o:p></o:p></SPAN></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">+</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"> <SPAN style="COLOR: blue">case</SPAN> sysidx<SPAN style="COLOR: gray">.</SPAN>is_unique_constraint <SPAN style="COLOR: green">-- 1: drop constraint 0: drop index<o:p></o:p></SPAN></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: blue; FONT-SIZE: 10pt">when</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"> 1 <SPAN style="COLOR: blue">then</SPAN> <SPAN style="COLOR: red">' ALTER TABLE ['</SPAN> <SPAN style="COLOR: gray">+</SPAN> sysschema<SPAN style="COLOR: gray">.</SPAN>name <SPAN style="COLOR: gray">+</SPAN> <SPAN style="COLOR: red">'].['</SPAN> <SPAN style="COLOR: gray">+</SPAN> sysobj<SPAN style="COLOR: gray">.</SPAN>name <SPAN style="COLOR: gray">+</SPAN> <SPAN style="COLOR: red">'] DROP CONSTRAINT ['</SPAN> <SPAN style="COLOR: gray">+</SPAN> sysidx<SPAN style="COLOR: gray">.</SPAN>name <SPAN style="COLOR: gray">+</SPAN> <SPAN style="COLOR: red">']'</SPAN> <o:p></o:p></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: blue; FONT-SIZE: 10pt">else</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"> <SPAN style="COLOR: red">' DROP INDEX ['</SPAN> <SPAN style="COLOR: gray">+</SPAN> sysidx<SPAN style="COLOR: gray">.</SPAN>name <SPAN style="COLOR: gray">+</SPAN> <SPAN style="COLOR: red">'] ON ['</SPAN> <SPAN style="COLOR: gray">+</SPAN> sysschema<SPAN style="COLOR: gray">.</SPAN>name <SPAN style="COLOR: gray">+</SPAN> <SPAN style="COLOR: red">'].['</SPAN> <SPAN style="COLOR: gray">+</SPAN> sysobj<SPAN style="COLOR: gray">.</SPAN>name <SPAN style="COLOR: gray">+</SPAN> <SPAN style="COLOR: red">']'<o:p></o:p></SPAN></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: blue; FONT-SIZE: 10pt">end<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: blue; FONT-SIZE: 10pt">from</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"> <SPAN style="COLOR: green">sys</SPAN><SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: green">indexes</SPAN> sysidx<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">join</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"> <SPAN style="COLOR: green">sys</SPAN><SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: green">objects</SPAN> sysobj <SPAN style="COLOR: blue">on</SPAN> sysidx<SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: fuchsia">object_id</SPAN> <SPAN style="COLOR: gray">=</SPAN> sysobj<SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: fuchsia">object_id<o:p></o:p></SPAN></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">join</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"> <SPAN style="COLOR: green">sys</SPAN><SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: green">schemas</SPAN> sysschema <SPAN style="COLOR: blue">on</SPAN> sysobj<SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: fuchsia">schema_id</SPAN> <SPAN style="COLOR: gray">=</SPAN> sysschema<SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: fuchsia">schema_id</SPAN> <o:p></o:p></SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">left</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"> <SPAN style="COLOR: gray">join</SPAN> <SPAN style="COLOR: green">sys</SPAN><SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: green">dm_db_index_usage_stats</SPAN> idxstats <o:p></o:p></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: blue; FONT-SIZE: 10pt">on</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"> sysidx<SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: fuchsia">object_id</SPAN><SPAN style="COLOR: gray">=</SPAN>idxstats<SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: fuchsia">object_id<o:p></o:p></SPAN></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">and</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"> sysidx<SPAN style="COLOR: gray">.</SPAN>index_id<SPAN style="COLOR: gray">=</SPAN>idxstats<SPAN style="COLOR: gray">.</SPAN>index_id<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">and</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"> idxstats<SPAN style="COLOR: gray">.</SPAN>database_id<SPAN style="COLOR: gray">=</SPAN><SPAN style="COLOR: fuchsia">db_id</SPAN><SPAN style="COLOR: gray">()<o:p></o:p></SPAN></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: blue; FONT-SIZE: 10pt">where</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"> sysobj<SPAN style="COLOR: gray">.</SPAN>type_desc <SPAN style="COLOR: gray">=</SPAN> <SPAN style="COLOR: red">'USER_TABLE'<o:p></o:p></SPAN></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">and</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"> sysidx<SPAN style="COLOR: gray">.</SPAN>index_id <SPAN style="COLOR: gray">&gt;</SPAN> 1<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: gray; FONT-SIZE: 10pt">and</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"> sysobj<SPAN style="COLOR: gray">.</SPAN>is_ms_shipped <SPAN style="COLOR: gray">=</SPAN> 0 <SPAN style="COLOR: green">-- exclude object created by an internal SQL Server component such as sysarticleupdates, syspublications, syssubscriptions<o:p></o:p></SPAN></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; COLOR: blue; FONT-SIZE: 10pt">order</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"> <SPAN style="COLOR: blue">by</SPAN> 1<SPAN style="COLOR: gray">,</SPAN>2<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"></SPAN>&nbsp;</P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">Executing the script, you get a list of unused indexes and also the drop&nbsp;index statements that you can&nbsp;copy and paste&nbsp;as a deployment script.<o:p></o:p></SPAN></P>
<P mce_keep="true"><IMG style="WIDTH: 850px; HEIGHT: 150px" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/09/93/79/69/Result.png" original-url="https://blogs.msdn.com/photos/mikecha/images/9937969/original.aspx" width=850 height=150 mce_src="https://blogs.msdn.com/photos/mikecha/images/9937969/original.aspx"></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"></SPAN>&nbsp;</P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">As I mentioned before, indexes&nbsp;can be used&nbsp;find data&nbsp;(read-only) but they are also subject to updates (write-only)&nbsp;if the underlying data is modified.&nbsp; By permutating read and write activities on the indexes, we can conclude four types of index usage patterns as the table below:<o:p></o:p></SPAN></P>
<P>
<TABLE style="BORDER-BOTTOM: medium none; BORDER-LEFT: medium none; BORDER-COLLAPSE: collapse; BORDER-TOP: medium none; BORDER-RIGHT: medium none; mso-yfti-tbllook: 1184; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-border-alt: solid windowtext .5pt" class=MsoNormalTable border=1 cellSpacing=0 cellPadding=0 class="MsoNormalTable">
<TBODY>
<TR style="mso-yfti-irow: 0; mso-yfti-firstrow: yes">
<TD style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: windowtext 1pt solid; PADDING-BOTTOM: 0in; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; WIDTH: 95.75pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0in; mso-border-alt: solid windowtext .5pt" vAlign=top width=128>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">Row # from output above<o:p></o:p></SPAN></P></TD>
<TD style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: #f0f0f0; PADDING-BOTTOM: 0in; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; WIDTH: 95.75pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0in; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt" vAlign=top width=128>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">Read-only access pattern (scans, seeks and lookups)<o:p></o:p></SPAN></P></TD>
<TD style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: #f0f0f0; PADDING-BOTTOM: 0in; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; WIDTH: 95.75pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0in; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt" vAlign=top width=128>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">Write-only access pattern (updates)<o:p></o:p></SPAN></P></TD>
<TD style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: #f0f0f0; PADDING-BOTTOM: 0in; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; WIDTH: 95.75pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0in; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt" vAlign=top width=128>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">Indexes not used and should be dropped<o:p></o:p></SPAN></P></TD>
<TD style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: #f0f0f0; PADDING-BOTTOM: 0in; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; WIDTH: 95.75pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0in; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt" vAlign=top width=128>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">Will improve performance after dropping indexes<o:p></o:p></SPAN></P></TD></TR>
<TR style="mso-yfti-irow: 1">
<TD style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: windowtext 1pt solid; PADDING-BOTTOM: 0in; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; WIDTH: 95.75pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0in; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" vAlign=top width=128>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">26<o:p></o:p></SPAN></P></TD>
<TD style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: #f0f0f0; PADDING-BOTTOM: 0in; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; WIDTH: 95.75pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0in; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" vAlign=top width=128>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">Yes<o:p></o:p></SPAN></P></TD>
<TD style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: #f0f0f0; PADDING-BOTTOM: 0in; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; WIDTH: 95.75pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0in; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" vAlign=top width=128>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">Yes<o:p></o:p></SPAN></P></TD>
<TD style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: #f0f0f0; PADDING-BOTTOM: 0in; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; WIDTH: 95.75pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0in; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" vAlign=top width=128>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">No<o:p></o:p></SPAN></P></TD>
<TD style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: #f0f0f0; PADDING-BOTTOM: 0in; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; WIDTH: 95.75pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0in; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" vAlign=top width=128>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">N/A<o:p></o:p></SPAN></P></TD></TR>
<TR style="mso-yfti-irow: 2">
<TD style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: windowtext 1pt solid; PADDING-BOTTOM: 0in; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; WIDTH: 95.75pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0in; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" vAlign=top width=128>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">23, 28<o:p></o:p></SPAN></P></TD>
<TD style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: #f0f0f0; PADDING-BOTTOM: 0in; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; WIDTH: 95.75pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0in; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" vAlign=top width=128>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">No<o:p></o:p></SPAN></P></TD>
<TD style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: #f0f0f0; PADDING-BOTTOM: 0in; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; WIDTH: 95.75pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0in; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" vAlign=top width=128>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">No<o:p></o:p></SPAN></P></TD>
<TD style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: #f0f0f0; PADDING-BOTTOM: 0in; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; WIDTH: 95.75pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0in; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" vAlign=top width=128>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">Yes<o:p></o:p></SPAN></P></TD>
<TD style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: #f0f0f0; PADDING-BOTTOM: 0in; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; WIDTH: 95.75pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0in; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" vAlign=top width=128>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">No<o:p></o:p></SPAN></P></TD></TR>
<TR style="mso-yfti-irow: 3">
<TD style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: windowtext 1pt solid; PADDING-BOTTOM: 0in; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; WIDTH: 95.75pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0in; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" vAlign=top width=128>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">24, 25<o:p></o:p></SPAN></P></TD>
<TD style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: #f0f0f0; PADDING-BOTTOM: 0in; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; WIDTH: 95.75pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0in; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" vAlign=top width=128>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">Yes<o:p></o:p></SPAN></P></TD>
<TD style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: #f0f0f0; PADDING-BOTTOM: 0in; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; WIDTH: 95.75pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0in; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" vAlign=top width=128>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">No<o:p></o:p></SPAN></P></TD>
<TD style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: #f0f0f0; PADDING-BOTTOM: 0in; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; WIDTH: 95.75pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0in; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" vAlign=top width=128>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">No<o:p></o:p></SPAN></P></TD>
<TD style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: #f0f0f0; PADDING-BOTTOM: 0in; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; WIDTH: 95.75pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0in; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" vAlign=top width=128>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">N/A<o:p></o:p></SPAN></P></TD></TR>
<TR style="mso-yfti-irow: 4; mso-yfti-lastrow: yes">
<TD style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: windowtext 1pt solid; PADDING-BOTTOM: 0in; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; WIDTH: 95.75pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0in; mso-border-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" vAlign=top width=128>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">27<o:p></o:p></SPAN></P></TD>
<TD style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: #f0f0f0; PADDING-BOTTOM: 0in; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; WIDTH: 95.75pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0in; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" vAlign=top width=128>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">No<o:p></o:p></SPAN></P></TD>
<TD style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: #f0f0f0; PADDING-BOTTOM: 0in; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; WIDTH: 95.75pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0in; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" vAlign=top width=128>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">Yes<o:p></o:p></SPAN></P></TD>
<TD style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: #f0f0f0; PADDING-BOTTOM: 0in; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; WIDTH: 95.75pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0in; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" vAlign=top width=128>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">Yes<o:p></o:p></SPAN></P></TD>
<TD style="BORDER-BOTTOM: windowtext 1pt solid; BORDER-LEFT: #f0f0f0; PADDING-BOTTOM: 0in; BACKGROUND-COLOR: transparent; PADDING-LEFT: 5.4pt; WIDTH: 95.75pt; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; BORDER-RIGHT: windowtext 1pt solid; PADDING-TOP: 0in; mso-border-alt: solid windowtext .5pt; mso-border-left-alt: solid windowtext .5pt; mso-border-top-alt: solid windowtext .5pt" vAlign=top width=128>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">Yes<o:p></o:p></SPAN></P></TD></TR></TBODY></TABLE></P>
<P style="MARGIN: 0in 0in 0pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">Let me go over all four of them.<o:p></o:p></SPAN></P>
<P style="TEXT-INDENT: -0.25in; MARGIN-LEFT: 0.5in; mso-list: l1 level1 lfo1"><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt; mso-fareast-font-family: Arial"><SPAN style="mso-list: Ignore">1.<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">BOTH read&nbsp;and write access patterns: this is indexes that help answering queries and also get updated when underlying data changes.&nbsp;&nbsp;Let's&nbsp;keep this kind.<o:p></o:p></SPAN></P>
<P style="TEXT-INDENT: -0.25in; MARGIN-LEFT: 0.5in; mso-list: l1 level1 lfo1"><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt; mso-fareast-font-family: Arial"><SPAN style="mso-list: Ignore">2.<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">No&nbsp;read&nbsp;or write access pattern:&nbsp;although it's not used but it's not hindering&nbsp;system performance.&nbsp;&nbsp;Removing this kind&nbsp;of indexs&nbsp;saves you disk space at most without influencing performance.<o:p></o:p></SPAN></P>
<P style="TEXT-INDENT: -0.25in; MARGIN-LEFT: 0.5in; mso-list: l1 level1 lfo1"><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt; mso-fareast-font-family: Arial"><SPAN style="mso-list: Ignore">3.<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">Only read access pattern but not write access pattern: like the first one, this sort helps your joins and the underlying data is never changed so no writes.&nbsp; Let's also keep it.<o:p></o:p></SPAN></P>
<P style="TEXT-INDENT: -0.25in; MARGIN-LEFT: 0.5in; mso-list: l1 level1 lfo1"><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt; mso-fareast-font-family: Arial"><SPAN style="mso-list: Ignore">4.<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">Last is the unnecessary indexes that are never used to answer queries but are needed to be updated to maintain index tree structure.&nbsp; Grab the DropScript column for your convenience where the drop statements are generated.&nbsp; Getting rid of these indexes should improve your replication performance.<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">The same exercise can be performed on a non-replication system.&nbsp; You should get the same&nbsp;performance gain when those unused indexes are dropped.<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">The gain should be in the form of shorter replication backup on the subscriber when the publisher makes a&nbsp;big push.&nbsp; Another form of gain, regardless of a&nbsp;replication system or not,&nbsp;should be noticed with lower CPU usage.&nbsp; The main reason is the reduction of index searches associated with&nbsp;maintaining the index trees.<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">A few notes of caution:<o:p></o:p></SPAN></P>
<P style="TEXT-INDENT: -0.25in; MARGIN-LEFT: 0.5in; mso-list: l0 level1 lfo2"><SPAN style="FONT-FAMILY: Symbol; FONT-SIZE: 10pt; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"><SPAN style="mso-list: Ignore">·<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">Be aware&nbsp;that the&nbsp;DMV's used&nbsp;in the script accumulates&nbsp;index usages since SQL last restarts.&nbsp; You should make sure&nbsp;SQL has been running for&nbsp;a while,&nbsp;long enough to&nbsp;capture all&nbsp;patterns of your loads.<o:p></o:p></SPAN></P>
<P style="TEXT-INDENT: -0.25in; MARGIN-LEFT: 0.5in; mso-list: l0 level1 lfo2"><SPAN style="FONT-FAMILY: Symbol; FONT-SIZE: 10pt; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"><SPAN style="mso-list: Ignore">·<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">Some unused indexes can be unique constraints which enforce your business rules.&nbsp; Carefully examine them to decide if they can be drop without breaking any business rules.<o:p></o:p></SPAN></P>
<P style="TEXT-INDENT: -0.25in; MARGIN-LEFT: 0.5in; mso-list: l0 level1 lfo2"><SPAN style="FONT-FAMILY: Symbol; FONT-SIZE: 10pt; mso-fareast-font-family: Symbol; mso-bidi-font-family: Symbol"><SPAN style="mso-list: Ignore">·<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">Lastly, thoroughly try this out on you test or PPE environment before proceeding to production.<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">Leave a&nbsp;comment if&nbsp;you see any performance gain and by how much&nbsp;after removing these indexes.<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">Happy coding!<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">Mike Chang (Chien-Shen)<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt">SQL SDE, MSDN<o:p></o:p></SPAN></P>
<P style="MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt"><o:p>&nbsp;</o:p></SPAN></P>]]></content:encoded>
    </item>
    <item>
      <title>Temp Table Recompilation Activity in One Session Can Affect Another Session</title>
      <link>https://docs.microsoft.com/archive/blogs/mikecha/temp-table-recompilation-activity-in-one-session-can-affect-another-session</link>
      <pubDate>Thu, 13 Aug 2009 00:43:00 GMT</pubDate>
      <dc:creator><![CDATA[mikecha]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/mikecha/2009/08/13/temp-table-recompilation-activity-in-one-session-can-affect-another-session/</guid>
      <description><![CDATA[Query recompilation related to temp table data changes&nbsp;has long been thought limited to...]]></description>
      <content:encoded><![CDATA[<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 10pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" class=MsoNormal><SPAN style="mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><FONT size=3><FONT face=Calibri>Query recompilation related to temp table data changes&nbsp;has long been thought limited to individual session scope.&nbsp; In another word, different sessions may populate their temp tables very differently and we count on the query recompilation&nbsp;to happen individually in each session&nbsp;if the temp table crosses different recompilation thresholds (RT).&nbsp; That way, the optimizer can give us an optimal plan based on the stats of the temp table.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></FONT></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 10pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" class=MsoNormal><SPAN style="mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><FONT size=3><FONT face=Calibri>However, I have discovered a "defect" that the recompilation thresholds are not always honored.&nbsp; The&nbsp;anomaly is that if one session has&nbsp;crossed a higher level RT and also incurs a recompilation, a later, different session that populates the temp table with fewer rows and also crossed a RT that equal or less than the RT of the aforementioned session, a recompilation that should have incurred would be skipped.<o:p></o:p></FONT></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 10pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" class=MsoNormal><SPAN style="mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><FONT size=3 face=Calibri>The best way to explain this anomaly is to show you how to repro it.&nbsp; I will use an easy to understand example and also screenshots to illustrate the issue.&nbsp; You don't need to repro it to get the idea it but I bet a lot of you will be intrigued to repro it.&nbsp; When I first discovered this bug, I thought I was out of my&nbsp;mind during the </FONT><A title="All time record high 106F in Remond, WA" href="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/09/86/77/39/RecordHigh106InRedmond.jpg" original-url="https://blogs.msdn.com/photos/mikecha/images/9867739/original.aspx" target=_blank mce_href="https://blogs.msdn.com/photos/mikecha/images/9867739/original.aspx"><SPAN style="COLOR: blue"><FONT size=3 face=Calibri>all time high record hot day in Remond, WA</FONT></SPAN></A><FONT size=3><FONT face=Calibri>!&nbsp;<o:p></o:p></FONT></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 10pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" class=MsoNormal><SPAN style="mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><FONT size=3><FONT face=Calibri>My environment is Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (X64)&nbsp;&nbsp; Jul&nbsp; 9 2008 14:17:44&nbsp;&nbsp; Copyright (c) 1988-2008 Microsoft Corporation&nbsp; Enterprise Edition (64-bit) on Windows NT 6.0 &lt;X64&gt; (Build 6001: Service Pack 1)<o:p></o:p></FONT></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 10pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" class=MsoNormal><SPAN style="mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><FONT size=3><FONT face=Calibri>Here's the repro:<o:p></o:p></FONT></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 10pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" class=MsoNormal><SPAN style="mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><FONT size=3><FONT face=Calibri>First to create objects we need: one dbo.Num table as the source of the temp table and one dbo.proc1 to be executed on different sessions.<o:p></o:p></FONT></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Times New Roman','serif'; FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'"><o:p>&nbsp;</o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">use</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> tempdb<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">create</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> <SPAN style="COLOR: blue">table</SPAN> dbo<SPAN style="COLOR: gray">.</SPAN>Num<SPAN style="COLOR: gray">(</SPAN> CIKey <SPAN style="COLOR: blue">int</SPAN> <SPAN style="COLOR: blue">primary</SPAN> <SPAN style="COLOR: blue">key</SPAN><SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: gray; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"><o:p>&nbsp;</o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">declare</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> @rowcount <SPAN style="COLOR: blue">int</SPAN> <SPAN style="COLOR: gray">=</SPAN> 2000<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">declare</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> @i <SPAN style="COLOR: blue">int</SPAN> <SPAN style="COLOR: gray">=</SPAN> 1<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">while</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> @i <SPAN style="COLOR: gray">&lt;=</SPAN> @rowcount<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">begin<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"><o:p>&nbsp;</o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">&nbsp;&nbsp; <SPAN style="COLOR: blue">insert</SPAN> <SPAN style="COLOR: blue">into</SPAN> dbo<SPAN style="COLOR: gray">.</SPAN>Num<SPAN style="COLOR: gray">(</SPAN>CIKey<SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">&nbsp;&nbsp; <SPAN style="COLOR: blue">values </SPAN><SPAN style="COLOR: gray">(</SPAN>@i<SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">&nbsp;&nbsp; <o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">&nbsp;&nbsp; <SPAN style="COLOR: blue">select</SPAN> @i <SPAN style="COLOR: gray">+=</SPAN> 1<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">end<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"><o:p>&nbsp;</o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">select</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> <SPAN style="COLOR: gray">*</SPAN> <SPAN style="COLOR: blue">from</SPAN> dbo<SPAN style="COLOR: gray">.</SPAN>Num <SPAN style="COLOR: green">-- return 1 - 2000<o:p></o:p></SPAN></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"><o:p>&nbsp;</o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"><o:p>&nbsp;</o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">use</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> tempdb<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">create</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> <SPAN style="COLOR: blue">procedure</SPAN> Proc1<SPAN style="COLOR: blue"> </SPAN><SPAN style="COLOR: gray">(</SPAN>@start <SPAN style="COLOR: blue">int</SPAN><SPAN style="COLOR: gray">,</SPAN> @rowcount <SPAN style="COLOR: blue">int</SPAN><SPAN style="COLOR: gray">,</SPAN> @filter <SPAN style="COLOR: blue">int</SPAN><SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">as<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">begin<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"><o:p>&nbsp;</o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">create</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> <SPAN style="COLOR: blue">table</SPAN> #t<SPAN style="COLOR: blue"> </SPAN><SPAN style="COLOR: gray">(<o:p></o:p></SPAN></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [CIKey]&nbsp;&nbsp; [int] <SPAN style="COLOR: blue">identity</SPAN><SPAN style="COLOR: gray">(</SPAN>1<SPAN style="COLOR: gray">,</SPAN>1<SPAN style="COLOR: gray">)</SPAN> <SPAN style="COLOR: blue">primary</SPAN> <SPAN style="COLOR: blue">key</SPAN><SPAN style="COLOR: gray">,<o:p></o:p></SPAN></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [NCKey] [int] <SPAN style="COLOR: gray">,<o:p></o:p></SPAN></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">&nbsp;&nbsp; filler <SPAN style="COLOR: blue">nvarchar</SPAN><SPAN style="COLOR: gray">(</SPAN>1000<SPAN style="COLOR: gray">)</SPAN> <SPAN style="COLOR: blue">default </SPAN><SPAN style="COLOR: gray">(</SPAN><SPAN style="COLOR: fuchsia">replicate</SPAN><SPAN style="COLOR: gray">(</SPAN><SPAN style="COLOR: red">'x'</SPAN><SPAN style="COLOR: gray">,</SPAN>1000<SPAN style="COLOR: gray">))<o:p></o:p></SPAN></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: gray; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">)<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">create</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> <SPAN style="COLOR: blue">index</SPAN> #t_NCKey <SPAN style="COLOR: blue">on</SPAN> #t<SPAN style="COLOR: gray">(</SPAN>NCKey<SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: gray; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"><o:p>&nbsp;</o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">insert</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> <SPAN style="COLOR: blue">into</SPAN> #t<SPAN style="COLOR: blue"> </SPAN><SPAN style="COLOR: gray">(</SPAN>[NCKey]<SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">select</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> cikey <SPAN style="COLOR: blue">from</SPAN> dbo<SPAN style="COLOR: gray">.</SPAN>num <o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">where</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> cikey <SPAN style="COLOR: gray">between</SPAN> @start <SPAN style="COLOR: gray">and</SPAN> @start <SPAN style="COLOR: gray">+</SPAN> @rowcount<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">&nbsp; <o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">-- Where the recompilation occurs<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">select</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> <SPAN style="COLOR: gray">*</SPAN> <SPAN style="COLOR: blue">from</SPAN> #t<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">where</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> NCKey <SPAN style="COLOR: gray">&gt;=</SPAN> @filter <o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"><o:p>&nbsp;</o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">end<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">go<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"><o:p>&nbsp;</o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">dbcc</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> freeproccache<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">dbcc</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> dropcleanbuffers<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"><o:p>&nbsp;</o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: green; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">-- get plan compiled and cached<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">exec</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> proc1<SPAN style="COLOR: blue"> </SPAN>1<SPAN style="COLOR: gray">,</SPAN>1<SPAN style="COLOR: gray">,</SPAN> 1<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 10pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" class=MsoNormal><SPAN style="mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><FONT size=3><FONT face=Calibri>To catch the anomaly, let's use&nbsp;SQL profiler and select events <B style="mso-bidi-font-weight: normal"><SPAN style="COLOR: black">SP:Completed</SPAN></B><SPAN style="COLOR: black"> </SPAN>and <B style="mso-bidi-font-weight: normal"><SPAN style="COLOR: black">SP:Recompile</SPAN></B><SPAN style="COLOR: black">.<o:p></o:p></SPAN></FONT></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><FONT size=3><FONT face=Calibri>That was the setup of our test environment.&nbsp; We are ready for our repro of the bug.&nbsp;<o:p></o:p></FONT></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><o:p><FONT size=3 face=Calibri>&nbsp;</FONT></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><FONT size=3><FONT face=Calibri>Open session 1, and run these four calls, all of them should incur <B>2 - Statistics changed </B>EventSubClass in the profiler.<o:p></o:p></FONT></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt; mso-ascii-font-family: 'Times New Roman'; mso-fareast-font-family: 'Times New Roman'">&nbsp;</SPAN><SPAN style="FONT-FAMILY: 'Times New Roman','serif'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial"><o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">exec</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> proc1<SPAN style="COLOR: blue"> </SPAN>1<SPAN style="COLOR: gray">,</SPAN>10<SPAN style="COLOR: gray">,</SPAN> 1<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">exec</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> proc1<SPAN style="COLOR: blue"> </SPAN>1<SPAN style="COLOR: gray">,</SPAN>515<SPAN style="COLOR: gray">,</SPAN> 1<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">exec</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> proc1<SPAN style="COLOR: blue"> </SPAN>1<SPAN style="COLOR: gray">,</SPAN>1150<SPAN style="COLOR: gray">,</SPAN> 1<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">exec</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> proc1<SPAN style="COLOR: blue"> </SPAN>1<SPAN style="COLOR: gray">,</SPAN>1900<SPAN style="COLOR: gray">,</SPAN> 1<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Times New Roman','serif'; FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'"><o:p>&nbsp;</o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 10pt" class=MsoNormal><SPAN style="mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><FONT size=3><FONT face=Calibri>Open session2 and run the same four calls but to slightly change the first two parameters to differentiate from session 1.<o:p></o:p></FONT></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Times New Roman','serif'; FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'"><o:p>&nbsp;</o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">exec</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> proc1<SPAN style="COLOR: blue"> </SPAN>100<SPAN style="COLOR: gray">,</SPAN>11<SPAN style="COLOR: gray">,</SPAN> 1<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">exec</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> proc1<SPAN style="COLOR: blue"> </SPAN>100<SPAN style="COLOR: gray">,</SPAN>516<SPAN style="COLOR: gray">,</SPAN> 1<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">exec</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> proc1<SPAN style="COLOR: blue"> </SPAN>100<SPAN style="COLOR: gray">,</SPAN>1151<SPAN style="COLOR: gray">,</SPAN> 1<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">exec</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> proc1<SPAN style="COLOR: blue"> </SPAN>100<SPAN style="COLOR: gray">,</SPAN>1901<SPAN style="COLOR: gray">,</SPAN> 1<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="FONT-FAMILY: 'Times New Roman','serif'; FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'"><o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 10pt" class=MsoNormal><SPAN style="mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><FONT size=3><FONT face=Calibri>Here is the screen shot of eight calls from two sessions (SPID 75 vs 76):<o:p></o:p></FONT></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Times New Roman','serif'; FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'"><IMG style="WIDTH: 770px; HEIGHT: 495px" title="Expected Recompilations" alt="Expected Recompilations" src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/09/89/85/77/New%20Picture.png" original-url="https://blogs.msdn.com/photos/mikecha/images/9898577/original.aspx" width=770 height=495 mce_src="https://blogs.msdn.com/photos/mikecha/images/9898577/original.aspx">&nbsp;<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-SIZE: 12pt; mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri; mso-no-proof: yes"><?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><v:shapetype id=_x0000_t75 stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><v:stroke joinstyle="miter"></v:stroke><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"></v:f><v:f eqn="sum @0 1 0"></v:f><v:f eqn="sum 0 0 @1"></v:f><v:f eqn="prod @2 1 2"></v:f><v:f eqn="prod @3 21600 pixelWidth"></v:f><v:f eqn="prod @3 21600 pixelHeight"></v:f><v:f eqn="sum @0 0 1"></v:f><v:f eqn="prod @6 1 2"></v:f><v:f eqn="prod @7 21600 pixelWidth"></v:f><v:f eqn="sum @8 21600 0"></v:f><v:f eqn="prod @7 21600 pixelHeight"></v:f><v:f eqn="sum @10 21600 0"></v:f></v:formulas><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></v:path><o:lock aspectratio="t" v:ext="edit"></o:lock></v:shapetype><v:shape style="WIDTH: 577.2pt; HEIGHT: 371.4pt; VISIBILITY: visible; mso-wrap-style: square" id=Picture_x0020_1 alt="Expected Recompilation Behavior" type="#_x0000_t75" o:spid="_x0000_i1027"><v:imagedata mce_src="file:///C:\Users\mikecha\AppData\Local\Temp\msohtmlclip1\01\clip_image001.png" o:title="Expected Recompilation Behavior" src="file:///C:\Users\mikecha\AppData\Local\Temp\msohtmlclip1\01\clip_image001.png"></v:imagedata></v:shape></SPAN><SPAN style="FONT-FAMILY: 'Times New Roman','serif'; FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'"><o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-SIZE: 12pt; mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><o:p><FONT face=Calibri>&nbsp;</FONT></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 10pt" class=MsoNormal><SPAN style="mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><FONT size=3><FONT face=Calibri>It's normal as expected with eight recompilations since each call would populate the temp table with more rows just enough to cross the RT and hence the recompilations.<o:p></o:p></FONT></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 10pt" class=MsoNormal><SPAN style="mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><o:p><FONT size=3 face=Calibri>&nbsp;</FONT></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 10pt" class=MsoNormal><SPAN style="mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><FONT size=3><FONT face=Calibri>Now the weird repro <o:p></o:p></FONT></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 10pt" class=MsoNormal><SPAN style="mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><o:p><FONT size=3 face=Calibri>&nbsp;</FONT></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 10pt" class=MsoNormal><SPAN style="mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><FONT size=3><FONT face=Calibri>Clear the profiler output but keep it running and make these calls in the following order:<o:p></o:p></FONT></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Times New Roman','serif'; FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'"><o:p>&nbsp;</o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-SIZE: 12pt; mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><FONT face=Calibri>Session 1:<o:p></o:p></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">exec</SPAN> proc1<SPAN style="COLOR: blue"> </SPAN>1<SPAN style="COLOR: gray">,</SPAN>10<SPAN style="COLOR: gray">,</SPAN> 1<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Times New Roman','serif'; FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'"><o:p>&nbsp;</o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-SIZE: 12pt; mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><FONT face=Calibri>Session 2:<o:p></o:p></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">exec</SPAN> proc1<SPAN style="COLOR: blue"> </SPAN>100<SPAN style="COLOR: gray">,</SPAN>11<SPAN style="COLOR: gray">,</SPAN> 1<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">exec</SPAN> proc1<SPAN style="COLOR: blue"> </SPAN>100<SPAN style="COLOR: gray">,</SPAN>516<SPAN style="COLOR: gray">,</SPAN> 1<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Times New Roman','serif'; FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'"><o:p>&nbsp;</o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-SIZE: 12pt; mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><FONT face=Calibri>Session 1:<o:p></o:p></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">exec</SPAN> proc1<SPAN style="COLOR: blue"> </SPAN>1<SPAN style="COLOR: gray">,</SPAN>515<SPAN style="COLOR: gray">,</SPAN> 1<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">exec</SPAN> proc1<SPAN style="COLOR: blue"> </SPAN>1<SPAN style="COLOR: gray">,</SPAN>1150<SPAN style="COLOR: gray">,</SPAN> 1<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Times New Roman','serif'; FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'"><o:p>&nbsp;</o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-SIZE: 12pt; mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><FONT face=Calibri>Session 2:<o:p></o:p></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">exec</SPAN> proc1<SPAN style="COLOR: blue"> </SPAN>100<SPAN style="COLOR: gray">,</SPAN>1151<SPAN style="COLOR: gray">,</SPAN> 1<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">exec</SPAN> proc1<SPAN style="COLOR: blue"> </SPAN>100<SPAN style="COLOR: gray">,</SPAN>1901<SPAN style="COLOR: gray">,</SPAN> 1<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Times New Roman','serif'; FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'"><o:p>&nbsp;</o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-SIZE: 12pt; mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><FONT face=Calibri>Session 1:<o:p></o:p></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">exec</SPAN> proc1<SPAN style="COLOR: blue"> </SPAN>1<SPAN style="COLOR: gray">,</SPAN>1900<SPAN style="COLOR: gray">,</SPAN> 1<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Times New Roman','serif'; FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'">&nbsp;<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><FONT size=3><FONT face=Calibri>I have highlighted the four abnormal calls missing recompilations in the&nbsp;Profiler screenshot below.<o:p></o:p></FONT></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Times New Roman','serif'; FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'">&nbsp;<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Times New Roman','serif'; FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'; mso-no-proof: yes"><v:shape style="WIDTH: 577.2pt; HEIGHT: 371.4pt; VISIBILITY: visible; mso-wrap-style: square" id=Picture_x0020_2 alt="Abnormal Recompilation Behavior" type="#_x0000_t75" o:spid="_x0000_i1026"><v:imagedata mce_src="file:///C:\Users\mikecha\AppData\Local\Temp\msohtmlclip1\01\clip_image002.png" o:title="Abnormal Recompilation Behavior" src="file:///C:\Users\mikecha\AppData\Local\Temp\msohtmlclip1\01\clip_image002.png"></v:imagedata></v:shape></SPAN><SPAN style="FONT-FAMILY: 'Times New Roman','serif'; FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'"><o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Times New Roman','serif'; FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'"><o:p><IMG style="WIDTH: 770px; HEIGHT: 495px" title="Abnormal Recompilations" alt="Abnormal Recompilations" src="https://blogs.msdn.com/photos/mikecha/images/9898579/original.aspx" width=770 height=495 mce_src="https://blogs.msdn.com/photos/mikecha/images/9898579/original.aspx">&nbsp;</o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Times New Roman','serif'; FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'"><o:p></o:p></SPAN>&nbsp;</P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><FONT size=3><FONT face=Calibri>The correlation I can draw here is that if a different session has previously populated the temp table with closely same amount of rows, optimality recompilation will be skipped.&nbsp; Look at the right most column, RowCounts in the above screenshot.&nbsp; The abnormal ones have rowcounts that are within the same RT boundaries.&nbsp; For example, the second, normal line has 22 rows while the third, abnormal line has 24.<o:p></o:p></FONT></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><o:p><FONT size=3 face=Calibri>&nbsp;</FONT></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><FONT size=3><FONT face=Calibri>This has a huge impact on how we use temp table.&nbsp; Take my test proc for example.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>I have a third parameter @filter used to limit the returned recordset:<o:p></o:p></FONT></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Times New Roman','serif'; FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'"><o:p>&nbsp;</o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">select</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> <SPAN style="COLOR: gray">*</SPAN> <SPAN style="COLOR: blue">from</SPAN> #t<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">where</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> NCKey <SPAN style="COLOR: gray">&gt;=</SPAN> @filter <o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Times New Roman','serif'; FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'"><o:p>&nbsp;</o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 10pt" class=MsoNormal><SPAN style="mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><FONT size=3 face=Calibri>Let's say session 1 filters out a lot of rows by using a large @filter.&nbsp; For example,</FONT></SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">&nbsp;<SPAN style="COLOR: blue">exec</SPAN> proc1<SPAN style="COLOR: blue"> </SPAN>1<SPAN style="COLOR: gray">,</SPAN>2000<SPAN style="COLOR: gray">,</SPAN> 1995 </SPAN><FONT size=3><FONT face=Calibri><SPAN style="mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri">statement returns just 6 rows and the good plan is obviously to scan 6 rows on NC plus 6 CI bookmark lookups:</SPAN><SPAN style="FONT-FAMILY: 'Arial','sans-serif'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"><o:p></o:p></SPAN></FONT></FONT></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-FAMILY: 'Times New Roman','serif'; FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'"><IMG style="WIDTH: 718px; HEIGHT: 104px" title="Query Plan" alt="Query Plan" src="https://blogs.msdn.com/photos/mikecha/images/9898584/original.aspx" width=718 height=104 mce_src="https://blogs.msdn.com/photos/mikecha/images/9898584/original.aspx">&nbsp;<o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-SIZE: 12pt; mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri; mso-no-proof: yes"><v:shape style="WIDTH: 538.2pt; HEIGHT: 78pt; VISIBILITY: visible; mso-wrap-style: square" id=Picture_x0020_3 alt="Query Plan" type="#_x0000_t75" o:spid="_x0000_i1025"><v:imagedata mce_src="file:///C:\Users\mikecha\AppData\Local\Temp\msohtmlclip1\01\clip_image003.png" o:title="Query Plan" src="file:///C:\Users\mikecha\AppData\Local\Temp\msohtmlclip1\01\clip_image003.png"></v:imagedata></v:shape></SPAN><SPAN style="FONT-FAMILY: 'Times New Roman','serif'; FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'"><o:p></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="FONT-SIZE: 12pt; mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><o:p><FONT face=Calibri>&nbsp;</FONT></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><FONT size=3><SPAN style="mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><FONT face=Calibri>In session 2, without the needed recompilation, when running this call that doesn't do any filtering:</FONT></SPAN><SPAN style="FONT-FAMILY: 'Times New Roman','serif'; FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'">&nbsp;</SPAN></FONT><SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'">exec</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'"> proc1<SPAN style="COLOR: blue"> </SPAN>1<SPAN style="COLOR: gray">,</SPAN>2000<SPAN style="COLOR: gray">,</SPAN> 1, </SPAN><SPAN style="mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><FONT size=3><FONT face=Calibri>the above inefficient plan is re-used causing bookmark lookup on the CI for every row (4006 reads)<o:p></o:p></FONT></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><o:p><FONT size=3 face=Calibri>&nbsp;</FONT></o:p></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><FONT size=3><FONT face=Calibri>If the recompilation did happen, SQL would have used a efficient CI scan to dump out the temp table (501 reads).<o:p></o:p></FONT></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><FONT size=3><FONT face=Calibri>&nbsp;<o:p></o:p></FONT></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><FONT size=3 face=Calibri>This unexpected behavior has been reported to SQL team and is confirmed as a </FONT><A title="Bug: Temp Table Recompilation Activity in One Session Can Affect Another" href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=480871" target=_blank mce_href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=480871"><SPAN style="COLOR: windowtext; TEXT-DECORATION: none; text-underline: none"><FONT size=3 face=Calibri><U>bug</U></FONT></SPAN></A><FONT size=3><FONT face=Calibri>.&nbsp; One of the developers put it as "a very astute discovery".&nbsp; I will update my blogs when I hear a fix.&nbsp; In the meantime, check your code and see if your application can be affected.&nbsp; Also feel free to leave a comment on how you would work around this.<o:p></o:p></FONT></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><FONT size=3><FONT face=Calibri>&nbsp;<o:p></o:p></FONT></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><FONT size=3><FONT face=Calibri>Happy coding, <o:p></o:p></FONT></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><FONT size=3><FONT face=Calibri>&nbsp;<o:p></o:p></FONT></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><FONT size=3><FONT face=Calibri>Mike Chang (Chien-Shen)<o:p></o:p></FONT></FONT></SPAN></P>
<P style="LINE-HEIGHT: normal; MARGIN: 0in 0in 0pt" class=MsoNormal><SPAN style="mso-ascii-font-family: Calibri; mso-fareast-font-family: 'Times New Roman'; mso-hansi-font-family: Calibri; mso-bidi-font-family: Calibri"><FONT size=3><FONT face=Calibri>SQL Dev, MSDN<o:p></o:p></FONT></FONT></SPAN></P>
<P style="MARGIN: 0in 0in 10pt" class=MsoNormal><o:p><FONT size=3 face=Calibri>&nbsp;</FONT></o:p></P>]]></content:encoded>
    </item>
    <item>
      <title>Two Fast Ways to Bulk Insert Client Generated Data to SQL Database</title>
      <link>https://docs.microsoft.com/archive/blogs/mikecha/two-fast-ways-to-bulk-insert-client-generated-data-to-sql-database</link>
      <pubDate>Fri, 07 Aug 2009 14:08:00 GMT</pubDate>
      <dc:creator><![CDATA[mikecha]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/mikecha/2009/08/07/two-fast-ways-to-bulk-insert-client-generated-data-to-sql-database/</guid>
      <description><![CDATA[&nbsp;
Bulk loading client side data into a SQL database is a common task in software...]]></description>
      <content:encoded><![CDATA[<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Calibri size=3>Bulk loading client side data into a SQL database is a common task in software development.&nbsp; Sometimes the data is client generated and cached.&nbsp; When the data accumulates to a specific size or over a specific period time, it's then bulk loaded into the database, for example, web server logs or system events.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Calibri size=3>On Microsoft platform, several approaches can be used to achieve this goal very effectively.&nbsp; Here are two approaches, using ADO.Net DataTableReader class and using ADO.Net to construct a Dynamic TSQL.&nbsp; Both approaches use a new SQL 2008 feature, table valued parameter (TVP) as the vehicle to transmit data to SQL server.&nbsp; They are all fairly easy to implement and have good performance results which will be detailed later.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT size=3><FONT face=Calibri><B>First is the setup at the SQL side</B> that we will be utilized by the client ADO.Net code.&nbsp; We will create a database, a table, a TVP type and a procedure.&nbsp; The procedure has a TVP type parameter passed in from the client and populates the table.&nbsp; Here's the script:</FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">CREATE</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> <SPAN style="COLOR: blue">database</SPAN> DemoDB<SPAN style="COLOR: gray">;<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">GO<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">CREATE</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> <SPAN style="COLOR: blue">table</SPAN> dbo<SPAN style="COLOR: gray">.</SPAN>MyTvpTargetTable <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'">(<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp; c1 <SPAN style="COLOR: blue">int<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp; <SPAN style="COLOR: gray">,</SPAN>c2 <SPAN style="COLOR: blue">int<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'">)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">GO<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">CREATE</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> <SPAN style="COLOR: blue">TYPE</SPAN> dbo<SPAN style="COLOR: gray">.</SPAN>MyTVP <SPAN style="COLOR: blue">AS</SPAN> <SPAN style="COLOR: blue">TABLE</SPAN> <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'">(<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp; c1 <SPAN style="COLOR: blue">int<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp; <SPAN style="COLOR: gray">,</SPAN>c2 <SPAN style="COLOR: blue">int<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'">)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">GO<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">Create</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> <SPAN style="COLOR: blue">proc</SPAN> MyProc<SPAN style="COLOR: blue"> </SPAN><SPAN style="COLOR: gray">(</SPAN>@tvp dbo<SPAN style="COLOR: gray">.</SPAN>MyTVP <SPAN style="COLOR: blue">readonly</SPAN><SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">as<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">begin<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp; <SPAN style="COLOR: blue">insert</SPAN> <SPAN style="COLOR: blue">into</SPAN> MyTvpTargetTable<SPAN style="COLOR: blue"> </SPAN><SPAN style="COLOR: gray">(</SPAN>c1<SPAN style="COLOR: gray">,</SPAN>c2<SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp; <SPAN style="COLOR: blue">select</SPAN> c1<SPAN style="COLOR: gray">,</SPAN> c2 <SPAN style="COLOR: blue">from</SPAN> @tvp<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">end<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">GO<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><B><FONT size=3><FONT face=Calibri>Now the first approach, using ADO.Net DataTableReader with TVP<o:p></o:p></FONT></FONT></B></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Calibri size=3>In the nutshell, a DataTable is used to cache our accumulated data.&nbsp; Once it reaches a size, we use a DataTableReader to read it.&nbsp; We also create a SqlCommand that is of StoredProcedure CommandType.&nbsp; The SqlCommand has a SqlParameter which has the value of the DataTableReader.&nbsp; The code goes like this:</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">static</SPAN> <SPAN style="COLOR: blue">void</SPAN> InsertWithDataTableReader(<SPAN style="COLOR: blue">int</SPAN> rowCount)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp; {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">// used to time how fast this function runs later</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #2b91af">DateTime</SPAN> dt = <SPAN style="COLOR: #2b91af">DateTime</SPAN>.Now;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">// Create a new DataTable</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #2b91af">DataTable</SPAN> table = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">DataTable</SPAN>(<SPAN style="COLOR: #a31515">"tvp"</SPAN>);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">// Declare variables for DataColumn and DataRow objects.</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #2b91af">DataColumn</SPAN> column;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #2b91af">DataRow</SPAN> row;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">// Create new DataColumn, set DataType, </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">// ColumnName and add to DataTable.&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; column = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">DataColumn</SPAN>();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; column.DataType = System.<SPAN style="COLOR: #2b91af">Type</SPAN>.GetType(<SPAN style="COLOR: #a31515">"System.Int32"</SPAN>);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; column.ColumnName = <SPAN style="COLOR: #a31515">"c1"</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; column.ReadOnly = <SPAN style="COLOR: blue">true</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">// Add the Column to the DataColumnCollection.</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; table.Columns.Add(column);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">// Create second column.</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; column = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">DataColumn</SPAN>();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; column.DataType = System.<SPAN style="COLOR: #2b91af">Type</SPAN>.GetType(<SPAN style="COLOR: #a31515">"System.Int32"</SPAN>);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp; column.ColumnName = <SPAN style="COLOR: #a31515">"c2"</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; column.ReadOnly = <SPAN style="COLOR: blue">true</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">// Add the Column to the DataColumnCollection.</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; table.Columns.Add(column);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">// Imagine how you can accumulate your data in this DataTable.</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">// When it reaches a size, we will bulk load to SQL</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">for</SPAN> (<SPAN style="COLOR: blue">int</SPAN> i = 1; i &lt;= rowCount; i++)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">// Use a DataRow object and add </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">// it to the DataTable.</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; row = table.NewRow();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; row[<SPAN style="COLOR: #a31515">"c1"</SPAN>] = i;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; row[<SPAN style="COLOR: #a31515">"c2"</SPAN>] = -i;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; table.Rows.Add(row);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">// To read the DataTable and to be passed in as value of the SqlParameter</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #2b91af">DataTableReader</SPAN> dtReader = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">DataTableReader</SPAN>(table);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #2b91af">SqlConnection</SPAN> sqlconnection = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">SqlConnection</SPAN>(<SPAN style="COLOR: #a31515">"Persist Security Info=False;Integrated Security=true;Initial Catalog=DemoDB;Server=(local)"</SPAN>);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sqlconnection.Open();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">// Configure the SqlCommand and table-valued parameter.</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #2b91af">SqlCommand</SPAN> insertCommand = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">SqlCommand</SPAN>(<SPAN style="COLOR: #a31515">"MyProc"</SPAN>, sqlconnection);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; insertCommand.CommandType = <SPAN style="COLOR: #2b91af">CommandType</SPAN>.StoredProcedure;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #2b91af">SqlParameter</SPAN> tvpParam =<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; insertCommand.Parameters.AddWithValue(<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #a31515">"@tvp"</SPAN>, dtReader);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tvpParam.SqlDbType = <SPAN style="COLOR: #2b91af">SqlDbType</SPAN>.Structured;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">// Execute the command.</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; insertCommand.ExecuteNonQuery();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">//Time how fast we run this function</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #2b91af">Console</SPAN>.WriteLine(<SPAN style="COLOR: #2b91af">DateTime</SPAN>.Now - dt);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas">&nbsp;&nbsp;&nbsp; }<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Calibri size=3>This function can be used in a console app.&nbsp; You will also need to reference these assemblies:</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; COLOR: blue; FONT-FAMILY: Consolas">using</SPAN><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas"> System.Data.SqlClient;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; COLOR: blue; FONT-FAMILY: Consolas">using</SPAN><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas"> System.Data.Sql;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; COLOR: blue; FONT-FAMILY: Consolas">using</SPAN><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas"> System.Data.Common;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; COLOR: blue; FONT-FAMILY: Consolas">using</SPAN><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas"> System.Data;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 9.5pt; FONT-FAMILY: Consolas"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Calibri size=3>My test method is to call this method ten times and each time it will "accumulate" 1,000 rows.&nbsp; So we have a test load of 10,000 rows:</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">static</SPAN> <SPAN style="COLOR: blue">void</SPAN> Main(<SPAN style="COLOR: blue">string</SPAN>[] args)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp; {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #2b91af">DateTime</SPAN> dt = <SPAN style="COLOR: #2b91af">DateTime</SPAN>.Now;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">for</SPAN> (<SPAN style="COLOR: blue">int</SPAN> i = 0; i &lt; 10; i++)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; InsertWithDataTableReader(1000);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">// InsertWithDynamicSQL(1000);<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #2b91af">Console</SPAN>.Write(<SPAN style="COLOR: #a31515">"total run time: "</SPAN>);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #2b91af">Console</SPAN>.WriteLine(<SPAN style="COLOR: #2b91af">DateTime</SPAN>.Now - dt);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #2b91af">Console</SPAN>.Read();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp; }<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Calibri size=3>It took 236 ms on my box to insert 10,000 rows (10 batches x 1000 rows).&nbsp; It's 42373 rows per second.&nbsp; Pretty fast.&nbsp; This approach scales very good too.&nbsp; I tried a few different batch sizes for a few rounds and the performance is consistent and it has a better throughput when the batch sizes increase.&nbsp; Since the streaming of data is robust, as the data volume increases, the overhead of various client side .Net classes is also diluted.&nbsp; The throughput ramps up after reaching 500,000 rows and stay at above 110,000 rows per seconds.&nbsp; See below for comparison.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<TABLE class=MsoNormalTable style="MARGIN: auto auto auto -0.75pt; WIDTH: 185pt; BORDER-COLLAPSE: collapse; mso-yfti-tbllook: 1184; mso-padding-alt: 0in 0in 0in 0in" cellSpacing=0 cellPadding=0 width=247 border=0 class="MsoNormalTable">
<TBODY>
<TR style="HEIGHT: 15pt; mso-yfti-irow: 0; mso-yfti-firstrow: yes">
<TD class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: windowtext 1pt solid; WIDTH: 48pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" vAlign=bottom noWrap width=64>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="COLOR: black"><FONT size=3><FONT face=Calibri>Rows<o:p></o:p></FONT></FONT></SPAN></P></TD>
<TD class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #f0f0f0; WIDTH: 48pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" vAlign=bottom noWrap width=64>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="COLOR: black"><FONT size=3><FONT face=Calibri>Seconds<o:p></o:p></FONT></FONT></SPAN></P></TD>
<TD class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #f0f0f0; WIDTH: 89pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" vAlign=bottom noWrap width=119>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="COLOR: black"><FONT size=3><FONT face=Calibri>Rows / S<o:p></o:p></FONT></FONT></SPAN></P></TD></TR>
<TR style="HEIGHT: 15pt; mso-yfti-irow: 1">
<TD class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: windowtext 1pt solid; WIDTH: 48pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" vAlign=bottom noWrap width=64>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: right" align=right><SPAN style="COLOR: black"><FONT size=3><FONT face=Calibri>10000<o:p></o:p></FONT></FONT></SPAN></P></TD>
<TD class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #f0f0f0; WIDTH: 48pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" vAlign=bottom noWrap width=64>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: right" align=right><SPAN style="COLOR: black"><FONT size=3><FONT face=Calibri>0.236<o:p></o:p></FONT></FONT></SPAN></P></TD>
<TD class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #f0f0f0; WIDTH: 89pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" vAlign=bottom noWrap width=119>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: right" align=right><SPAN style="COLOR: black"><FONT size=3><FONT face=Calibri>42372.88<o:p></o:p></FONT></FONT></SPAN></P></TD></TR>
<TR style="HEIGHT: 15pt; mso-yfti-irow: 2">
<TD class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: windowtext 1pt solid; WIDTH: 48pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" vAlign=bottom noWrap width=64>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: right" align=right><SPAN style="COLOR: black"><FONT size=3><FONT face=Calibri>50000<o:p></o:p></FONT></FONT></SPAN></P></TD>
<TD class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #f0f0f0; WIDTH: 48pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" vAlign=bottom noWrap width=64>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: right" align=right><SPAN style="COLOR: black"><FONT size=3><FONT face=Calibri>0.578<o:p></o:p></FONT></FONT></SPAN></P></TD>
<TD class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #f0f0f0; WIDTH: 89pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" vAlign=bottom noWrap width=119>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: right" align=right><SPAN style="COLOR: black"><FONT size=3><FONT face=Calibri>86505.19<o:p></o:p></FONT></FONT></SPAN></P></TD></TR>
<TR style="HEIGHT: 15pt; mso-yfti-irow: 3">
<TD class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: windowtext 1pt solid; WIDTH: 48pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" vAlign=bottom noWrap width=64>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: right" align=right><SPAN style="COLOR: black"><FONT size=3><FONT face=Calibri>100000<o:p></o:p></FONT></FONT></SPAN></P></TD>
<TD class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #f0f0f0; WIDTH: 48pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" vAlign=bottom noWrap width=64>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: right" align=right><SPAN style="COLOR: black"><FONT size=3><FONT face=Calibri>0.972<o:p></o:p></FONT></FONT></SPAN></P></TD>
<TD class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #f0f0f0; WIDTH: 89pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" vAlign=bottom noWrap width=119>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: right" align=right><SPAN style="COLOR: black"><FONT size=3><FONT face=Calibri>102880.66<o:p></o:p></FONT></FONT></SPAN></P></TD></TR>
<TR style="HEIGHT: 15pt; mso-yfti-irow: 4">
<TD class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: windowtext 1pt solid; WIDTH: 48pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" vAlign=bottom noWrap width=64>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: right" align=right><SPAN style="COLOR: black"><FONT size=3><FONT face=Calibri>500000<o:p></o:p></FONT></FONT></SPAN></P></TD>
<TD class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #f0f0f0; WIDTH: 48pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" vAlign=bottom noWrap width=64>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: right" align=right><SPAN style="COLOR: black"><FONT size=3><FONT face=Calibri>4.4<o:p></o:p></FONT></FONT></SPAN></P></TD>
<TD class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #f0f0f0; WIDTH: 89pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" vAlign=bottom noWrap width=119>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: right" align=right><SPAN style="COLOR: black"><FONT size=3><FONT face=Calibri>113636.36<o:p></o:p></FONT></FONT></SPAN></P></TD></TR>
<TR style="HEIGHT: 15pt; mso-yfti-irow: 5">
<TD class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: windowtext 1pt solid; WIDTH: 48pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" vAlign=bottom noWrap width=64>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: right" align=right><SPAN style="COLOR: black"><FONT size=3><FONT face=Calibri>1000000<o:p></o:p></FONT></FONT></SPAN></P></TD>
<TD class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #f0f0f0; WIDTH: 48pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" vAlign=bottom noWrap width=64>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: right" align=right><SPAN style="COLOR: black"><FONT size=3><FONT face=Calibri>8.7<o:p></o:p></FONT></FONT></SPAN></P></TD>
<TD class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #f0f0f0; WIDTH: 89pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" vAlign=bottom noWrap width=119>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: right" align=right><SPAN style="COLOR: black"><FONT size=3><FONT face=Calibri>114942.53<o:p></o:p></FONT></FONT></SPAN></P></TD></TR>
<TR style="HEIGHT: 15pt; mso-yfti-irow: 6; mso-yfti-lastrow: yes">
<TD class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: windowtext 1pt solid; WIDTH: 48pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" vAlign=bottom noWrap width=64>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: right" align=right><SPAN style="COLOR: black"><FONT size=3><FONT face=Calibri>5000000<o:p></o:p></FONT></FONT></SPAN></P></TD>
<TD class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #f0f0f0; WIDTH: 48pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" vAlign=bottom noWrap width=64>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: right" align=right><SPAN style="COLOR: black"><FONT size=3><FONT face=Calibri>44.5<o:p></o:p></FONT></FONT></SPAN></P></TD>
<TD class="" style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #f0f0f0; WIDTH: 89pt; PADDING-TOP: 0in; BORDER-BOTTOM: windowtext 1pt solid; HEIGHT: 15pt; BACKGROUND-COLOR: transparent" vAlign=bottom noWrap width=119>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: right" align=right><SPAN style="COLOR: black"><FONT size=3><FONT face=Calibri>112359.55<o:p></o:p></FONT></FONT></SPAN></P></TD></TR></TBODY></TABLE>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Calibri size=3>On the SQL side, it's pretty calm.&nbsp; There's no CPU time used to parse and compile.&nbsp; This is a proc call after all.&nbsp; Most CPU time is used to insert data.&nbsp; One million row insertion takes 4,413 ms CPU time.&nbsp; Not a whole lot of ticks and seems very reasonable to me.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Calibri size=3>One thing worth mentioning in the profiler trace is the TextData of the RPC:Completed event of the proc call.&nbsp; It looks like this and is misleading:</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">declare</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> @p1 dbo<SPAN style="COLOR: gray">.</SPAN>myTVP<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">insert</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> <SPAN style="COLOR: blue">into</SPAN> @p1 <SPAN style="COLOR: blue">values</SPAN><SPAN style="COLOR: gray">(</SPAN>1<SPAN style="COLOR: gray">,-</SPAN>1<SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">insert</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> <SPAN style="COLOR: blue">into</SPAN> @p1 <SPAN style="COLOR: blue">values</SPAN><SPAN style="COLOR: gray">(</SPAN>2<SPAN style="COLOR: gray">,-</SPAN>2<SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">insert</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> <SPAN style="COLOR: blue">into</SPAN> @p1 <SPAN style="COLOR: blue">values</SPAN><SPAN style="COLOR: gray">(</SPAN>3<SPAN style="COLOR: gray">,-</SPAN>3<SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">insert</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> <SPAN style="COLOR: blue">into</SPAN> @p1 <SPAN style="COLOR: blue">values</SPAN><SPAN style="COLOR: gray">(</SPAN>4<SPAN style="COLOR: gray">,-</SPAN>4<SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">insert</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> <SPAN style="COLOR: blue">into</SPAN> @p1 <SPAN style="COLOR: blue">values</SPAN><SPAN style="COLOR: gray">(</SPAN>5<SPAN style="COLOR: gray">,-</SPAN>5<SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'">......<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">insert</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> <SPAN style="COLOR: blue">into</SPAN> @p1 <SPAN style="COLOR: blue">values</SPAN><SPAN style="COLOR: gray">(</SPAN>1000<SPAN style="COLOR: gray">,-</SPAN>1000<SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">exec</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> MyProc<SPAN style="COLOR: blue"> </SPAN>@tvp<SPAN style="COLOR: gray">=</SPAN>@p1<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT size=3><FONT face=Calibri>This seems like a bunch of singleton calls&nbsp; but it's not.&nbsp; As my colleage Keith Elmore at Microsoft CSS pointed out, <I><SPAN style="COLOR: #1f497d">that is just the convention used to allow the data to be displayed or copied/pasted into a query window and run.&nbsp; The TVP isn’t actually implemented in that way—the rows are streamed across just like a bulk insert—but it may have led to this impression that it does simple inserts.</SPAN> </I></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><B><FONT size=3><FONT face=Calibri>Then there's second approach using ADO.Net to construct a SQL Dynamic TSQL.<o:p></o:p></FONT></FONT></B></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Calibri size=3>This approach is often frowned upon for the SQL injection security concern.&nbsp; It's got okay performance, compared to the first approach.&nbsp; It put weights on SQL server and does not scale well.&nbsp; I listed this approach as a side by side comparison with the first approach.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Calibri size=3>I am not going to elaborate much.&nbsp; Basically, in ADO.Net we construct a dynamic SQL to be executed at the SQL server side.&nbsp; There's not much at the client except the string concatenation.&nbsp; So what kind of string we should build?&nbsp; Obviously, you don't want to build a bunch of single inserts like we just saw above in the first approach.&nbsp; It's running very slow.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Calibri size=3>So how about a single VALUES set with all the rows like this?</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">declare</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> @p1 dbo<SPAN style="COLOR: gray">.</SPAN>myTVP<SPAN style="COLOR: gray">;</SPAN> <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">insert</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> <SPAN style="COLOR: blue">into</SPAN> @p1 <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">select</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> c1<SPAN style="COLOR: gray">,</SPAN> c2 <SPAN style="COLOR: blue">from </SPAN><SPAN style="COLOR: gray">(</SPAN><SPAN style="COLOR: blue">values</SPAN><SPAN style="COLOR: gray">(</SPAN>1<SPAN style="COLOR: gray">,-</SPAN>1<SPAN style="COLOR: gray">),(</SPAN>2<SPAN style="COLOR: gray">,-</SPAN>2<SPAN style="COLOR: gray">),(</SPAN>3<SPAN style="COLOR: gray">,-</SPAN>3<SPAN style="COLOR: gray">),......,(</SPAN>3000<SPAN style="COLOR: gray">,-</SPAN>3000<SPAN style="COLOR: gray">))</SPAN> <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">as</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> alias<SPAN style="COLOR: blue"> </SPAN><SPAN style="COLOR: gray">(</SPAN>c1<SPAN style="COLOR: gray">,</SPAN>c2<SPAN style="COLOR: gray">)</SPAN> <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'">;</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">exec</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> myproc<SPAN style="COLOR: blue"> </SPAN>@tvp<SPAN style="COLOR: gray">=</SPAN>@p1</SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Calibri size=3>It works but can't scale.&nbsp; When I try a batch size of 10,000 rows, my console app times out.&nbsp; So a compromise is to build batches of 1000 rows and UNION ALL together:</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">declare</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> @p1 dbo<SPAN style="COLOR: gray">.</SPAN>myTVP<SPAN style="COLOR: gray">;</SPAN> <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">insert</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> <SPAN style="COLOR: blue">into</SPAN> @p1 <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">select</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> c1<SPAN style="COLOR: gray">,</SPAN> c2 <SPAN style="COLOR: blue">from </SPAN><SPAN style="COLOR: gray">(</SPAN><SPAN style="COLOR: blue">values</SPAN><SPAN style="COLOR: gray">(</SPAN>1<SPAN style="COLOR: gray">,-</SPAN>1<SPAN style="COLOR: gray">),(</SPAN>2<SPAN style="COLOR: gray">,-</SPAN>2<SPAN style="COLOR: gray">),(</SPAN>3<SPAN style="COLOR: gray">,-</SPAN>3<SPAN style="COLOR: gray">),......,(</SPAN>1000<SPAN style="COLOR: gray">,-</SPAN>1000<SPAN style="COLOR: gray">))</SPAN> <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">as</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> alias<SPAN style="COLOR: blue"> </SPAN><SPAN style="COLOR: gray">(</SPAN>c1<SPAN style="COLOR: gray">,</SPAN>c2<SPAN style="COLOR: gray">)</SPAN> <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">union</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> <SPAN style="COLOR: gray">all</SPAN>&nbsp; <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">select</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> c1<SPAN style="COLOR: gray">,</SPAN> c2 <SPAN style="COLOR: blue">from </SPAN><SPAN style="COLOR: gray">(</SPAN><SPAN style="COLOR: blue">values</SPAN><SPAN style="COLOR: gray">(</SPAN>1001<SPAN style="COLOR: gray">,-</SPAN>1001<SPAN style="COLOR: gray">),(</SPAN>1002<SPAN style="COLOR: gray">,-</SPAN>1002<SPAN style="COLOR: gray">),(</SPAN>1003<SPAN style="COLOR: gray">,-</SPAN>1003<SPAN style="COLOR: gray">),......,(</SPAN>2000<SPAN style="COLOR: gray">,-</SPAN>2000<SPAN style="COLOR: gray">))</SPAN> <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">as</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> alias<SPAN style="COLOR: blue"> </SPAN><SPAN style="COLOR: gray">(</SPAN>c1<SPAN style="COLOR: gray">,</SPAN>c2<SPAN style="COLOR: gray">)</SPAN> <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">union</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> <SPAN style="COLOR: gray">all</SPAN>&nbsp; <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">select</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> c1<SPAN style="COLOR: gray">,</SPAN> c2 <SPAN style="COLOR: blue">from </SPAN><SPAN style="COLOR: gray">(</SPAN><SPAN style="COLOR: blue">values</SPAN><SPAN style="COLOR: gray">(</SPAN>2001<SPAN style="COLOR: gray">,-</SPAN>2001<SPAN style="COLOR: gray">),(</SPAN>2002<SPAN style="COLOR: gray">,-</SPAN>2002<SPAN style="COLOR: gray">),(</SPAN>2003<SPAN style="COLOR: gray">,-</SPAN>2003<SPAN style="COLOR: gray">),......,(</SPAN>3000<SPAN style="COLOR: gray">,-</SPAN>3000<SPAN style="COLOR: gray">))</SPAN> <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">as</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> alias<SPAN style="COLOR: blue"> </SPAN><SPAN style="COLOR: gray">(</SPAN>c1<SPAN style="COLOR: gray">,</SPAN>c2<SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'">;</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'">exec</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"> myproc<SPAN style="COLOR: blue"> </SPAN>@tvp<SPAN style="COLOR: gray">=</SPAN>@p1<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Calibri size=3>The C# function to build such a string to be passed to SQL can be seen here:</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">static</SPAN> <SPAN style="COLOR: blue">void</SPAN> InsertWithDynamicSQL(<SPAN style="COLOR: blue">int</SPAN> rowCount)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp; {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #2b91af">DateTime</SPAN> dt = <SPAN style="COLOR: #2b91af">DateTime</SPAN>.Now;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #2b91af">StringBuilder</SPAN> sql = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">StringBuilder</SPAN>(<SPAN style="COLOR: #a31515">"declare @p1 dbo.myTVP; insert into @p1 select c1, c2 from (values"</SPAN>);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">for</SPAN> (<SPAN style="COLOR: blue">int</SPAN> i = 1; i &lt;= rowCount; i++)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sql.Append(<SPAN style="COLOR: #a31515">"("</SPAN>);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sql.Append(i.ToString() + <SPAN style="COLOR: #a31515">","</SPAN> + (-i).ToString());<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sql.Append(<SPAN style="COLOR: #a31515">")"</SPAN>);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">if</SPAN> (i == rowCount)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sql.Append(<SPAN style="COLOR: #a31515">") as alias (c1,c2)"</SPAN>);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">break</SPAN>;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">if</SPAN> (i % 1000 == 0)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sql.Append(<SPAN style="COLOR: #a31515">") as a1 (c1,c2) union all&nbsp; select c1, c2 from (values"</SPAN>);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: blue">else<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sql.Append(<SPAN style="COLOR: #a31515">","</SPAN>);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sql.Append(<SPAN style="COLOR: #a31515">";exec myproc @tvp=@p1"</SPAN>);</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #2b91af">SqlConnection</SPAN> sqlconnection = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">SqlConnection</SPAN>(<SPAN style="COLOR: #a31515">"Persist Security Info=False;Integrated Security=true;Initial Catalog=DemoDB;Server=(local)"</SPAN>);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sqlconnection.Open();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">// Configure the SqlCommand and table-valued parameter.<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #2b91af">SqlCommand</SPAN> insertCommand = <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">SqlCommand</SPAN>(sql.ToString(), sqlconnection);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; insertCommand.CommandType = <SPAN style="COLOR: #2b91af">CommandType</SPAN>.Text;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: green">// Execute the command.<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; insertCommand.ExecuteNonQuery();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <SPAN style="COLOR: #2b91af">Console</SPAN>.WriteLine(<SPAN style="COLOR: #2b91af">DateTime</SPAN>.Now - dt);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'">&nbsp;&nbsp;&nbsp; }<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Calibri size=3>The approach puts a lot of pressures on the SQL side in the form of high CPU usage.&nbsp; As we remember in the first approach, the CPU time used to insert one million rows into SQL is mere 4.4 seconds.&nbsp; In this dynamic SQL approach, just to parse and compile a 10,000 row batch takes 4.4 seconds.&nbsp; We are not even executing the batch yet!</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Calibri size=3>Here's the parsing and compilation time (no execution time) for different rows.&nbsp; I can't get any higher batch row number as it times out.&nbsp; You get the idea of how this doesn't scale well.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in; TEXT-ALIGN: right" align=right><FONT face=Calibri size=3>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></P>
<TABLE class=MsoNormalTable style="MARGIN: auto auto auto 30.2pt; BORDER-COLLAPSE: collapse; mso-yfti-tbllook: 1184; mso-padding-alt: 0in 0in 0in 0in" cellSpacing=0 cellPadding=0 border=0 class="MsoNormalTable">
<TBODY>
<TR style="mso-yfti-irow: 0; mso-yfti-firstrow: yes">
<TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: black 1pt solid; WIDTH: 95.4pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent" vAlign=top width=127>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: right" align=right><FONT face=Calibri size=3>1000 rows</FONT></P></TD>
<TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #f0f0f0; WIDTH: 1.25in; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent" vAlign=top width=120>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: right" align=right><FONT face=Calibri size=3>317ms</FONT></P></TD></TR>
<TR style="mso-yfti-irow: 1">
<TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: black 1pt solid; WIDTH: 95.4pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent" vAlign=top width=127>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: right" align=right><FONT face=Calibri size=3>10000 rows</FONT></P></TD>
<TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #f0f0f0; WIDTH: 1.25in; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent" vAlign=top width=120>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: right" align=right><FONT face=Calibri size=3>4400ms</FONT></P></TD></TR>
<TR style="mso-yfti-irow: 2; mso-yfti-lastrow: yes">
<TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: black 1pt solid; WIDTH: 95.4pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent" vAlign=top width=127>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: right" align=right><FONT face=Calibri size=3>30000 rows</FONT></P></TD>
<TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #f0f0f0; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #f0f0f0; WIDTH: 1.25in; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent" vAlign=top width=120>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: right" align=right><FONT face=Calibri size=3>13860ms</FONT></P></TD></TR></TBODY></TABLE>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in; TEXT-ALIGN: right" align=right><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Calibri size=3>While compilation is a resource (CPU) intensive operation, all the compiled execution plans are mostly used just once.&nbsp; Two batches must match character by character so that the execution plan can be reused.&nbsp; Since our real world data is constantly changing, it's virtually impossible to have two same batches.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><B><FONT size=3><FONT face=Calibri>Summary<o:p></o:p></FONT></FONT></B></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Calibri size=3>I think we have a clear winner here from all perspectives.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Calibri size=3>The first approach with DataTable, DataTableReader and TVP is robust, fast and scale well.&nbsp; It uses much less resource and put less strains on SQL server.&nbsp; This is important for scale out.&nbsp; SQL can have multiple clients all streaming data in without bringing down the database.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Calibri size=3>If you have any better solution, show it off!&nbsp; Otherwise, leave a comment and let me know if this post is helpful.</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Calibri size=3>Happy coding!</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT face=Calibri size=3>Mike Chang (Chien-Shen)</FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>SQL SDE, MSDN</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>]]></content:encoded>
    </item>
    <item>
      <title>Large Scale DW/BI Design Considerations</title>
      <link>https://docs.microsoft.com/archive/blogs/mikecha/large-scale-dwbi-design-considerations</link>
      <pubDate>Wed, 29 Jul 2009 02:29:00 GMT</pubDate>
      <dc:creator><![CDATA[mikecha]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/mikecha/2009/07/29/large-scale-dwbi-design-considerations/</guid>
      <description><![CDATA[&nbsp;
One of the most challenging aspects of large scale data warehouse development is the sheer...]]></description>
      <content:encoded><![CDATA[<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><FONT size=3><FONT face=Calibri>One of the most challenging aspects of large scale data warehouse development is the sheer immense size.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>It’s common to see all sorts of problems once the data warehouse gets larger.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Common problems include performance degradation, out of storage space, long running queries, blocking and failure to keep up with data feeds.<o:p></o:p></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><FONT size=3><FONT face=Calibri>I have compiled a list of tips that help me to manage the size effectively over the years on terabyte DW projects.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>I hope they will be beneficial to you as well.<SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>Here we go:<o:p></o:p></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT size=3><FONT face=Calibri><B style="mso-bidi-font-weight: normal"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi">Keep transaction small:</SPAN></B><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>The goal is to reduce blocking or even deadlocks.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>In my own implementation, there's almost no usage of BEGIN TRAN...... COMMIT TRAN.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Sometimes, there's obvious place you can tweak to keep transaction small.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>For example, let’s say you have a data source that contains both new dimension and fact data.<SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>They should be able to put in two different transactions.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Now, it's not always easy to separate statements of a transaction into individual ones since it may affect the data accuracy.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>For example, if you have an ETL workitem queue table where you keep track of what raw data has been processed.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>To avoid double counting, the insertion into fact table and updating completion of<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>the workitem in the queue table are usually in the same transaction.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Separating them can still be achieved and that leads to my next tip, Fault tolerant and robust design.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN><o:p></o:p></SPAN></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT size=3><FONT face=Calibri><B style="mso-bidi-font-weight: normal"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi">Fault tolerant and robust design:</SPAN></B><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>This is a design principle that requires a different approach of schema design and implementation.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>With the goal to keep transaction small and short, the goal is to only touch one table at a time.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Using the workitem queue table and fact table example again, without a transaction, the logic goes like this.<o:p></o:p></SPAN></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><SPAN style="mso-list: Ignore"><FONT face=Calibri size=3>1.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><FONT size=3><FONT face=Calibri>Mark workitem processing start time<o:p></o:p></FONT></FONT></SPAN></P>
<P class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><SPAN style="mso-list: Ignore"><FONT face=Calibri size=3>2.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><FONT size=3><FONT face=Calibri>Delete fact table if there's any row associated with the workitem<o:p></o:p></FONT></FONT></SPAN></P>
<P class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><SPAN style="mso-list: Ignore"><FONT face=Calibri size=3>3.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><FONT size=3><FONT face=Calibri>Process <o:p></o:p></FONT></FONT></SPAN></P>
<P class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><SPAN style="mso-list: Ignore"><FONT face=Calibri size=3>4.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><FONT size=3><FONT face=Calibri>Insert fact table with new rows from the workitem<o:p></o:p></FONT></FONT></SPAN></P>
<P class=MsoListParagraph style="MARGIN: 0in 0in 0pt 0.5in; TEXT-INDENT: -0.25in; mso-list: l0 level1 lfo1"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: Calibri; mso-fareast-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin"><SPAN style="mso-list: Ignore"><FONT face=Calibri size=3>5.</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><FONT size=3><FONT face=Calibri>Mark workitem processing end time<o:p></o:p></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><FONT size=3><FONT face=Calibri>The trick is on step 2 where we delete any left over, half way rows associated with the workitem in the fact table.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>In normal situation, there's nothing to delete and this step is no ops.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>However if there's a failure between 4 and 5, when we reprocess the same workitem, step 2 will remove the previous insertion, reprocess and insert fact table without double counting.<o:p></o:p></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><FONT size=3><FONT face=Calibri>Putting everything in a transaction is an easy implementation and it might have passed test, integration, PPE and runs smoothly in production.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>But one day after several months in production, you begin to see long blocking and even deadlocks.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>The time you spent on trouble shooting can be several times more expensive than if you'd have implemented differently.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>I will explain why this can happen in the next tip, Load as much data as possible in the test phase.<o:p></o:p></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT size=3><FONT face=Calibri><B style="mso-bidi-font-weight: normal"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi">Load as much data as possible in the test phase: <SPAN style="mso-spacerun: yes">&nbsp;&nbsp;</SPAN></SPAN></B><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi">When designed and implemented correctly, the ETL time for the same amount of raw data should remain the same.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>However, it's common to see this ETL time taking longer gradually or going up to a cliff.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Common symptoms are index fragmentations, blocking and plan changes.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>However they all resulted from the same root cause-- underlying data changes.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Your data may be seasonal where some values become rare or frequent and that can change the stats.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>The data may grow too large to use the same access method.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Loop join in a plan can be changed to a hash or merge join.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>You bottle neck can hence be interchanged among disk, memory or CPU.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>The scenario goes on and on so allocate more time for you load testing and try as much data as possible during the dev/test phases.<o:p></o:p></SPAN></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT size=3><FONT face=Calibri><B style="mso-bidi-font-weight: normal"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi">Watch the batch size: </SPAN></B><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>This is orthogonal to the first tip.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>While we want to keep transaction small by avoiding touching several tables in one same transaction, we also want to make sure each transaction is limited to a manageable batch size.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Other than for the benefit of less blocking, a reasonable batch size can also help to alleviate the pressure on the log file size.<o:p></o:p></SPAN></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT size=3><FONT face=Calibri><B style="mso-bidi-font-weight: normal"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi">Use partitioned table:</SPAN></B><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"> This is one of the best new SQL server features for DW development.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>By using a rolling sliding window, you can effectively manage your vast amount of data.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Kimberly Tripp has done an excellent white paper on this topic at <A href="https://msdn.microsoft.com/en-us/library/ms345146(SQL.90).aspx" mce_href="https://msdn.microsoft.com/en-us/library/ms345146(SQL.90).aspx">https://msdn.microsoft.com/en-us/library/ms345146(SQL.90).aspx</A>.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>The only thing I want to add is that you should make the sliding window size configurable.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>When your data source grows, the DW may not be able to accommodate the same number of partitions.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Simply by decreasing the value of the partition number parameter to the procedure that manages the sliding window will get the disk space back in a split second.<o:p></o:p></SPAN></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT size=3><FONT face=Calibri><B style="mso-bidi-font-weight: normal"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi">Use Compression:</SPAN></B><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"> In my application of page level compression on the fact table, I see up to 66% saving on disk space while not observing any changes in the overall performance.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Some people may even see performance increase on IO since less disk space is used to store the same amount of information.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Read more at <A href="https://msdn.microsoft.com/en-us/library/cc280449.aspx" mce_href="https://msdn.microsoft.com/en-us/library/cc280449.aspx">https://msdn.microsoft.com/en-us/library/cc280449.aspx</A> and <A href="https://msdn.microsoft.com/en-us/library/cc280464.aspx" mce_href="https://msdn.microsoft.com/en-us/library/cc280464.aspx">https://msdn.microsoft.com/en-us/library/cc280464.aspx</A>.<o:p></o:p></SPAN></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT size=3><FONT face=Calibri><B style="mso-bidi-font-weight: normal"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi">Use Indexed view for aggregation:</SPAN></B><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>An indexed view is materialized on disk and the rollups are updated whenever the underlying fact data changes.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>This feature helps to keep the rollups current without a separate process to manage or calculate the rollups.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>The only thing need to be done is to define and create the indexed view.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Read more at <A href="https://msdn.microsoft.com/en-us/library/ms187864.aspx" mce_href="https://msdn.microsoft.com/en-us/library/ms187864.aspx">https://msdn.microsoft.com/en-us/library/ms187864.aspx</A>.<o:p></o:p></SPAN></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT size=3><FONT face=Calibri><B style="mso-bidi-font-weight: normal"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi">Design for Scale out: <SPAN style="mso-spacerun: yes">&nbsp;</SPAN></SPAN></B><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi">Two common reasons for scaling out are for processing throughput or storage space.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Unfortunately, most people found out they need to scale out when it’s too late after months in production.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>At that awkward moment, it’s going to be really hard to alleviate without a major overhaul if the system is never designed for scaling out.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>So always keep scaling out in mind when you architect the DW.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>For example, if your data source if file based, then it’s better to use UNC file path than local drive letter file path.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>The same idea goes to your destination fact table.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>It can be on different servers when scaling out.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Also, try to use procedures and avoid using base tables or views.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>With the abstraction of procedure interface, the data processing or retrieving can be from different servers and the insertion of data can scale out to different locations or without breaking the interface.<o:p></o:p></SPAN></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><FONT size=3><FONT face=Calibri>It's the hottest day I ever have in Seattle with the highest temparature at around 95F. (Don't laugh, Texans :))<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>I heard it can be triple digit, record setting hot tomorrow.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>So leave a comment if I mumble too much or if you think my head is over heated.&nbsp;<SPAN style="mso-spacerun: yes">&nbsp;</SPAN>:)<o:p></o:p></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><FONT size=3><FONT face=Calibri>Keep it cool and happy coding!<o:p></o:p></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><SPAN style="mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-font-family: 'Times New Roman'; mso-fareast-theme-font: minor-fareast; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-font-family: 'Times New Roman'; mso-bidi-theme-font: minor-bidi"><o:p><FONT face=Calibri size=3></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt" minmax_bound="true">Mike Chang<o:p minmax_bound="true">&nbsp;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt" minmax_bound="true"><o:p minmax_bound="true">SQL SDE, </o:p><o:p minmax_bound="true">MSDN</o:p></P></FONT></o:p></SPAN>]]></content:encoded>
    </item>
    <item>
      <title>SQL High CPU and TempDB Growth By Scalar UDF</title>
      <link>https://docs.microsoft.com/archive/blogs/mikecha/sql-high-cpu-and-tempdb-growth-by-scalar-udf</link>
      <pubDate>Tue, 19 May 2009 02:44:00 GMT</pubDate>
      <dc:creator><![CDATA[mikecha]]></dc:creator>
      <guid
        isPermaLink="false">https://blogs.msdn.microsoft.com/mikecha/2009/05/19/sql-high-cpu-and-tempdb-growth-by-scalar-udf/</guid>
      <description><![CDATA[Most high CPU troubleshooting technologies focus on execution plan compilation...]]></description>
      <content:encoded><![CDATA[<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">Most high CPU troubleshooting technologies focus on execution plan compilation and&nbsp;recompilation OR suboptimal plans such as hash or merge join, among others.&nbsp; We (MSDN SQL team) recently found out a unique scenario of using scalar UDF (user defined function) and tempDB object (temp table or temp variable) that can also lead to high CPU usage as well as tempDB growth.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">In this&nbsp;post, I will show you an easy&nbsp;to understand, reproducible demo that you can&nbsp;try it out yourself.&nbsp; I will also use several performance counters&nbsp;to illustrate&nbsp;different anomalies by changing a section of the code.<o:p></o:p></SPAN></P>
<P><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">Preparation:<o:p></o:p></SPAN></B></P>
<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">First, let's create a simple table and a one liner scalar UDF:<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none" mce_keep="true"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">create</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">table</SPAN> tblDateDT<SPAN style="COLOR: blue"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN style="COLOR: gray">(</SPAN>DateID <SPAN style="COLOR: blue">int</SPAN> <SPAN style="COLOR: blue">identity</SPAN><SPAN style="COLOR: gray">(</SPAN>1<SPAN style="COLOR: gray">,</SPAN>1<SPAN style="COLOR: gray">)</SPAN> <SPAN style="COLOR: blue">primary</SPAN> <SPAN style="COLOR: blue">key</SPAN><SPAN style="COLOR: gray">,</SPAN> dtDate <SPAN style="COLOR: blue">datetime</SPAN><SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">go<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">declare</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> @i<SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">int</SPAN> <SPAN style="COLOR: gray">=</SPAN> 0<SPAN style="COLOR: gray">,<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>@cnt <SPAN style="COLOR: blue">int</SPAN> <SPAN style="COLOR: gray">=</SPAN> 1000000<SPAN style="COLOR: gray">,<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>@dt<SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: blue">datetime</SPAN> <SPAN style="COLOR: gray">=</SPAN> <SPAN style="COLOR: red">'2000-01-01'<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">-- Populate tblDateDT with one million rows<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">while</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> @i <SPAN style="COLOR: gray">&lt;</SPAN> @cnt<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">begin<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">insert</SPAN> tblDateDT<SPAN style="COLOR: blue"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN><SPAN style="COLOR: gray">(</SPAN>dtDate<SPAN style="COLOR: gray">)</SPAN> <SPAN style="COLOR: blue">values </SPAN><SPAN style="COLOR: gray">(</SPAN> <SPAN style="COLOR: fuchsia">dateadd</SPAN><SPAN style="COLOR: gray">(</SPAN><SPAN style="COLOR: fuchsia">day</SPAN><SPAN style="COLOR: gray">,</SPAN> @i<SPAN style="COLOR: gray">,</SPAN> @dt<SPAN style="COLOR: gray">)</SPAN> <SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">select</SPAN> @i <SPAN style="COLOR: gray">=</SPAN> @i <SPAN style="COLOR: gray">+</SPAN> 1<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">end</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">go<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Create</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">function</SPAN> dbo<SPAN style="COLOR: gray">.</SPAN>UDF_GetLongDateString<SPAN style="COLOR: blue"> </SPAN><SPAN style="COLOR: gray">(</SPAN>@dtDate <SPAN style="COLOR: blue">datetime</SPAN><SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">returns</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">varchar</SPAN><SPAN style="COLOR: gray">(</SPAN>1000<SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">as<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">begin<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">return</SPAN> <SPAN style="COLOR: fuchsia">convert</SPAN><SPAN style="COLOR: gray">(</SPAN><SPAN style="COLOR: blue">varchar</SPAN><SPAN style="COLOR: gray">(</SPAN>1000<SPAN style="COLOR: gray">),</SPAN> @dtDate<SPAN style="COLOR: gray">,</SPAN> 109<SPAN style="COLOR: gray">)</SPAN> <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">end</SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">;<o:p></o:p></SPAN></P>
<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">Next, select the following performance counters and start a trace:<o:p></o:p></SPAN></P>
<P>
<TABLE class=MsoTableGrid style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; MARGIN: auto auto auto 30.2pt; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-yfti-tbllook: 1184; mso-padding-alt: 0in 5.4pt 0in 5.4pt" cellSpacing=0 cellPadding=0 border=1 class="MsoTableGrid">
<TBODY>
<TR style="mso-yfti-irow: 0; mso-yfti-firstrow: yes">
<TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: black 1pt solid; WIDTH: 152.6pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt; mso-border-themecolor: text1" vAlign=top width=203>
<P><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">Object<o:p></o:p></SPAN></B></P></TD>
<TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 148.5pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1" vAlign=top width=198>
<P><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">Counter<o:p></o:p></SPAN></B></P></TD></TR>
<TR style="mso-yfti-irow: 1">
<TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: black 1pt solid; WIDTH: 152.6pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1" vAlign=top width=203>
<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">Processor<o:p></o:p></SPAN></P></TD>
<TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 148.5pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; mso-border-bottom-themecolor: text1; mso-border-right-themecolor: text1" vAlign=top width=198>
<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">% Processor Time<o:p></o:p></SPAN></P></TD></TR>
<TR style="mso-yfti-irow: 2">
<TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: black 1pt solid; WIDTH: 152.6pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1" vAlign=top width=203>
<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">SQLServer:Access Methods<o:p></o:p></SPAN></P></TD>
<TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 148.5pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; mso-border-bottom-themecolor: text1; mso-border-right-themecolor: text1" vAlign=top width=198>
<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">Index Searches/sec<o:p></o:p></SPAN></P></TD></TR>
<TR style="mso-yfti-irow: 3">
<TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: black 1pt solid; WIDTH: 152.6pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1" vAlign=top width=203>
<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">SQLServer:Access Methods<o:p></o:p></SPAN></P></TD>
<TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 148.5pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; mso-border-bottom-themecolor: text1; mso-border-right-themecolor: text1" vAlign=top width=198>
<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">Page Splits/sec<o:p></o:p></SPAN></P></TD></TR>
<TR style="mso-yfti-irow: 4; mso-yfti-lastrow: yes">
<TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: black 1pt solid; WIDTH: 152.6pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1" vAlign=top width=203>
<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">SQLServer:Transactions<o:p></o:p></SPAN></P></TD>
<TD class="" style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: #d4d0c8; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0in; BORDER-LEFT: #d4d0c8; WIDTH: 148.5pt; PADDING-TOP: 0in; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent; mso-border-alt: solid black .5pt; mso-border-themecolor: text1; mso-border-left-alt: solid black .5pt; mso-border-left-themecolor: text1; mso-border-top-alt: solid black .5pt; mso-border-top-themecolor: text1; mso-border-bottom-themecolor: text1; mso-border-right-themecolor: text1" vAlign=top width=198>
<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'">Free Space in tempdb (KB)<o:p></o:p></SPAN></P></TD></TR></TBODY></TABLE></P>
<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'"><o:p>&nbsp;</o:p></SPAN></P>
<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'"><STRONG>Baseline from Perf Counter Perspective:<o:p></o:p></STRONG></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT size=3><FONT face=Calibri>Let's first establish a "healthy" baseline without CPU and tempDB anomalies.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Run this simple select statement:<o:p></o:p></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">declare</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> @temp <SPAN style="COLOR: blue">table </SPAN><SPAN style="COLOR: gray">(</SPAN>DateID <SPAN style="COLOR: blue">int</SPAN><SPAN style="COLOR: gray">,</SPAN> strDate <SPAN style="COLOR: blue">varchar</SPAN><SPAN style="COLOR: gray">(</SPAN>1000<SPAN style="COLOR: gray">),</SPAN> dtDate <SPAN style="COLOR: blue">datetime</SPAN><SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">insert</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> @temp<SPAN style="COLOR: gray">(</SPAN>DateID<SPAN style="COLOR: gray">,</SPAN> strDate<SPAN style="COLOR: gray">,</SPAN> dtDate<SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">select</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><FONT color=#000000> dt</FONT><SPAN style="COLOR: gray">.</SPAN><FONT color=#000000>DateID</FONT><SPAN style="COLOR: gray">,</SPAN><FONT color=#000000> </FONT><SPAN style="COLOR: fuchsia">convert</SPAN><SPAN style="COLOR: gray">(</SPAN><SPAN style="COLOR: blue">varchar</SPAN><SPAN style="COLOR: gray">(</SPAN><FONT color=#000000>1000</FONT><SPAN style="COLOR: gray">),</SPAN><FONT color=#000000> dt</FONT><SPAN style="COLOR: gray">.</SPAN><FONT color=#000000>dtDate</FONT><SPAN style="COLOR: gray">,</SPAN><FONT color=#000000> 109</FONT><SPAN style="COLOR: gray">),</SPAN><FONT color=#000000> dt</FONT><SPAN style="COLOR: gray">.</SPAN><FONT color=#000000>dtDate </FONT><SPAN style="COLOR: blue">from</SPAN><FONT color=#000000> dbo</FONT><SPAN style="COLOR: gray">.</SPAN><FONT color=#000000>tblDateDT dt <o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"></SPAN><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P><o:p>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">select</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">top</SPAN> 1 <SPAN style="COLOR: gray">*</SPAN> <SPAN style="COLOR: blue">from</SPAN> @temp<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none" mce_keep="true">&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none">Here's the @@version of my&nbsp;test box: Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (X64)&nbsp;&nbsp; Enterprise Edition (64-bit) on Windows NT 6.0 &lt;X64&gt; (Build 6001: Service Pack 1).</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none">&nbsp;&nbsp;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT size=3><FONT face=Calibri>On my quad proc, 8G workstation, it took&nbsp;just one&nbsp;second to run.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Pause perfmon and you should see a graph like below.<o:p></o:p></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><IMG style="WIDTH: 601px; HEIGHT: 544px" height=544 src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/09/63/32/97/Perf_Baseline.gif" original-url="https://blogs.msdn.com/photos/mikecha/images/9633297/original.aspx" width=601 mce_src="https://blogs.msdn.com/photos/mikecha/images/9633297/original.aspx">&nbsp;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'"><STRONG>Figure 1</STRONG>: A baseline showing a CPU uptick and a tempDB free space dip<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><FONT size=3><FONT face=Calibri>As you can see, we have a slight CPU spike while in the meantime the free tempDB space dips a bit for populating the temp variable.<o:p></o:p></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri size=3>&nbsp;</FONT></o:p></P><o:p><FONT face=Calibri size=3>
<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'"><STRONG>Baseline from SQL Internal&nbsp;Perspective:<o:p></o:p></STRONG></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">Now&nbsp;let's get some baseline from SQL internal perspective.&nbsp; We will turn on statistics time,&nbsp;IO and profile options to see the duration, temp objects and execution plan.&nbsp; Run the same query but add the three extra statistics options</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true">&nbsp;</P><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">set</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><FONT color=#000000> </FONT><SPAN style="COLOR: blue">statistics</SPAN><FONT color=#000000> </FONT><SPAN style="COLOR: blue">time</SPAN><FONT color=#000000> </FONT><SPAN style="COLOR: blue">on<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">set</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><FONT color=#000000> </FONT><SPAN style="COLOR: blue">statistics</SPAN><FONT color=#000000> </FONT><SPAN style="COLOR: blue">profile</SPAN><FONT color=#000000> </FONT><SPAN style="COLOR: blue">on<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">set</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><FONT color=#000000> </FONT><SPAN style="COLOR: blue">statistics</SPAN><FONT color=#000000> </FONT><SPAN style="COLOR: blue">io</SPAN><FONT color=#000000> </FONT><SPAN style="COLOR: blue">on<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">go<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">declare</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><FONT color=#000000> @temp </FONT><SPAN style="COLOR: blue">table </SPAN><SPAN style="COLOR: gray">(</SPAN><FONT color=#000000>DateID </FONT><SPAN style="COLOR: blue">int</SPAN><SPAN style="COLOR: gray">,</SPAN><FONT color=#000000> strDate </FONT><SPAN style="COLOR: blue">varchar</SPAN><SPAN style="COLOR: gray">(</SPAN><FONT color=#000000>1000</FONT><SPAN style="COLOR: gray">),</SPAN><FONT color=#000000> dtDate </FONT><SPAN style="COLOR: blue">datetime</SPAN><SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"></SPAN></SPAN></SPAN>&nbsp;</P><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">insert</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> @temp<SPAN style="COLOR: gray">(</SPAN>DateID<SPAN style="COLOR: gray">,</SPAN> strDate<SPAN style="COLOR: gray">,</SPAN> dtDate<SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none">select</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><FONT color=#000000> dt</FONT><SPAN style="COLOR: gray">.</SPAN><FONT color=#000000>DateID</FONT><SPAN style="COLOR: gray">,</SPAN><FONT color=#000000> </FONT><SPAN style="COLOR: fuchsia">convert</SPAN><SPAN style="COLOR: gray">(</SPAN><SPAN style="COLOR: blue">varchar</SPAN><SPAN style="COLOR: gray">(</SPAN><FONT color=#000000>1000</FONT><SPAN style="COLOR: gray">),</SPAN><FONT color=#000000> dt</FONT><SPAN style="COLOR: gray">.</SPAN><FONT color=#000000>dtDate</FONT><SPAN style="COLOR: gray">,</SPAN><FONT color=#000000> 109</FONT><SPAN style="COLOR: gray">),</SPAN><FONT color=#000000> dt</FONT><SPAN style="COLOR: gray">.</SPAN><FONT color=#000000>dtDate </FONT><SPAN style="COLOR: blue">from</SPAN><FONT color=#000000> dbo</FONT><SPAN style="COLOR: gray">.</SPAN><FONT color=#000000>tblDateDT dt <o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"></SPAN><o:p><FONT face=Calibri color=#000000 size=3>&nbsp;</FONT></o:p></P><o:p>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">select</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">top</SPAN> 1 <SPAN style="COLOR: gray">*</SPAN> <SPAN style="COLOR: blue">from</SPAN> @temp</SPAN></P></o:p></SPAN>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">go<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">set</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><FONT color=#000000> </FONT><SPAN style="COLOR: blue">statistics</SPAN><FONT color=#000000> </FONT><SPAN style="COLOR: blue">time</SPAN><FONT color=#000000> </FONT><SPAN style="COLOR: blue">off<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">set</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><FONT color=#000000> </FONT><SPAN style="COLOR: blue">statistics</SPAN><FONT color=#000000> </FONT><SPAN style="COLOR: blue">profile</SPAN><FONT color=#000000> </FONT><SPAN style="COLOR: blue">off<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">set</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><FONT color=#000000> </FONT><SPAN style="COLOR: blue">statistics</SPAN><FONT color=#000000> </FONT><SPAN style="COLOR: blue">io</SPAN><FONT color=#000000> </FONT><SPAN style="COLOR: blue">off<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><FONT face=Calibri color=#000000 size=3>&nbsp;</FONT></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"></SPAN>And here is the output: (&nbsp;The output is truncated to the right&nbsp;so you may want to blow up your IE window if lines don't align well.)</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none" mce_keep="true">&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Table 'tblDateDT'. Scan count 1, logical reads 2609, physical reads 0, read-ahead reads 0, lob logical reads 0<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">(1000000 row(s) affected)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Rows<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Executes<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>StmtText<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">-------------------- -------------------- --------------------------------------------------------------------<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1000000<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN>insert @temp(DateID, strDate, dtDate)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">select dt.DateID, convert(varchar(1000), dt.dtDate, 109), dt.dtDate from dbo.tblDateDT dt<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1000000<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>|--Table Insert(OBJECT:(@temp), SET:([DateID] = [DemoDB].[dbo].[tb<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1000000<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>|--Top(ROWCOUNT est 0)<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">0<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>0<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>|--Compute Scalar(DEFINE:([Expr1006]=CONVERT(varchar(100<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1000000<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>|--Clustered Index Scan(OBJECT:([DemoDB].[dbo].[tbl<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">(5 row(s) affected)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>SQL Server Execution Times:<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>CPU time = 1014 ms,<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>elapsed time = 1100 ms.<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">DateID<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>strDate<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">----------- --------------------------------------------------------------------------------------------------<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Jan<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>1 2000 12:00:00:000AM<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">(1 row(s) affected)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Table '#2FCF1A8A'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, l<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Rows<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Executes<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>StmtText<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>StmtId<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>NodeId<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Parent<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">-------------------- -------------------- ------------------------------------- ----------- ----------- ------<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>select top 1 * from @temp<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>2<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>0<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>|--Top(TOP EXPRESSION:((1)))<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>2<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>2<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>|--Table Scan(OBJECT:(@temp))<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>2<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>3<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>2<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">(3 row(s) affected)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none" mce_keep="true">&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none" mce_keep="true">There is nothing unusual here.&nbsp; It took one second to scan the tblDateDT and then put it into temp table variable,&nbsp;#2FCF1A8A.&nbsp; The execution plan shows that there just one execution in each node and the data is kind of "streaming" into @temp.&nbsp; The inline Convert function is handled, well, "inlined".&nbsp; Later,&nbsp;when we&nbsp;replace the inline Convert function with the&nbsp;scalar UDF <FONT face="Courier New">UDF_GetLongDateString,</FONT>&nbsp;we will see the&nbsp;a change of the plan as well as how SQL handles the data flow.</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none" mce_keep="true">&nbsp;</P>
<P><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'"><STRONG>Use Scalar UDF to Insert&nbsp;Temp&nbsp;Table Variable:</STRONG></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none" mce_keep="true">&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none" mce_keep="true">The following script has the inline Convert function replaced by the scalar UDF, <FONT face="Courier New">dbo<SPAN style="COLOR: gray">.</SPAN>UDF_GetLongDateString<FONT color=#0000ff>.&nbsp; </FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none" mce_keep="true">&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none" mce_keep="true"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">declare</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> @temp <SPAN style="COLOR: blue">table </SPAN><SPAN style="COLOR: gray">(</SPAN>DateID <SPAN style="COLOR: blue">int</SPAN><SPAN style="COLOR: gray">,</SPAN> strDate <SPAN style="COLOR: blue">varchar</SPAN><SPAN style="COLOR: gray">(</SPAN>1000<SPAN style="COLOR: gray">),</SPAN> dtDate <SPAN style="COLOR: blue">datetime</SPAN><SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">insert</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> @temp<SPAN style="COLOR: gray">(</SPAN>DateID<SPAN style="COLOR: gray">,</SPAN> strDate<SPAN style="COLOR: gray">,</SPAN> dtDate<SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">select</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> dt<SPAN style="COLOR: gray">.</SPAN>DateID<SPAN style="COLOR: gray">,</SPAN> <U><STRONG>dbo<SPAN style="COLOR: gray">.</SPAN>UDF_GetLongDateString<SPAN style="COLOR: gray">(</SPAN>dt<SPAN style="COLOR: gray">.</SPAN>dtDate</STRONG></U><SPAN style="COLOR: gray"><U><STRONG>)</STRONG></U>,</SPAN> dt<SPAN style="COLOR: gray">.</SPAN>dtDate <SPAN style="COLOR: blue">from</SPAN> dbo<SPAN style="COLOR: gray">.</SPAN>tblDateDT dt <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p>&nbsp;</o:p></P><o:p>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">select</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">top</SPAN> 1 <SPAN style="COLOR: gray">*</SPAN> <SPAN style="COLOR: blue">from</SPAN> @temp<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">&nbsp;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p><STRONG>Perf Counter Analysis:</STRONG></o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p>&nbsp;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none" mce_keep="true">
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none" mce_keep="true">Resume&nbsp;the perfmon counter trace and run the script.&nbsp; Upon completion, pause the perfmon trace and you should see something similar like below.</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none" mce_keep="true">&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none" mce_keep="true"><IMG style="WIDTH: 601px; HEIGHT: 544px" height=544 src="https://msdnshared.blob.core.windows.net/media/MSDNBlogsFS/prod.evol.blogs.msdn.com/CommunityServer.Components.PostAttachments/00/09/63/32/92/Perf_ScalarUDF.gif" original-url="https://blogs.msdn.com/photos/mikecha/images/9633292/original.aspx" width=601 mce_src="https://blogs.msdn.com/photos/mikecha/images/9633292/original.aspx"></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none" mce_keep="true"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'"><STRONG>Figure 2</STRONG>: Compared to the baseline in the left, the center is the scalar UDF anomalies of extra&nbsp;tempDB&nbsp;usage, page&nbsp;splits and index searches.</SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none" mce_keep="true"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'"></SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'"></SPAN>&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none" mce_keep="true"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Arial','sans-serif'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none" mce_keep="true">The duration is a long 12 seconds on my box, 10 times longer than our base line.&nbsp; CPU usage spikes up from the get go and stays that way for the rest of the batch execution.&nbsp; Ignore the tiny spike&nbsp;two thirds of the course as it's backgound noise.&nbsp; The page splits and index searches counters not seen in the baseline stay plateaued and trend together&nbsp;with&nbsp;CPU usage.&nbsp; Lastly, the tempDB free space slowly chips off and has a sudden drop at the end.&nbsp; The space is reclaimed in two steps, at the end of the batch and about 3 seconds afterwards.</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none" mce_keep="true">&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none" mce_keep="true">While these are interesting trends but how do they correlate with each other?&nbsp; Stay with me, they will make more sense once we look into SQL internal.</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none" mce_keep="true">&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none" mce_keep="true"><STRONG>SQL Internal Analysis:</STRONG></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none" mce_keep="true">&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none" mce_keep="true">Let's run the same script with three statistics options on</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none" mce_keep="true">&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">set</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">statistics</SPAN> <SPAN style="COLOR: blue">time</SPAN> <SPAN style="COLOR: blue">on<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">set</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">statistics</SPAN> <SPAN style="COLOR: blue">profile</SPAN> <SPAN style="COLOR: blue">on<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">set</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">statistics</SPAN> <SPAN style="COLOR: blue">io</SPAN> <SPAN style="COLOR: blue">on<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">go<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">declare</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> @temp <SPAN style="COLOR: blue">table </SPAN><SPAN style="COLOR: gray">(</SPAN>DateID <SPAN style="COLOR: blue">int</SPAN><SPAN style="COLOR: gray">,</SPAN> strDate <SPAN style="COLOR: blue">varchar</SPAN><SPAN style="COLOR: gray">(</SPAN>1000<SPAN style="COLOR: gray">),</SPAN> dtDate <SPAN style="COLOR: blue">datetime</SPAN><SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: gray; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">insert</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> @temp<SPAN style="COLOR: gray">(</SPAN>DateID<SPAN style="COLOR: gray">,</SPAN> strDate<SPAN style="COLOR: gray">,</SPAN> dtDate<SPAN style="COLOR: gray">)<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">select</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> dt<SPAN style="COLOR: gray">.</SPAN>DateID<SPAN style="COLOR: gray">,</SPAN> dbo<SPAN style="COLOR: gray">.</SPAN>UDF_GetLongDateString<SPAN style="COLOR: gray">(</SPAN>dt<SPAN style="COLOR: gray">.</SPAN>dtDate<SPAN style="COLOR: gray">),</SPAN> dt<SPAN style="COLOR: gray">.</SPAN>dtDate <SPAN style="COLOR: blue">from</SPAN> dbo<SPAN style="COLOR: gray">.</SPAN>tblDateDT dt <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">select</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">top</SPAN> 1 <SPAN style="COLOR: gray">*</SPAN> <SPAN style="COLOR: blue">from</SPAN> @temp<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">go<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">set</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">statistics</SPAN> <SPAN style="COLOR: blue">time</SPAN> <SPAN style="COLOR: blue">off<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">set</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">statistics</SPAN> <SPAN style="COLOR: blue">profile</SPAN> <SPAN style="COLOR: blue">off<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">set</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"> <SPAN style="COLOR: blue">statistics</SPAN> <SPAN style="COLOR: blue">io</SPAN> <SPAN style="COLOR: blue">off<o:p></o:p></SPAN></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p>Output:</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p>&nbsp;</o:p><o:p><o:p>&nbsp;</o:p></P><o:p>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Table 'Worktable'. Scan count 1, logical reads 2974753, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical r<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Table 'tblDateDT'. Scan count 1, logical reads 2609, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical read<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">(1000000 row(s) affected)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Rows<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Executes<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>StmtText<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">-------------------- -------------------- ---------------------------------------------------------------------------------------<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1000000<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>insert @temp(DateID, strDate, dtDate)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">select dt.DateID, dbo.UDF_GetLongDateString(dt.dtDate), dt.dtDate from dbo.tblDateDT dt<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1000000<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>|--Table Insert(OBJECT:(@temp), SET:([DateID] = [DemoDB].[dbo].[tblDateDT].[DateID] a<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1000000<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>|--Table Spool<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1000000<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>|--Compute Scalar(DEFINE:([Expr1006]=[DemoDB].[dbo].[UDF_GetLongDateString]<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1000000<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>|--Top(ROWCOUNT est 0)<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1000000<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>|--Clustered Index Scan(OBJECT:([DemoDB].[dbo].[tblDateDT].[PK__t<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">(6 row(s) affected)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;</SPAN>SQL Server Execution Times:<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>CPU time = 13557 ms,<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>elapsed time = 14208 ms.<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">DateID<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>strDate<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">----------- ---------------------------------------------------------------------------------------------------------------------<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">793793<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Apr 30 4173 12:00:00:000AM<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">(1 row(s) affected)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Table '#6166761E'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">Rows<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Executes<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>StmtText<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>StmtId<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>NodeId<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Parent<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>PhysicalOp<SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">-------------------- -------------------- ------------------------------------- ----------- ----------- ----------- -------------<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>select top 1 * from @temp<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>2<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>0<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>NULL<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;</SPAN>|--Top(TOP EXPRESSION:((1)))<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>2<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>2<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Top<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes">1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>1<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>|--Table Scan(OBJECT:(@temp))<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>2<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>3<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>2<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>Table Scan<SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 8pt; FONT-FAMILY: 'Courier New'; mso-no-proof: yes"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">&nbsp;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p>&nbsp;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p>&nbsp;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">Using the&nbsp;scalar UDF in the insert @temp statement&nbsp;introduces an extra "Worktable"&nbsp;table.&nbsp;&nbsp;It is&nbsp;created with the strDate column left blank.&nbsp; The scalar function then updates the nvarchar column with an about 26 character wide value (for example, <FONT face="Courier New" size=2>Apr 30 4173 12:00:00:000AM</FONT>).&nbsp; There you have the classical page splits.&nbsp; What I also experimented is the make the nvarchar column narrower and I can see a lower page splits plateau.&nbsp; However, as I tried different page splits rates, they don't affect CPU&nbsp;usage much.&nbsp; So where is CPU&nbsp;usage coming from?&nbsp; It's the index searches.</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true">&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">Notice that there are close to 3,000,000 logical reads against the "Worktable".&nbsp; That's 3 times the row size.&nbsp; If this number sounds familiar, you're not mistaken.&nbsp; It's a bookmark lookup.&nbsp; When the scalar UDF needs to update the "Worktable", it performs a bookmark lookup to get to the row.&nbsp; The number of reads is not exactly 3 times since it contains&nbsp;reads when the BTree is only two level deep.&nbsp; Remember we&nbsp;have a constant page split&nbsp;causing "Worktable" to gradually grow into a big size.&nbsp; This also explains why we have a slow&nbsp;but constant&nbsp;consumption of tempDB free space.&nbsp;&nbsp;It's the scalar UDF, row by row,&nbsp;updating the varchar column and expanding the "Worktable".&nbsp; When the UDF is done with the last row, it jams all the complete resultset into the temp table variable and that causes the sudden dip in tempDB&nbsp;at the end of the batch.</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true">&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true">&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><STRONG>Summary:</STRONG></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true">&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">In a nutshell, when&nbsp;INSERT INTO&nbsp;@temp or&nbsp;#temp&nbsp;... followed by&nbsp;&nbsp;SELECT scalar_UDF()... syntax is used, an extra worktable is used to pass data to the&nbsp;UDF, which in turns, tries to find one row at a time to process (hence the index searches caused by&nbsp;repetitive bookmark lookups) and updates a column in the worktable (hence the page splits and tempDB usage growth).&nbsp; </P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true">&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">Just like in the case of temp table variable&nbsp;VS temp table, you should know the pros and cons of&nbsp;different technologies in order to implement the&nbsp;features that can help you to achieve your business need.&nbsp; This blog serves as an informational purpose and hope with the better understanding of the behaviors I just presented, you will use scalar UDF in the scenario that's best suited for you.</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true">&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">Happy coding,</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt" mce_keep="true">&nbsp;</P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">Mike Chang</o:p><o:p>&nbsp;</o:p></P>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt"><o:p>SQL SDE, </o:p><o:p>MSDN</o:p></P></FONT></o:p>
<P mce_keep="true">&nbsp;</P>]]></content:encoded>
    </item>
  </channel>
</rss>