<?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>lock contention &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/lock-contention/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>
	</channel>
</rss>
