<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tag 'Indexed View'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=Indexed+View&amp;orTags=0</link><description>Search results matching tag 'Indexed View'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Incorrect Results with Indexed Views</title><link>http://sqlblog.com/blogs/paul_white/archive/2013/02/05/incorrect-results-with-indexed-views.aspx</link><pubDate>Tue, 05 Feb 2013 22:18:11 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:47500</guid><dc:creator>Paul White</dc:creator><description>&lt;hr /&gt;&lt;font size="3" face="Calibri"&gt;&lt;em&gt;Summary: If you use MERGE, indexed views and foreign keys, your queries can return incorrect results.&lt;/em&gt;&lt;/font&gt;   &lt;br /&gt;  &lt;hr /&gt;  &lt;p&gt;&lt;font size="3" face="Calibri"&gt;Microsoft have &lt;a href="http://support.microsoft.com/kb/2756471" target="_blank"&gt;released a fix&lt;/a&gt; for incorrect results returned when querying an indexed view. &lt;/font&gt;&lt;font size="3" face="Calibri"&gt;The problem applies to:&lt;/font&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font size="3" face="Calibri"&gt;&lt;strong&gt;SQL Server 2012&lt;/strong&gt;&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font size="3" face="Calibri"&gt;&lt;strong&gt;SQL Server 2008 R2&lt;/strong&gt;&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font size="3" face="Calibri"&gt;&lt;strong&gt;SQL Server 2008&lt;/strong&gt;&lt;/font&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;font size="3" face="Calibri"&gt;The &lt;a href="http://support.microsoft.com/kb/2756471" target="_blank"&gt;Knowledge Base article&lt;/a&gt; does not go into much detail, or provide a reproduction script, but this blog entry does :)&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="3" face="Calibri"&gt;The KB does say that reproducing the bug requires these features:&lt;/font&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font size="3" face="Calibri"&gt;An indexed view on two tables that have a foreign key relationship&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font size="3" face="Calibri"&gt;An update performed against the base tables&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font size="3" face="Calibri"&gt;A query executed against the indexed view using a NOEXPAND hint&lt;/font&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;font size="3" face="Calibri"&gt;There are two important details I would like to add right away:&lt;/font&gt;&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;font size="3" face="Calibri"&gt;The &lt;a href="http://msdn.microsoft.com/en-us/library/ms181714.aspx" target="_blank"&gt;NOEXPAND&lt;/a&gt; hint is &lt;strong&gt;not required&lt;/strong&gt; to reproduce the bug on Enterprise Edition&lt;/font&gt; &lt;/li&gt;    &lt;li&gt;&lt;font size="3" face="Calibri"&gt;The update must be performed by a &lt;a href="http://technet.microsoft.com/en-us/library/bb510625.aspx" target="_blank"&gt;MERGE&lt;/a&gt; statement&lt;/font&gt; &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;font size="3" face="Calibri"&gt;The fix is available in the following cumulative update packages:&lt;/font&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;a href="http://support.microsoft.com/kb/2790947" target="_blank"&gt;Cumulative Update 2 for SQL Server 2012 SP1&lt;/a&gt; [build 11.0.3339]         &lt;br /&gt;&lt;/font&gt;&lt;font size="3" face="Calibri"&gt;&lt;a href="http://support.microsoft.com/kb/2777772" target="_blank"&gt;Cumulative Update 5 for SQL Server 2012 RTM&lt;/a&gt; [build 11.0.2395]         &lt;br /&gt;&lt;/font&gt;&lt;font size="3" face="Calibri"&gt;&lt;a href="http://support.microsoft.com/kb/2777358" target="_blank"&gt;Cumulative Update 4 for SQL Server 2008 R2 SP2&lt;/a&gt; [build 10.50.4270]         &lt;br /&gt;&lt;/font&gt;&lt;font size="3" face="Calibri"&gt;&lt;a href="http://support.microsoft.com/kb/2783135" target="_blank"&gt;Cumulative Update 10 for SQL Server 2008 R2 SP1&lt;/a&gt; [build 10.50.2868]         &lt;br /&gt;&lt;/font&gt;&lt;font size="3" face="Calibri"&gt;&lt;a href="http://support.microsoft.com/kb/2771833" target="_blank"&gt;Cumulative Update 8 for SQL Server 2008 SP3&lt;/a&gt; [build 10.00.5828]&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;font size="3" face="Calibri"&gt;No service pack contains this fix, you must apply one of the hotfix packages above.&lt;/font&gt;&lt;/p&gt;  &lt;h2&gt;Steps to Reproduce&lt;/h2&gt;  &lt;p&gt;&lt;font size="3" face="Calibri"&gt;The first thing we will need is two tables:&lt;/font&gt;&lt;/p&gt;  &lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:300px;width:97.5%;background-color:#f4f4f4;"&gt;   &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;     &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;CREATE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;TABLE&lt;/span&gt; dbo.Parent &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;(&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    parent_id &lt;span style="color:#0000ff;"&gt;integer&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;IDENTITY&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NULL&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;varchar&lt;/span&gt;(20) &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NULL&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;CONSTRAINT&lt;/span&gt; PK_Parent_id &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;        &lt;span style="color:#0000ff;"&gt;PRIMARY&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;KEY&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;CLUSTERED&lt;/span&gt; (parent_id)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;);&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;GO&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;CREATE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;TABLE&lt;/span&gt; dbo.Child&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;(&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    child_id &lt;span style="color:#0000ff;"&gt;integer&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;IDENTITY&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NULL&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    parent_id &lt;span style="color:#0000ff;"&gt;integer&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NULL&lt;/span&gt;,&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;CONSTRAINT&lt;/span&gt; PK_Child_id &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;        &lt;span style="color:#0000ff;"&gt;PRIMARY&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;KEY&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;CLUSTERED&lt;/span&gt; (child_id)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;And a few rows of data:&lt;/font&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;INSERT dbo.Child (parent_id)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt;.parent_id &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    (&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    INSERT Parent &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;OUTPUT&lt;/span&gt; inserted.parent_id &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;VALUES&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;        (&lt;span style="color:#006080;"&gt;'Apple'&lt;/span&gt;), &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;        (&lt;span style="color:#006080;"&gt;'Banana'&lt;/span&gt;), &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;        (&lt;span style="color:#006080;"&gt;'Cherry'&lt;/span&gt;)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    ) &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;New&lt;/span&gt;;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;The tables now look like this (parent first):&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_796FB1B9.png" target="_blank"&gt;&lt;img title="Original Data" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Original Data" src="http://sqlblog.com/blogs/paul_white/image_thumb_3141F5DD.png" width="168" height="203" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can now add the required &lt;strong&gt;FOREIGN KEY&lt;/strong&gt; relationship:&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;ALTER&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;TABLE&lt;/span&gt; dbo.Child&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;ADD&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;CONSTRAINT&lt;/span&gt; FK_Child_Parent&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;FOREIGN&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;KEY&lt;/span&gt; (parent_id)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;REFERENCES&lt;/span&gt; dbo.Parent (parent_id);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;Next, a very simple &lt;strong&gt;indexed view&lt;/strong&gt; that joins the two tables (the view could contain more complex features like aggregates):&lt;/font&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:300px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;CREATE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;VIEW&lt;/span&gt; dbo.ParentsAndChildren&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;WITH&lt;/span&gt; SCHEMABINDING &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    p.parent_id, &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    p.&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;, &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    c.child_id&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; dbo.Parent &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; p&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;JOIN&lt;/span&gt; dbo.Child &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; c &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    c.parent_id = p.parent_id;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;GO&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;CREATE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;UNIQUE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;CLUSTERED&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;INDEX&lt;/span&gt; cuq &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; dbo.ParentsAndChildren (child_id);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;The final step is to use a MERGE statement to make some changes to the Parent table:&lt;/font&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:400px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;DECLARE&lt;/span&gt; @ParentMerge &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;TABLE&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;(&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    parent_id &lt;span style="color:#0000ff;"&gt;integer&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;PRIMARY&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;KEY&lt;/span&gt;, &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;varchar&lt;/span&gt;(20) &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NULL&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;);&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;INSERT @ParentMerge&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    (parent_id, &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;VALUES&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    (1, &lt;span style="color:#006080;"&gt;'Kiwi Fruit'&lt;/span&gt;),&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    (4, &lt;span style="color:#006080;"&gt;'Dragon Fruit'&lt;/span&gt;);&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&amp;#160;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;MERGE dbo.Parent &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; p&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;USING&lt;/span&gt; @ParentMerge &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; s &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    s.parent_id = p.parent_id&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; MATCHED &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    &lt;span style="color:#0000ff;"&gt;UPDATE&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;SET&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; = s.&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;WHEN&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;NOT&lt;/span&gt; MATCHED &lt;span style="color:#0000ff;"&gt;THEN&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    INSERT (&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;) &lt;span style="color:#0000ff;"&gt;VALUES&lt;/span&gt; (s.&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt;)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;OUTPUT&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    $&lt;span style="color:#0000ff;"&gt;action&lt;/span&gt;, &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    inserted.parent_id, &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    deleted.&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; old_value, &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    inserted.&lt;span style="color:#0000ff;"&gt;value&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; new_value;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;This MERGE performs two actions:&lt;/font&gt;&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;font size="3" face="Calibri"&gt;Updates the value column of parent row 1 from Apple to Kiwi Fruit&lt;/font&gt; &lt;/li&gt;

  &lt;li&gt;&lt;font size="3" face="Calibri"&gt;Adds a new parent row 4 for Dragon Fruit&lt;/font&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;The statement includes an &lt;a href="http://msdn.microsoft.com/en-us/library/ms177564.aspx" target="_blank"&gt;OUTPUT&lt;/a&gt; clause to show the changes it makes (this is not required for the repro):&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_3634B15D.png" target="_blank"&gt;&lt;img title="MERGE changes" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="MERGE changes" src="http://sqlblog.com/blogs/paul_white/image_thumb_5C2A61B3.png" width="306" height="93" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;This confirms that the changes have been made as we requested: parent row 1 has changed; and row 4 has been added. The changes are reflected in the base tables:&lt;/font&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; * &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; dbo.Parent &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; p;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; * &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; dbo.Child &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; c;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_5432BF51.png" target="_blank"&gt;&lt;img title="Updated Base Table Data" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Updated Base Table Data" src="http://sqlblog.com/blogs/paul_white/image_thumb_6536ED34.png" width="187" height="224" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;As highlighted, row 1 has changed from &lt;em&gt;Apple&lt;/em&gt; to &lt;em&gt;Kiwi Fruit&lt;/em&gt; and row 4 has been added.&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;We do &lt;em&gt;not&lt;/em&gt; expect to see row 4 in the indexed view because there are no child records for that row, and the indexed view uses an &lt;em&gt;inner&lt;/em&gt; join. Checking the indexed view using the NOEXPAND table hint (required in non-Enterprise SKUs to use indexes on a view):&lt;/font&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; *&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; dbo.ParentsAndChildren &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;WITH&lt;/span&gt; (NOEXPAND);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_2ADB7753.png" target="_blank"&gt;&lt;img title="Incorrect indexed view data" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Incorrect indexed view data" src="http://sqlblog.com/blogs/paul_white/image_thumb_22E3D4F1.png" width="217" height="111" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;strong&gt;The results are incorrect.&lt;/strong&gt; They show the old value of the data for parent row 1.&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;Now we try using the &lt;a href="http://msdn.microsoft.com/en-us/library/ms181714.aspx" target="_blank"&gt;EXPAND VIEWS&lt;/a&gt; query hint to force SQL Server to access the base tables rather than reading view indexes:&lt;/font&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; *&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; dbo.ParentsAndChildren&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;OPTION&lt;/span&gt; (EXPAND VIEWS);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_48D98547.png" target="_blank"&gt;&lt;img title="Expand Views Hint" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Expand Views Hint" src="http://sqlblog.com/blogs/paul_white/image_thumb_40E1E2E5.png" width="223" height="113" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;strong&gt;This query produces correct results&lt;/strong&gt;.&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;On SQL Server Enterprise Edition, the optimizer chooses whether to access the indexed view or the base tables. For following query, without any hints, the optimizer chooses not to expand the view. It reads the view index and produces &lt;strong&gt;incorrect&lt;/strong&gt; results:&lt;/font&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#008000;"&gt;-- Enterprise Edition ONLY&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; * &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; dbo.ParentsAndChildren;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_14C4E5F4.png" target="_blank"&gt;&lt;img title="Indexed View Matching" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Indexed View Matching" src="http://sqlblog.com/blogs/paul_white/image_thumb_0CCD4392.png" width="217" height="111" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;Perhaps adding a child row to match the new parent row 4 will somehow fix things up?&lt;/font&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;INSERT dbo.Child (parent_id) &lt;span style="color:#0000ff;"&gt;VALUES&lt;/span&gt; (4);&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;GO&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; * &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; dbo.ParentsAndChildren &lt;span style="color:#0000ff;"&gt;WITH&lt;/span&gt; (NOEXPAND);&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; * &lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; dbo.ParentsAndChildren &lt;span style="color:#0000ff;"&gt;OPTION&lt;/span&gt; (EXPAND VIEWS);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_32C2F3E8.png" target="_blank"&gt;&lt;img title="After Insert" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="After Insert" src="http://sqlblog.com/blogs/paul_white/image_thumb_71B47483.png" width="239" height="248" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;No. The query plan that accesses the view index still returns an incorrect value for row 1. &lt;strong&gt;It seems MERGE has corrupted our indexed view&lt;/strong&gt;.&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;/font&gt;&lt;/p&gt;

