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

<channel>
	<title>Indexed View &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/indexed-view/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Tue, 07 Mar 2017 19:02:46 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.6</generator>
	<item>
		<title>SQL Server: Indexed View acquires a Lock on Data</title>
		<link>https://www.dbrnd.com/2017/03/sql-server-indexed-view-acquires-a-lock-on-data-blocking-state/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Fri, 17 Mar 2017 16:16:49 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database lock]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Indexed key]]></category>
		<category><![CDATA[Indexed View]]></category>
		<category><![CDATA[lock contention]]></category>
		<category><![CDATA[Materialized View]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Error]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tuning]]></category>
		<category><![CDATA[SQL Server Programming]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=4770</guid>

					<description><![CDATA[A database view with the index is called as an Indexed view or Materialized view which has dedicated storage space for their indexes. People are suggesting to use an Indexed view for better performance, but this is not 100% correct. There are lots factors need to consider before using an Indexed view like database lock, [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-indexed-view-acquires-a-lock-on-data-blocking-state%2F&amp;linkname=SQL%20Server%3A%20Indexed%20View%20acquires%20a%20Lock%20on%20Data" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-indexed-view-acquires-a-lock-on-data-blocking-state%2F&amp;linkname=SQL%20Server%3A%20Indexed%20View%20acquires%20a%20Lock%20on%20Data" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-indexed-view-acquires-a-lock-on-data-blocking-state%2F&amp;linkname=SQL%20Server%3A%20Indexed%20View%20acquires%20a%20Lock%20on%20Data" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-indexed-view-acquires-a-lock-on-data-blocking-state%2F&amp;linkname=SQL%20Server%3A%20Indexed%20View%20acquires%20a%20Lock%20on%20Data" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-indexed-view-acquires-a-lock-on-data-blocking-state%2F&amp;linkname=SQL%20Server%3A%20Indexed%20View%20acquires%20a%20Lock%20on%20Data" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-indexed-view-acquires-a-lock-on-data-blocking-state%2F&amp;linkname=SQL%20Server%3A%20Indexed%20View%20acquires%20a%20Lock%20on%20Data" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-indexed-view-acquires-a-lock-on-data-blocking-state%2F&#038;title=SQL%20Server%3A%20Indexed%20View%20acquires%20a%20Lock%20on%20Data" data-a2a-url="https://www.dbrnd.com/2017/03/sql-server-indexed-view-acquires-a-lock-on-data-blocking-state/" data-a2a-title="SQL Server: Indexed View acquires a Lock on Data"></a></p><p>A database view with the index is called as an Indexed view or Materialized view which has dedicated storage space for their indexes. </p>
<p>People are suggesting to use an Indexed view for better performance, but this is not 100% correct. There are lots factors need to consider before using an Indexed view like database lock, usage of base table indexes, storage of additional indexes, maintenance of indexes.</p>
<blockquote><p>I would suggest, please avoid the use of Indexed view because it also increases the lock contention which causes to deadlock. There are very rare situations, where you can think of an Indexed view.</p></blockquote>
<p>In this post, I will demonstrate on how Indexed view acquires a lock on data. When you create an Indexed view, your base table data need to modify indexes of view as well which requires an additional lock. </p>
<p><strong>Create a sample table:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE dbo.tbl_Students
(
	StudID INT
	,StudName VARCHAR(20)
	,Class CHAR(1)
	,CONSTRAINT pk_tbl_Students_StudID PRIMARY KEY(StudID)
); 
GO</pre> </p>
<p><strong>Open a new session and execute below insert statement:</strong></p>
<pre class="crayon-plain-tag">BEGIN TRANSACTION
INSERT INTO dbo.tbl_Students
VALUES (1,'Anvesh','A')
GO</pre> </p>
<p><strong>Open another second session and execute below insert statement:</strong></p>
<pre class="crayon-plain-tag">BEGIN TRANSACTION
INSERT INTO dbo.tbl_Students
VALUES (2,'Neevan','A')
GO</pre> </p>
<p>Above both statements completed successfully without blocking to each other.<br />
<strong>Now execute ROLLBACK in both the sessions:</strong></p>
<pre class="crayon-plain-tag">ROLLBACK
GO</pre> </p>
<p><strong>Let me create an Indexed View:</strong></p>
<pre class="crayon-plain-tag">CREATE VIEW dbo.vw_tbl_Students WITH SCHEMABINDING 
AS 
SELECT	
	COUNT_BIG(*) AS StudCount
	,Class  		
FROM dbo.tbl_Students
GROUP BY Class 
GO</pre> </p>
<p><strong>Create an unique index on view:</strong></p>
<pre class="crayon-plain-tag">CREATE UNIQUE CLUSTERED INDEX idx_tbl_students_class
ON dbo.vw_tbl_Students(Class);</pre> </p>
<p><strong>Now again, execute below queries:</strong><br />
<strong>Open a new session and execute below insert statement:</strong></p>
<pre class="crayon-plain-tag">BEGIN TRANSACTION
INSERT INTO dbo.tbl_Students
VALUES (1,'Anvesh','A')
GO</pre> </p>
<p><strong>Open another second session and execute below insert statement:</strong></p>
<pre class="crayon-plain-tag">BEGIN TRANSACTION
INSERT INTO dbo.tbl_Students
VALUES (2,'Neevan','A')
GO</pre> </p>
<p>At this time, You can find the second query in blocking state, and it is waiting for your first query to complete because the first query also held the key to Indexed view. </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL Server Interview: The Indexed View &#8211; Frequently asked questions</title>
		<link>https://www.dbrnd.com/2017/03/sql-server-interview-the-indexed-view-frequently-asked-questions-answers-materialized-view/</link>
					<comments>https://www.dbrnd.com/2017/03/sql-server-interview-the-indexed-view-frequently-asked-questions-answers-materialized-view/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Thu, 16 Mar 2017 16:16:22 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Interview]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Indexed View]]></category>
		<category><![CDATA[Materialized View]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Error]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tuning]]></category>
		<category><![CDATA[SQL Server Programming]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=4761</guid>

					<description><![CDATA[What is Indexed View or Materialized View? A clustered index is the only type of SQL Server index that contains data; the clustered index on a view contains all the data that makes up the view definition. As soon as you create this clustered index, the view is materialized, meaning SQL Server allocates storage space [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-interview-the-indexed-view-frequently-asked-questions-answers-materialized-view%2F&amp;linkname=SQL%20Server%20Interview%3A%20The%20Indexed%20View%20%E2%80%93%20Frequently%20asked%20questions" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-interview-the-indexed-view-frequently-asked-questions-answers-materialized-view%2F&amp;linkname=SQL%20Server%20Interview%3A%20The%20Indexed%20View%20%E2%80%93%20Frequently%20asked%20questions" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-interview-the-indexed-view-frequently-asked-questions-answers-materialized-view%2F&amp;linkname=SQL%20Server%20Interview%3A%20The%20Indexed%20View%20%E2%80%93%20Frequently%20asked%20questions" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-interview-the-indexed-view-frequently-asked-questions-answers-materialized-view%2F&amp;linkname=SQL%20Server%20Interview%3A%20The%20Indexed%20View%20%E2%80%93%20Frequently%20asked%20questions" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-interview-the-indexed-view-frequently-asked-questions-answers-materialized-view%2F&amp;linkname=SQL%20Server%20Interview%3A%20The%20Indexed%20View%20%E2%80%93%20Frequently%20asked%20questions" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-interview-the-indexed-view-frequently-asked-questions-answers-materialized-view%2F&amp;linkname=SQL%20Server%20Interview%3A%20The%20Indexed%20View%20%E2%80%93%20Frequently%20asked%20questions" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F03%2Fsql-server-interview-the-indexed-view-frequently-asked-questions-answers-materialized-view%2F&#038;title=SQL%20Server%20Interview%3A%20The%20Indexed%20View%20%E2%80%93%20Frequently%20asked%20questions" data-a2a-url="https://www.dbrnd.com/2017/03/sql-server-interview-the-indexed-view-frequently-asked-questions-answers-materialized-view/" data-a2a-title="SQL Server Interview: The Indexed View – Frequently asked questions"></a></p><p><strong>What is Indexed View or Materialized View?</strong></p>
<blockquote><p>A clustered index is the only type of SQL Server index that contains data; the clustered index on a view contains all the data that makes up the view definition. As soon as you create this clustered index, the view is materialized, meaning SQL Server allocates storage space for it which is called as Indexed view.</p></blockquote>
<p><strong>what happens when you create an indexed view?</strong></p>
<blockquote><p>The key threshold happens when you create the primary key for the view which is the unique clustered index for the view and required before you can add secondary indexes to the view.</p>
<p>What that does it “materializes” the view such that it is no longer just a query into your tables, but it becomes a table itself with real data.  And of course, that data must be maintained to match the underlying tables. </p></blockquote>
<p><strong>What should be your steps for the successful implementation of indexed view?</strong></p>
<blockquote>
<ul>
<li>Verify the SET options which are required for view and table</li>
<li>Verify that the view definition</li>
<li>Option IGNORE_DUP_KEY must be set to OFF</li>
<li>Create the view by using the WITH SCHEMABINDING option</li>
<li>Create the unique clustered index on the view</li>
<li>If GROUP BY is present, the VIEW definition must contain COUNT_BIG(*) and must not contain HAVING</li>
</ul>
</blockquote>
<p><strong>Why does the first index on a view have to be CLUSTERED and UNIQUE?</strong></p>
<blockquote><p>It must be UNIQUE to allow easy lookup of records in the view by key value during indexed view maintenance, and to prevent creation of views with duplicates, which would require special logic to maintain. It must be clustered because only a clustered index can enforce uniqueness and store the rows at the same time.</p></blockquote>
<p><strong>Can we create a Non-clustered index on a View?</strong></p>
<blockquote><p>Yes, If your queries could benefit from having more than one index on the view, non-clustered indexes can also be created on the view.</p></blockquote>
<p><strong>When Indexed View updated?</strong></p>
<blockquote><p>The indexed view is updated after immediately update of base table.</p></blockquote>
<p><strong>Which commands/items can&#8217;t contain by Indexed View?</strong></p>
<blockquote>
<ul>
<li>TOP</li>
<li>UNION</li>
<li>text, ntext, or image columns</li>
<li>DISTINCT</li>
<li>MIN, MAX, COUNT(*), COUNT(), STDEV, VARIANCE, or AVG</li>
<li>OUTER JOINs</li>
<li>Self JOINs</li>
<li>Subqueries</li>
<li>ROWSET function</li>
<li>CONTAIN or FREETEXT</li>
<li>COMPUTE or COMPUTE BY</li>
</ul>
</blockquote>
<p><strong>Does indexed view affect insert performance?</strong></p>
<blockquote><p>Yes, it can hurt insert performance quite dramatically. Any index must be updated whenever there is a relevant change. A relevant change is any insert or delete and any update which affects a column which is in the index or is an included column for the index.</p></blockquote>
<p><strong>How frequently you require maintenance for indexed view?</strong></p>
<blockquote><p>It dependes, If underlying data is frequently updated, Indexed view requires maintainances.</p></blockquote>
<p><strong>Does Indexed view increase a lock contention?</strong></p>
<blockquote><p>Yes, It increases the lock contention.</p></blockquote>
<p><strong>Can we create a compressed Indexed view?</strong></p>
<blockquote><p>Yes, SQL Server 2016 supports on-disk storage compression for indexed views. By using indexed views compression, you can reduce the total disk storage space.</p></blockquote>
<p><strong>What do you know about a partition-aligned indexed view?</strong></p>
<blockquote><p>SQL Server 2016 supports defining indexed views on partitioned data. These views are called partition-aligned indexed views. By using these views, you can increase the speed and efficiency of your queries.</p></blockquote>
<p><strong>Do you know what is the use of NOEXPAND hint with the indexed view?</strong></p>
<blockquote><p>The NOEXPAND hint tells the optimizer to just use the index defined and to not look at the underlying tables that define the view that has been indexed. This may be necessary for some circumstances where the indexed view would work faster than letting the optimizer access the underlying tables.</p></blockquote>
<p><strong>Can we modify the underlying tables or columns if your view created with the WITH SCHEMABINDING option?</strong></p>
<blockquote><p>No, we can&#8217;t.</p></blockquote>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2017/03/sql-server-interview-the-indexed-view-frequently-asked-questions-answers-materialized-view/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
