Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 March SQL Server: Indexed View acquires a Lock on Data

SQL Server: Indexed View acquires a Lock on Data

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

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, usage of base table indexes, storage of additional indexes, maintenance of indexes.

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.

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.

Create a sample table:

1
2
3
4
5
6
7
8
CREATE TABLE dbo.tbl_Students
(
StudID INT
,StudName VARCHAR(20)
,Class CHAR(1)
,CONSTRAINT pk_tbl_Students_StudID PRIMARY KEY(StudID)
);
GO

Open a new session and execute below insert statement:

1
2
3
4
BEGIN TRANSACTION
INSERT INTO dbo.tbl_Students
VALUES (1,'Anvesh','A')
GO

Open another second session and execute below insert statement:

1
2
3
4
BEGIN TRANSACTION
INSERT INTO dbo.tbl_Students
VALUES (2,'Neevan','A')
GO

Above both statements completed successfully without blocking to each other.
Now execute ROLLBACK in both the sessions:

1
2
ROLLBACK
GO

Let me create an Indexed View:

1
2
3
4
5
6
7
8
CREATE VIEW dbo.vw_tbl_Students WITH SCHEMABINDING
AS
SELECT
COUNT_BIG(*) AS StudCount
,Class
FROM dbo.tbl_Students
GROUP BY Class
GO

Create an unique index on view:

1
2
CREATE UNIQUE CLUSTERED INDEX idx_tbl_students_class
ON dbo.vw_tbl_Students(Class);

Now again, execute below queries:
Open a new session and execute below insert statement:

1
2
3
4
BEGIN TRANSACTION
INSERT INTO dbo.tbl_Students
VALUES (1,'Anvesh','A')
GO

Open another second session and execute below insert statement:

1
2
3
4
BEGIN TRANSACTION
INSERT INTO dbo.tbl_Students
VALUES (2,'Neevan','A')
GO

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.

Mar 17, 2017Anvesh Patel
SQL Server Interview: The Indexed View - Frequently asked questionsSQL Server Interview: Difference between Filtered Index and Table Partition
Anvesh Patel
Anvesh Patel

Database Engineer

March 17, 2017 SQL ServerAnvesh Patel, database, database lock, database research and development, dbrnd, Indexed key, Indexed View, lock contention, Materialized View, SQL Query, SQL Server, SQL Server Administrator, SQL Server Error, SQL Server Monitoring, SQL Server Performance Tuning, SQL Server Programming, SQL Server Tips and Tricks, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....