&lt;h3&gt;Analysis using DBCC CHECKTABLE&lt;/h3&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;Checking the view with &lt;a href="http://msdn.microsoft.com/en-us/library/ms174338.aspx" target="_blank"&gt;DBCC CHECKTABLE&lt;/a&gt; returns no errors:&lt;/font&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;DBCC&lt;/span&gt; CHECKTABLE (ParentsAndChildren);&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_4935926F.png" target="_blank"&gt;&lt;img title="DBCC output 1" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="DBCC output 1" src="http://sqlblog.com/blogs/paul_white/image_thumb_6F2B42C5.png" width="644" height="83" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;Unless we use the &lt;strong&gt;EXTENDED_LOGICAL_CHECKS&lt;/strong&gt; option:&lt;/font&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;DBCC&lt;/span&gt; CHECKTABLE (ParentsAndChildren) &lt;span style="color:#0000ff;"&gt;WITH&lt;/span&gt; EXTENDED_LOGICAL_CHECKS;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_7837CE46.png" target="_blank"&gt;&lt;img title="DBCC output 2" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="DBCC output 2" src="http://sqlblog.com/blogs/paul_white/image_thumb_6B5D7828.png" width="644" height="217" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;The damage is repairable:&lt;/font&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;ALTER&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;DATABASE&lt;/span&gt; Sandpit &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;SET&lt;/span&gt; SINGLE_USER &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;WITH&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;ROLLBACK&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;IMMEDIATE&lt;/span&gt;;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;GO&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;DBCC&lt;/span&gt; CHECKTABLE (ParentsAndChildren, REPAIR_REBUILD) &lt;span style="color:#0000ff;"&gt;WITH&lt;/span&gt; EXTENDED_LOGICAL_CHECKS;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;GO&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;DBCC&lt;/span&gt; CHECKTABLE (ParentsAndChildren) &lt;span style="color:#0000ff;"&gt;WITH&lt;/span&gt; EXTENDED_LOGICAL_CHECKS;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;GO&lt;/span&gt;&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;ALTER&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;DATABASE&lt;/span&gt; Sandpit &lt;span style="color:#0000ff;"&gt;SET&lt;/span&gt; MULTI_USER;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_191B1B21.png" target="_blank"&gt;&lt;img title="DBCC repair" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="DBCC repair" src="http://sqlblog.com/blogs/paul_white/image_thumb_3DCC3298.png" width="644" height="277" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;You probably do not want to set your database to SINGLE_USER mode and run a DBCC repair after every MERGE statement, however. We could also rebuild the indexed view’s clustered index manually, of course.&lt;/font&gt;&lt;/p&gt;

&lt;h2&gt;Cause&lt;/h2&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;For the MERGE statement above, the query optimizer builds a plan that &lt;strong&gt;does not update the indexed view&lt;/strong&gt; (click to enlarge):&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_072E6187.png" target="_blank"&gt;&lt;img title="Incorrect Update Plan" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Incorrect Update Plan" src="http://sqlblog.com/blogs/paul_white/image_thumb_6A64C6A4.png" width="660" height="103" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;In a version of SQL Server with the fix applied, the same MERGE statement produces a plan that does maintain the indexed view:&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_73715225.png" target="_blank"&gt;&lt;img title="Correct Update Plan" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Correct Update Plan" src="http://sqlblog.com/blogs/paul_white/image_thumb_049509FC.png" width="644" height="101" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;The plan operators used to keep the view index in step with the base tables are highlighted. Without these operators, the changes to the base table are not correctly written to any indexes defined on the view. The &lt;strong&gt;root cause&lt;/strong&gt; is related to the same simplification that allows the optimizer to remove the reference to the Parent table in this query:&lt;/font&gt;&lt;/p&gt;

&lt;div id="codeSnippetWrapper" style="overflow:auto;cursor:text;font-size:8pt;border-top:silver 1px solid;font-family:'Courier New', courier, monospace;border-right:silver 1px solid;border-bottom:silver 1px solid;padding-bottom:4px;direction:ltr;text-align:left;padding-top:4px;padding-left:4px;margin:20px 0px 10px;border-left:silver 1px solid;line-height:12pt;padding-right:4px;max-height:200px;width:97.5%;background-color:#f4f4f4;"&gt;
  &lt;div id="codeSnippet" style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;
    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;SELECT&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;    COUNT_BIG(*)&lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;&lt;span style="color:#0000ff;"&gt;FROM&lt;/span&gt; dbo.Parent &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; p &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:#f4f4f4;"&gt;&lt;span style="color:#0000ff;"&gt;JOIN&lt;/span&gt; dbo.Child &lt;span style="color:#0000ff;"&gt;AS&lt;/span&gt; c &lt;span style="color:#0000ff;"&gt;ON&lt;/span&gt; &lt;/pre&gt;


    &lt;pre style="border-top-style:none;overflow:visible;font-size:8pt;border-left-style:none;font-family:'Courier New', courier, monospace;border-bottom-style:none;color:black;padding-bottom:0px;direction:ltr;text-align:left;padding-top:0px;border-right-style:none;padding-left:0px;margin:0em;line-height:12pt;padding-right:0px;width:100%;background-color:white;"&gt;    c.parent_id = p.parent_id;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_0DA1957D.png" target="_blank"&gt;&lt;img title="Simplification Example Plan" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Simplification Example Plan" src="http://sqlblog.com/blogs/paul_white/image_thumb_0C5CFC9E.png" width="314" height="87" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;The FOREIGN KEY relationship and NOT NULL constraints on the referencing column together mean that the join to Parent cannot affect the result of the query, so the join &lt;a href="http://msmvps.com/blogs/robfarley/archive/2008/11/09/join-simplification-in-sql-server.aspx" target="_blank"&gt;is simplified away&lt;/a&gt;. In SQL Server 2012, we can see when this simplification is performed because the following message appears when undocumented trace flags 8619 and 3604 are enabled during compilation:&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_2009E632.png" target="_blank"&gt;&lt;img title="Trace Flag 8619 output" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Trace Flag 8619 output" src="http://sqlblog.com/blogs/paul_white/image_thumb_2A5B0A92.png" width="457" height="37" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;The same message is emitted when a MERGE statement contains a WHEN MATCHED THEN UPDATE clause and either a WHEN NOT MATCHED THEN INSERT or WHEN MATCHED … THEN DELETE clause. These conditions combine such that the optimizer incorrectly concludes that a table reference can be removed, when in fact it is needed later on when the update side of the plan is built.&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;Other details of the query and database can affect whether the simplification can be misapplied. For example, if the FOREIGN KEY constraint contains an ON DELETE CASCADE clause, and the MERGE contains a DELETE clause, the simplification is not performed, the TF 8619 message does not appear, and the bug does not manifest.&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;The key to determining whether a particular query is vulnerable to this bug (TF 8619 aside) is to check whether the query plan includes operators to maintain the indexed view. At a minimum, you should see a update operator for the view:&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://sqlblog.com/blogs/paul_white/image_22636830.png" target="_blank"&gt;&lt;img title="View Update Operator" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="View Update Operator" src="http://sqlblog.com/blogs/paul_white/image_thumb_48591886.png" width="183" height="107" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;SQL Sentry &lt;a href="http://www.sqlsentry.net/plan-explorer/sql-server-query-view.asp" target="_blank"&gt;Plan Explorer&lt;/a&gt; identifies the operator as applying to a view explicitly, in SSMS you need to click on the graphical operator and inspect the Properties window.&lt;/font&gt;&lt;/p&gt;

&lt;h2&gt;Summary&lt;/h2&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;The updated conditions for incorrect results are:&lt;/font&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;font size="3" face="Calibri"&gt;An &lt;strong&gt;indexed view&lt;/strong&gt; that joins tables&lt;/font&gt; &lt;/li&gt;

  &lt;li&gt;&lt;font size="3" face="Calibri"&gt;Two of the tables have a single-column &lt;strong&gt;FOREIGN KEY constraint&lt;/strong&gt;&lt;/font&gt; &lt;/li&gt;

  &lt;li&gt;&lt;font size="3" face="Calibri"&gt;A &lt;strong&gt;MERGE&lt;/strong&gt; statement contains an &lt;strong&gt;UPDATE&lt;/strong&gt; action that affects one of the tables&lt;/font&gt; &lt;/li&gt;

  &lt;li&gt;&lt;font size="3" face="Calibri"&gt;The &lt;strong&gt;MERGE&lt;/strong&gt; statement also contains an &lt;strong&gt;INSERT or DELETE&lt;/strong&gt; action (or both)&lt;/font&gt; &lt;/li&gt;

  &lt;li&gt;&lt;font size="3" face="Calibri"&gt;The optimizer applies a &lt;strong&gt;simplification&lt;/strong&gt; that removes a table reference based on the FK relationship and other metadata&lt;/font&gt; &lt;/li&gt;

  &lt;li&gt;&lt;font size="3" face="Calibri"&gt;As a result, the MERGE execution plan does not contain the operators necessary to correctly maintain the indexed view&lt;/font&gt; &lt;/li&gt;

  &lt;li&gt;&lt;font size="3" face="Calibri"&gt;A subsequent query plan accesses an index on the view, either explicitly or via &lt;strong&gt;indexed-view matching&lt;/strong&gt; (Enterprise Edition)&lt;/font&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;em&gt;Note:&lt;/em&gt;&lt;/font&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;font size="3" face="Calibri"&gt;The simplification is not applied in &lt;em&gt;tempdb&lt;/em&gt;&lt;/font&gt; &lt;/li&gt;

  &lt;li&gt;&lt;font size="3" face="Calibri"&gt;The simplification is not applied to multi-column foreign key constraints&lt;/font&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;Under these conditions, &lt;strong&gt;the indexes on the view do not reflect the state of the base tables and incorrect results are returned&lt;/strong&gt;. Once the hot fix is applied, the optimizer does not misapply the simplification so the correct indexed view maintenance features are built into execution plans.&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;&lt;strong&gt;Update:&lt;/strong&gt; I am adding this based on Ian Yates’ great question in the comments: my expectation is that &lt;strong&gt;applying this hotfix will not remove any existing indexed view corruption&lt;/strong&gt;. You would need to test the hotfix as usual, apply it, and then either rebuild all affected indexed views manually, or run DBCC CHECKDB with the EXTENDED_LOGICAL_CHECKS option (which could take a long time).&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;© 2013 Paul White – All Rights Reserved&lt;/font&gt;&lt;/p&gt;

&lt;p align="left"&gt;&lt;a title="SQL Intersection" href="http://www.sqlintersection.com/" target="_blank"&gt;&lt;img title="Ill_Be_There4" style="border-left-width:0px;border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;display:inline;padding-right:0px;border-top-width:0px;" border="0" alt="Ill_Be_There4" src="http://sqlblog.com/blogs/paul_white/Ill_Be_There4_0D1F26E1.jpg" width="140" height="110" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;font size="3" face="Calibri"&gt;Twitter: &lt;a href="http://twitter.com/SQL_Kiwi"&gt;@SQL_Kiwi&lt;/a&gt; 

    &lt;br /&gt;Email: &lt;a href="mailto:SQLKiwi@gmail.com"&gt;SQLKiwi@gmail.com&lt;/a&gt;&lt;/font&gt;&lt;/p&gt;</description></item><item><title>Be ready to drop your indexed view.</title><link>http://sqlblog.com/blogs/alexander_kuznetsov/archive/2009/06/02/be-ready-to-drop-your-indexed-view.aspx</link><pubDate>Tue, 02 Jun 2009 21:10:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:14419</guid><dc:creator>Alexander Kuznetsov</dc:creator><description>
&lt;p&gt;In all-too-many cases an indexed view may solve your short term performance goals but at some later time become counterproductive. So if you choose to use an indexed view, you may need an exit strategy. Let me describe a few common problems with indexed views.&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;br&gt;&lt;br&gt;&lt;u&gt;&lt;b&gt;Indexed views may increase lock contention.&lt;/b&gt;&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;It is very easy to demonstrate. Create the following table:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;code style="font-size:12px;"&gt;&lt;span style="color:blue;"&gt;CREATE&amp;nbsp;TABLE&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;dbo.ChildTable&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;ChildID&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;INT&amp;nbsp;&lt;/span&gt;&lt;span style="color:gray;"&gt;NOT&amp;nbsp;NULL&amp;nbsp;
&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;CONSTRAINT&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;PK_ChildTable&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;PRIMARY&amp;nbsp;KEY&lt;/span&gt;&lt;span style="color:gray;"&gt;,
&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;ParentID&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;INT&amp;nbsp;&lt;/span&gt;&lt;span style="color:gray;"&gt;NOT&amp;nbsp;NULL,
&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;Amount&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;INT&amp;nbsp;&lt;/span&gt;&lt;span style="color:gray;"&gt;NOT&amp;nbsp;NULL);
&lt;br&gt;&lt;/span&gt;&lt;span style="color:black;"&gt;GO&lt;/span&gt;&lt;/code&gt;                &amp;nbsp;&amp;nbsp;
              &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;From one tab in SSMS, run this script:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;code style="font-size:12px;"&gt;&lt;span style="color:blue;"&gt;BEGIN&amp;nbsp;TRAN&lt;/span&gt;&lt;span style="color:gray;"&gt;;
&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;INSERT&amp;nbsp;INTO&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;dbo.ChildTable&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;ChildID&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;ParentID&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;Amount&lt;/span&gt;&lt;span style="color:gray;"&gt;)
&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;VALUES&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;1&lt;/span&gt;&lt;span style="color:gray;"&gt;,&lt;/span&gt;&lt;span style="color:black;"&gt;1&lt;/span&gt;&lt;span style="color:gray;"&gt;,&lt;/span&gt;&lt;span style="color:black;"&gt;1&lt;/span&gt;&lt;span style="color:gray;"&gt;);&amp;nbsp;
&lt;/span&gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;From another tab, run a similar one:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;code style="font-size:12px;"&gt;&lt;span style="color:blue;"&gt;BEGIN&amp;nbsp;TRAN&lt;/span&gt;&lt;span style="color:gray;"&gt;;
&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;INSERT&amp;nbsp;INTO&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;dbo.ChildTable&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;ChildID&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;ParentID&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;Amount&lt;/span&gt;&lt;span style="color:gray;"&gt;)
&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;VALUES&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;2&lt;/span&gt;&lt;span style="color:gray;"&gt;,&lt;/span&gt;&lt;span style="color:black;"&gt;1&lt;/span&gt;&lt;span style="color:gray;"&gt;,&lt;/span&gt;&lt;span style="color:black;"&gt;1&lt;/span&gt;&lt;span style="color:gray;"&gt;);
&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;ROLLBACK&lt;/span&gt;&lt;span style="color:gray;"&gt;;
&lt;br&gt;&lt;/span&gt;&lt;/code&gt;                &amp;nbsp;&amp;nbsp;
              &lt;br&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Note that both inserts complete, they do not block each other. Rollback in both tabs, and create an indexed view:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;code style="font-size:12px;"&gt;&lt;span style="color:blue;"&gt;CREATE&amp;nbsp;VIEW&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;dbo.ChildTableTotals&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;WITH&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;SCHEMABINDING
&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;AS
&lt;br&gt;SELECT&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;ParentID&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;
&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;COUNT_BIG&lt;/span&gt;&lt;span style="color:gray;"&gt;(*)&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;AS&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;ChildRowsPerParent&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;
&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:magenta;"&gt;SUM&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;Amount&lt;/span&gt;&lt;span style="color:gray;"&gt;)&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;AS&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;SumAmount
&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;FROM&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;dbo.ChildTable
&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;GROUP&amp;nbsp;BY&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;ParentID&lt;/span&gt;&lt;span style="color:gray;"&gt;;
&lt;br&gt;&lt;/span&gt;&lt;span style="color:black;"&gt;GO
&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;CREATE&amp;nbsp;UNIQUE&amp;nbsp;CLUSTERED&amp;nbsp;INDEX&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;ChildTableTotals_CI&amp;nbsp;
&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;ON&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;dbo.ChildTableTotals&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;ParentID&lt;/span&gt;&lt;span style="color:gray;"&gt;);
&lt;/span&gt;&lt;/code&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;br&gt;&lt;br&gt;Rerun the two inserts. Note that the second one does not complete; it is blocked. The reason is very simple: the first insert modifies the corresponding entry in the indexed view, so the insert acquires and holds a lock on it. &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;It is just as easy to demonstrate that when you create an indexed view, deadlocks may become more likely too.&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&lt;br&gt;&lt;i&gt;&lt;b&gt;Note&lt;/b&gt;&lt;/i&gt;: this is not a problem with the way indexed views are implemented. If you roll out your own summary table, and develop triggers which directly modify it to keep it up-to-date, you will encounter the same problem. Only if you don't maintain your summary table all the time, you can get around this locking problem, but a more detailed discussion of this is beyond the scope of this post.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;a href="http://sqlblogcasts.com/blogs/tonyrogerson/archive/2008/01/03/views-they-offer-no-optimisation-benefits-they-are-simply-inline-macros-use-sparingly.aspx#comments"&gt;Also make sure you have read an excellent post by Tony Rogerson:&lt;/a&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;u&gt;&lt;b&gt;Indexed views on joins may become counterproductive.&lt;/b&gt;&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Create the following table and another indexed view:&lt;/p&gt;
&lt;p&gt;&lt;code style="font-size:12px;"&gt;&lt;span style="color:blue;"&gt;CREATE&amp;nbsp;TABLE&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;dbo.ParentTable&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;ParentID&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;INT&amp;nbsp;&lt;/span&gt;&lt;span style="color:gray;"&gt;NOT&amp;nbsp;NULL&amp;nbsp;
&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;CONSTRAINT&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;PK_ParentTable&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;PRIMARY&amp;nbsp;KEY&lt;/span&gt;&lt;span style="color:gray;"&gt;,
&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;WideData&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;CHAR&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;1000&lt;/span&gt;&lt;span style="color:gray;"&gt;)&amp;nbsp;NOT&amp;nbsp;NULL);
&lt;br&gt;&lt;/span&gt;&lt;span style="color:black;"&gt;GO
&lt;br&gt;
&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;CREATE&amp;nbsp;VIEW&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;dbo.ParentTableWithAmounts&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;WITH&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;SCHEMABINDING
&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;AS
&lt;br&gt;SELECT&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;p.ParentID&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;p.WideData&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;c.ChildID&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;c.Amount
&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;FROM&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;dbo.ParentTable&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;AS&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;p&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;JOIN&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;dbo.ChildTable&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;AS&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;c
&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;ON&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;p.ParentID&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;c.ParentID&lt;/span&gt;&lt;span style="color:gray;"&gt;;
&lt;br&gt;&lt;/span&gt;&lt;span style="color:black;"&gt;GO
&lt;br&gt;
&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;CREATE&amp;nbsp;UNIQUE&amp;nbsp;CLUSTERED&amp;nbsp;INDEX&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;ParentTableWithAmounts_CI&amp;nbsp;
&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;ON&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;dbo.ParentTableWithAmounts&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;ChildID&lt;/span&gt;&lt;span style="color:gray;"&gt;);
&lt;br&gt;&lt;/span&gt;&lt;/code&gt;&amp;nbsp;                &lt;/p&gt;
&lt;p&gt;Suppose that originally you have an average one child row per parent on, and that selecting from this indexed view is faster than joining two tables - that's why you created it in the first place. However, if on average you have 10K child rows per parent one, your indexed view becomes counterproductive. Let's add 10K child rows:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;code style="font-size:12px;"&gt;&lt;span style="color:blue;"&gt;INSERT&amp;nbsp;INTO&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;dbo.ParentTable&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;ParentID&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;WideData&lt;/span&gt;&lt;span style="color:gray;"&gt;)
&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;VALUES&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;1&lt;/span&gt;&lt;span style="color:gray;"&gt;,&lt;/span&gt;&lt;span style="color:red;"&gt;'asdf'&lt;/span&gt;&lt;span style="color:gray;"&gt;);
&lt;br&gt;
&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;SET&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;NOCOUNT&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;ON&lt;/span&gt;&lt;span style="color:gray;"&gt;;
&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;DECLARE&amp;nbsp;&lt;/span&gt;&lt;span&gt;@i&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;INT&lt;/span&gt;&lt;span style="color:gray;"&gt;;
&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;SET&amp;nbsp;&lt;/span&gt;&lt;span&gt;@i&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;&lt;span style="color:black;"&gt;10000&lt;/span&gt;&lt;span style="color:gray;"&gt;;
&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;WHILE&amp;nbsp;&lt;/span&gt;&lt;span&gt;@i&lt;/span&gt;&lt;span style="color:gray;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:black;"&gt;20000&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;BEGIN
&lt;br&gt;&amp;nbsp;&amp;nbsp;INSERT&amp;nbsp;INTO&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;dbo.ChildTable&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;ChildID&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;ParentID&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;Amount&lt;/span&gt;&lt;span style="color:gray;"&gt;)
&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;VALUES&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span&gt;@i&lt;/span&gt;&lt;span style="color:gray;"&gt;,&lt;/span&gt;&lt;span style="color:black;"&gt;1&lt;/span&gt;&lt;span style="color:gray;"&gt;,&lt;/span&gt;&lt;span style="color:black;"&gt;1&lt;/span&gt;&lt;span style="color:gray;"&gt;);
&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;SET&amp;nbsp;&lt;/span&gt;&lt;span&gt;@i&lt;/span&gt;&lt;span style="color:blue;"&gt;=&lt;/span&gt;&lt;span&gt;@i&lt;/span&gt;&lt;span style="color:gray;"&gt;+&lt;/span&gt;&lt;span style="color:black;"&gt;1&lt;/span&gt;&lt;span style="color:gray;"&gt;;
&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;END
&lt;br&gt;&lt;/span&gt;&lt;/code&gt;                &amp;nbsp;&amp;nbsp;
              &lt;/p&gt;
&lt;p&gt;Let us select from this indexed view:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;code style="font-size:12px;"&gt;&lt;span style="color:blue;"&gt;SELECT&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;ParentID&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;WideData&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;ChildID&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;Amount
&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;FROM&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;dbo.ParentTableWithAmounts&lt;/span&gt;&lt;span style="color:gray;"&gt;;&amp;nbsp;
&lt;/span&gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The optimizer has chosen not to use the indexed view, and the execution costs are as follows:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Table 'ChildTable'. Scan count 1, logical reads 28, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.&lt;br&gt;Table 'ParentTable'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If you force the optimizer to use the indexed view,&amp;nbsp; the execution costs are dramatically higher:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;code style="font-size:12px;"&gt;&lt;span style="color:blue;"&gt;SELECT&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;ParentID&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;WideData&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;ChildID&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;Amount
&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;FROM&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;dbo.ParentTableWithAmounts&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;WITH&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;NOEXPAND&lt;/span&gt;&lt;span style="color:gray;"&gt;);&lt;/span&gt;&lt;/code&gt;                &amp;nbsp;&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;&amp;nbsp;Table 'ParentTableWithAmounts'. Scan count 1, logical reads 1435, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.&lt;br&gt;&lt;/p&gt;
&lt;p&gt;As you have seen, when the parent table is wide and there are many child rows per parent, the indexed view becomes counterproductive, and the optimizer is able to recognize the fact.&lt;/p&gt;
&lt;p&gt;Also note that this indexed view increases lock contention just as the previous one did. You can try to update a parent row and one of its child rows and see for yourself. &lt;br&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;u&gt;&lt;b&gt;Exit strategy for NOEXPAND hint&lt;/b&gt;&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If your indexed view becomes counterproductive, you may consider dropping it altogether. However, dropping the index view will break all those queries with NOEXPAND hint:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;code style="font-size:12px;"&gt;&lt;span style="color:blue;"&gt;DROP&amp;nbsp;INDEX&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;dbo.ParentTableWithAmounts.ParentTableWithAmounts_CI&lt;/span&gt;&lt;span style="color:gray;"&gt;;
&lt;br&gt;&lt;/span&gt;&lt;span style="color:black;"&gt;GO
&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;SELECT&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;ParentID&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;WideData&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;ChildID&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;Amount
&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;FROM&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;dbo.ParentTableWithAmounts&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;WITH&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;NOEXPAND&lt;/span&gt;&lt;span style="color:gray;"&gt;);
&lt;br&gt;&lt;/span&gt;&lt;/code&gt;&amp;nbsp;                &lt;/p&gt;
&lt;p&gt;Msg 8171, Level 16, State 2, Line 1&lt;br&gt;Hint 'noexpand' on object 'dbo.ParentTableWithAmounts' is invalid.&lt;br&gt;&lt;/p&gt;
&lt;p&gt;I would recommend to be prepared for such possibility, to wrap all your selects using NOEXPAND in stored procedures, for example:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;code style="font-size:12px;"&gt;&lt;span style="color:blue;"&gt;CREATE&amp;nbsp;PROCEDURE&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;dbo.SelectParentTableWithAmounts
&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;AS
&lt;br&gt;SELECT&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;ParentID&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;WideData&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;ChildID&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;Amount
&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;FROM&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;dbo.ParentTableWithAmounts&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;WITH&lt;/span&gt;&lt;span style="color:gray;"&gt;(&lt;/span&gt;&lt;span style="color:black;"&gt;NOEXPAND&lt;/span&gt;&lt;span style="color:gray;"&gt;);
&lt;br&gt;
&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;&lt;/span&gt;&lt;span style="color:gray;"&gt; &lt;/span&gt;&lt;/code&gt;&lt;br&gt;&lt;/p&gt;
&lt;p&gt; and to provide a rollback script which alters these procedures, as follows:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;code style="font-size:12px;"&gt;&lt;span style="color:blue;"&gt;DROP&amp;nbsp;INDEX&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;dbo.ParentTableWithAmounts.ParentTableWithAmounts_CI&lt;/span&gt;&lt;span style="color:gray;"&gt;;
&lt;br&gt;&lt;/span&gt;&lt;span style="color:black;"&gt;GO
&lt;/span&gt;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;code style="font-size:12px;"&gt;&lt;span style="color:blue;"&gt;ALTER&amp;nbsp;PROCEDURE&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;dbo.SelectParentTableWithAmounts
&lt;br&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;AS
&lt;br&gt;SELECT&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;ParentID&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;WideData&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;ChildID&lt;/span&gt;&lt;span style="color:gray;"&gt;,&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;Amount
&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="color:blue;"&gt;FROM&amp;nbsp;&lt;/span&gt;&lt;span style="color:black;"&gt;dbo.ParentTableWithAmounts&lt;/span&gt;&lt;span style="color:gray;"&gt;;&amp;nbsp;&lt;/span&gt;&lt;/code&gt; &lt;br&gt;&lt;/p&gt;
&lt;p&gt;You can add error handling to this script, so that either both changes deploy or none does. Make sure to test this script. If you have unit tests, include this scenario in your test harness. Of course there are other approaches, but my main point is that you need to be aware that your indexed view can be dropped, and have a working exit strategy.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;u&gt;&lt;b&gt;Performance considerations for your exit strategy&lt;/b&gt;&lt;/u&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;We have just discussed how to make sure that your application does not break, but what about the performance? Clearly the performance of your selects may plunge, what can be done about it? In many cases, index covering gives you acceptable performance without too much lock contention. Also sometimes you want to roll out your own summary tables, but that sounds like a topic for another post.&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item></channel></rss>