+ All Categories
Home > Presentations & Public Speaking > SQL Server Blocking Analysis

SQL Server Blocking Analysis

Date post: 12-Apr-2017
Category:
Upload: hau-vo-tan
View: 250 times
Download: 0 times
Share this document with a friend
55
Blocking Analysis Vo Tan Hau July 08 ,2011 MS SQL Server
Transcript
Page 1: SQL Server Blocking Analysis

Blocking Analysis

Vo Tan HauJuly 08 ,2011

MS SQL Server

Page 2: SQL Server Blocking Analysis

CONTENTS

Overview SQL Server. Locks And Transaction isolation levels SQL Server Blocking. Q&A.

Page 3: SQL Server Blocking Analysis

1.Overview SQL Server

Relational DatabaseManagement System

SQL ServerClient

Results

Client Application

OLAP

OLTPQuery

Page 4: SQL Server Blocking Analysis

Client-Server Communication Process

Client Application

Client Net-LibraryClient

SQL Server

Relational Engine

Storage Engine

Server

LocalDatabase

Database API (OLE DB, ODBC,

DB-Library)

Memory

Open Data Services

Server Net-Libraries

Query

Result Set

Result Set

Query1

2

3

4

5

Tabular Data Stream(TDS)

Processor

Page 5: SQL Server Blocking Analysis

Lock in MS SQL Server.

2. Locks & Transaction Isolation Levels

1

2 Transaction Isolation Levels.

Page 6: SQL Server Blocking Analysis

2.1.Lock in MS SQL Server

Lock Granularity and Hierarchies.

a

b

d

Lock Modes.

What is Lock in SQL Server?

c

Lock Compatibility.

Page 7: SQL Server Blocking Analysis

2.1.a What is Lock in SQL Server? Locking is a mechanism used by the Microsoft SQL

Server Database Engine to synchronize access by multiple users to the same piece of data at the same time.

The basis of locking is to allow one transaction to update data, knowing that if it has to roll back any changes, no other transaction has modified the data since the first transaction did.

Page 8: SQL Server Blocking Analysis

2.1.b Lock Granularity and HierarchiesResource Description RID A row identifier used to lock a single row within a heap.

KEYA row lock within an index used to protect key ranges in serializable

transactions.PAGE An 8-kilobyte (KB) page in a database, such as data or index pages.EXTENT A contiguous group of eight pages, such as data or index pages.

HOBTA heap or B-tree. A lock protecting an index or the heap of data pages in

a table that does not have a clustered index.TABLE The entire table, including all data and indexes.FILE A database file.APPLICATION An application-specified resource.METADATA Metadata locks.ALLOCATION_

UNIT An allocation unit.DATABASE The entire database.

Page 9: SQL Server Blocking Analysis

2.1.c Locks Modes Locks have different modes that specify the level of access

other transactions have to the locked resource.

Update (U)

Shared (S)

Intent

Key-range

Exclusive (X)

Bulk Update (BU)

Locks Modes

Schema

Page 10: SQL Server Blocking Analysis

2.1.c Locks Modes (cont)ID CA CB

1 AA01 1002 AA02 1013 AA03 1024 BB01 2005 BB02 2016 BB03 2027 CC01 3018 CC02 3029 CC03 303

Page 11: SQL Server Blocking Analysis

2.1.c Locks Modes (cont) Shared locks (S): Used for read operations that do

not change or update data, such as a SELECT statement.

ID CA CB

3 AA03 102

RT RM RD

PAGE IS

1:154

OBJECT IS

KEY S

(03000d8f0ecc)

Begin tranSelect ID,CA,CB from dbo.tbl01 with( HOLDLOCK)where ID=3

SELECT resource_type RT, request_mode RM, resource_description RDFROM sys.dm_tran_locksWHERE resource_type <> 'DATABASE'

Commit tran

Page 12: SQL Server Blocking Analysis

2.1.c Locks Modes (cont)Exclusive locks (X): Used for data-modification operations, such as INSERT, UPDATE, or DELETE. Ensures that multiple updates cannot be made to the same resource at the same time.

BEGIN TRAN

UPDATE dbo.tbl01 SET CA = 'Exclusive Lock(X)'WHERE ID = 5

SELECT resource_type RT, request_mode RM, resource_description RDFROM sys.dm_tran_locksWHERE resource_type <> 'DATABASE'

ROLLBACK

RT RM RD

PAGE IX

1:163

OBJECT IX

OBJECT IX

KEY X

(0500d1d065e9)

Page 13: SQL Server Blocking Analysis

2.1.c Locks Modes (cont)

ID CA CB

10 CC03 303

Update locks (U): Used on resources that can be updated. Prevents a common form of deadlock that occurs when multiple sessions are reading, locking, and potentially updating resources later.

Begin tranSelect ID,CA,CB from dbo.tbl01 WITH (UPDLOCK)where CB >300

SELECT resource_type RT, request_mode RM, resource_description RDFROM sys.dm_tran_locksWHERE resource_type <> 'DATABASE'

Commit tran

RT RM RD

PAGE IU

1:163

KEY U

(0a0087c006b1)

OBJECT IX

Page 14: SQL Server Blocking Analysis

2.1.c Locks Modes (cont)Intent locks (I): Used to establish a lock hierarchy. The types of intent locks are: intent shared (IS), intent exclusive (IX), and shared with intent exclusive (SIX).

Begin tranUPDATE dbo.tbl01 SET CA = 'Test Intent locks (I)'WHERE ID = 5

SELECT resource_type RT, request_mode RM, resource_description RDFROM sys.dm_tran_locksWHERE resource_type <> 'DATABASE'

ROLLBACK

RT RM RDPAGE IX 1:163

KEY X (0500d1d065e9)

OBJECT IX

Page 15: SQL Server Blocking Analysis

2.1.c Locks Modes (cont)Schema locks (Sch): Used when an operation dependent on the schema of a table is executing. The types of schema locks are: schema modification (Sch-M) and schema stability (Sch-S).

Begin tran

CREATE TABLE tbl02 (TestColumn INT)

SELECT resource_type RT, request_mode RM, resource_description RDFROM sys.dm_tran_locksWHERE resource_type <> 'DATABASE'

ROLLBACK

RT RM RD

HOBT Sch-M

METADATA Sch-S

data_space_id = 1

OBJECT Sch-M

Page 16: SQL Server Blocking Analysis

2.1.c Locks Modes (cont) Key-Range Locks: Protects the range of rows read by a query when using the serializable transaction isolation level. Ensures that other transactions cannot insert rows that would qualify for the queries of the serializable transaction if the queries were run again.

SET TRANSACTION ISOLATION LEVEL serializable Begin tranUpdate dbo.tbl01 WITH (UPDLOCK) Set CA = 'Key-Range Locks'where CB >300SELECT resource_type RT, request_mode RM, resource_description RDFROM sys.dm_tran_locks WHERE resource_type <> 'DATABASE'Commit tran

RT RM RD

PAGE IX

1:163

KEY RangeX-X

(0a0087c006b1)

KEY RangeS-U

(ffffffffffff)

Page 17: SQL Server Blocking Analysis

2.1.c Locks Modes (cont) Bulk Update (BU): Used when bulk copying data into a

table and the TABLOCK hint is specified.

CREATE TABLE tbl03( CA VARCHAR(40),CB INT)GO

BULK INSERT tbl03FROM 'D:\Bulk.txt'WITH(FIELDTERMINATOR = ',',ROWTERMINATOR = '\n')GO

AA01,101

BB02,202

CC03,303

DD04,404

Page 18: SQL Server Blocking Analysis

2.1.d Lock Compatibility Lock compatibility controls whether multiple transactions can

acquire locks on the same resource at the same time. If a resource is already locked by another transaction, a new lock request can be granted only if the mode of the requested lock is compatible with the mode of the existing lock.

If the mode of the requested lock is not compatible with the existing lock, the transaction requesting the new lock waits for the existing lock to be released or for the lock timeout interval to expire.

Page 19: SQL Server Blocking Analysis

2.1.d Lock Compatibility (cont) Existing Lock Type

Requested Lock Type IS S U IX SIX X Sch-S SCH-M BU

Intent Shared (IS) Y Y Y Y Y N Y N N

Shared (S) Y Y Y N N N Y N N

Update (U) Y Y N N N N Y N N

Intent Exclusive (IX) Y N N Y N N Y N NShared with Intent Exclusive

(SIE) Y N N N N N Y N N

Exclusive (E) N N N N N N Y N N

Schema Stability (Sch-S) Y Y Y Y Y Y Y N Y

Schema Modify (Sch-M) N N N N N N N N N

Bulk Update (BU) N N N N N N Y N Y

Page 20: SQL Server Blocking Analysis

2.2 Transaction Isolation Levels

What is Isolation Levels in SQL Server?a

b Types of Isolation Level

Page 21: SQL Server Blocking Analysis

2.2.a What is Isolation Levels in SQL Server? Isolation levels come into play when you need

to isolate a resource for a transaction and protect that resource from other transactions. The protection is done by obtaining locks.

Lower Isolation Levels allow multiple users to access the resource simultaneously (concurrency) but they may introduce concurrency related problems such as dirty-reads and data inaccuracy.

Higher Isolation Levels eliminate concurrency related problems and increase the data accuracy but they may introduce blocking.

Page 22: SQL Server Blocking Analysis

2.2.b Types of Isolation LevelTraditionally, SQL Server has supported

six isolation levels: Read Uncommitted. Read Committed. Repeatable Read. Serializable Read. Snapshot. Read Committed Snapshot

Page 23: SQL Server Blocking Analysis

2.2.b Types of Isolation Level (cont) Read Uncommitted: This is the lowest level and can be

set, so that it provides higher concurrency but introduces all concurrency problems.

--Connection ABegin tranUPDATE dbo.tbl01 SET CA = ‘Read Uncommitted'WHERE ID = 5Commit Tran

--Connection BSelect ID,CA,CB From dbo.tbl01 WHERE ID = 5

--Change Isolation Level of Connection BSET TRANSACTION ISOLATION LEVEL READ UNCOMMITTEDSelect ID,CA,CB From dbo.tbl01 WHERE ID = 5

Connection B can see data but It not correct.This is call Dirty-Reading.

Page 24: SQL Server Blocking Analysis

2.2.b Types of Isolation Level (cont) Read Committed: This is the default Isolation Level of

SQL Server. This eliminates dirty-reads but all other concurrency related problems. You have already seen this.

CREATE PROCEDURE dbo.UpdateCB @CA NVarchar(100), @CB intASBEGIN TRANIf Exists( Select 1 from dbo.tbl01

WHERE CA = @CA)Begin WAITFOR DELAY ’00:00:05′ UPDATE dbo.tbl01 SET CB = @CB WHERE CA = @CA Commit Tran ReturnEndElseRAISERROR (‘Data not exist’, 16, 1) ;

--User A Call sp dbo.UpdateCB EXEC UpdateCB ‘AA01’,100

--After few second User B also call sp UpdateCB with difference CBEXEC UpdateCB ‘AA01’,999

User A made the update and no error message are returned but it has lost its update.

Page 25: SQL Server Blocking Analysis

2.2.b Types of Isolation Level (cont)

Repeatable Read Isolation Level:

This Isolation Level addresses all concurrency related problems except Phantom reads. Unlike Read Committed, it does not release the shared lock once the record is read. This stops other transactions accessing the resource, avoiding Lost Updates and Nonrepeatable reads.

Page 26: SQL Server Blocking Analysis

2.2.b Types of Isolation Level (cont)CREATE PROCEDURE dbo.UpdateCB @CA NVarchar(100), @CB intASSET TRANSACTION ISOLATION LEVEL REPEATABLE READ BEGIN TRANIf Exists( Select 1 from dbo.tbl01

WHERE CA = @CA)

Begin WAITFOR DELAY ’00:00:05′ UPDATE dbo.tbl01 SET CB = @CB WHERE CA = @CA Commit Tran ReturnEnd

ElseRAISERROR (‘Data not exist’, 16, 1) ;

--User A Call sp dbo.UpdateCB EXEC UpdateCB ‘AA01’,100

-- After few seconds, User B also call sp UpdateCB with difference CB

EXEC UpdateCB ‘AA01’,999

User B have been received throw 1205 error of SQL Server and Connection2 will be a deadlock victim.

Page 27: SQL Server Blocking Analysis

--Create new table tbl02 and Add Column CC into tbl01CREATE TABLE dbo.tbl02 (CB int, CC int DEFAULT(0) )ALTER TABLE dbo.tbl01 ADD CC bit DEFAULT(0) NOT NULL--Create sp to insert data tbl01 to tbl02 with condition CB>300Create PROCEDURE dbo.AddColCC ASBEGINBEGIN TRANSET TRANSACTION ISOLATION LEVEL REPEATABLE READ INSERT INTO dbo.tbl02(CB, CC)SELECT CB,100 FROM dbo.tbl01WHERE CC = 0 AND CB > 300WAITFOR DELAY '00:00:05'UPDATE tbl01 SET CC = 1 WHERE CC = 0 AND CB > 300COMMIT TRANEND --User A call sp AddColCCexec AddColCC

--User B insert data to tbl01 with CB>300insert into tbl01(CA,CB)Values('Test REPEATABLE READ',304)

Step01

Step02

Step03 Step04

Page 28: SQL Server Blocking Analysis

Result of User A & Btbl01 tbl02

ID CA CB CC CB CC

8 CC01 301 1 301 100

9 CC02 302 1 302 100

10 CC03 303 1 303 100

11 Test REPEATABLE READ 304 1    

In this case, we have an problem which is Phantom Reads.To avoid this problem, we need to use hightest Isolation Level that is Serializable.

Page 29: SQL Server Blocking Analysis

2.2.b Types of Isolation Level (cont) Serializable Isolation LevelThis is the highest Isolation Level and it avoids all

the concurrency related problems. The behavior of this level is just like the Repeatable

Read with one additional feature. It obtains key range locks based on the filters that

have been used.It locks not only current records that stratify the

filter but new records fall into same filter.

Page 30: SQL Server Blocking Analysis

--Alter sp to insert data tbl01 to tbl02 with condition CB>300Alter PROCEDURE dbo.AddColCC ASBEGINSET TRANSACTION ISOLATION LEVEL SerializableBEGIN TRANINSERT INTO dbo.tbl02(CB, CC)SELECT CB,100 FROM dbo.tbl01WHERE CC = 0 AND CB > 300WAITFOR DELAY '00:00:05'UPDATE tbl01 SET CC = 1 WHERE CC = 0 AND CB > 300COMMIT TRANEND

Step01

--Bring tbl01 & tbl02 table to original stateUpdate dbo.tbl01 set CC=0Delete FROM dbo.tbl01 where ID>10Delete FROM dbo.tbl02

--User A call sp AddColCCexec AddColCC

--User B insert data to tbl01 with CB>300insert into tbl01(CA,CB)Values('Test REPEATABLE READ',304)Step03 Step04

Step02

Page 31: SQL Server Blocking Analysis

Result of User A & Btbl01 tbl02

ID CA CB CC CB CC

8 CC01 301 1 301 100

9 CC02 302 1 302 100

10 CC03 303 1 303 100

12 Test REPEATABLE READ 304 0    

Connection of User B will be blocked until connection of User A completes the transaction, it is avoiding Phantom Reads

Page 32: SQL Server Blocking Analysis

2.2.b Types of Isolation Level (cont) The Snapshot Isolation Level works with Row Versioning

technology. Whenever the transaction requires a modification for a record, SQL Server first stores the consistence version of the record in the tempdb.

If another transaction that runs under Snapshot Isolation Level requires the same record, it can be taken from the version store.

This Isolation Level prevents all concurrency related problems just like Serializable Isolation Level, in addition to that it allows multiple updates for same resource by different transactions concurrently.

ALTER DATABASE [DB Name] SET ALLOW_SNAPSHOT_ISOLATION ON

Page 33: SQL Server Blocking Analysis

--Alter sp to insert data tbl01 to tbl02 with condition CB>300Alter PROCEDURE dbo.AddColCC ASBEGINSET TRANSACTION ISOLATION LEVEL SNAPSHOTBEGIN TRANINSERT INTO dbo.tbl02(CB, CC)SELECT CB,100 FROM dbo.tbl01WHERE CC = 0 AND CB > 300WAITFOR DELAY '00:00:05'UPDATE tbl01 SET CC = 1 WHERE CC = 0 AND CB > 300COMMIT TRANEND

Step02

--Bring tbl01 & tbl02 table to original stateUpdate dbo.tbl01 set CC=0Delete FROM dbo.tbl01 where ID>10Delete FROM dbo.tbl02

--User A call sp AddColCCexec AddColCC

--User B insert data to tbl01 with CB>300insert into tbl01(CA,CB)Values(‘ISOLATION LEVEL SNAPSHOT',305)Step04

Step05

Step03

--Enable Allow_Snapshot_Isolation on local DB TestALTER DATABASE [DB_Test] SET ALLOW_SNAPSHOT_ISOLATION ON

Step01

Page 34: SQL Server Blocking Analysis

Result of User A & Btbl01 tbl02

ID CA CB CC CB CC

8 CC01 301 1 301 100

9 CC02 302 1 302 100

10 CC03 303 1 303 100

13 SNAPSHOT 305 0    

The result is the same setting Serializable Isolation Level

Page 35: SQL Server Blocking Analysis

No User A User B

0  SET TRANSACTION ISOLATION

LEVEL SNAPSHOT

1 Begin tran Begin tran

2

Update dbo.tbl01 Set CA='SNAPSHOT-1‘Where CB=303

Select ID, CA, CB From tbl01 Where CB=303

3

Select ID, CA, CB From tbl01Where CB=303

Update dbo.tbl01 Set CA='SNAPSHOT-2' Where CB=303

4 Commit Commit

Example 2

Page 36: SQL Server Blocking Analysis

No User A User B

2 (1 row(s) affected) Return data with CA='SNAPSHOT‘

3Return data with

CA='SNAPSHOT-1‘ Processing

4

Return message:

Command(s) completed

successfully.

Return message: Snapshot isolation transaction aborted due to update conflict. You cannot use snapshot isolation to access table 'dbo.tbl01' directly or indirectly in database 'DB_Test' to update, delete, or insert the row that has been modified or deleted by another transaction. Retry the transaction or change the isolation level for the update/delete statement.

Result of User A & B

Page 37: SQL Server Blocking Analysis

2.2.b Types of Isolation Level (cont) Read Committed Snapshot is the new

implementation of the Read Committed Isolation Level.

It has to be set not at session/connection level but database level.

The Read Committed Snapshot differs from Snapshot in two ways; Unlike Snapshot, it always returns latest consistence version and no conflict detection.

ALTER DATABASE [DBName] SET READ_COMMITTED_SNAPSHOT ON

Page 38: SQL Server Blocking Analysis

Summarize of Isolation Level

 Dirty

ReadsLostUpdates

Nonrepeatable

readsPhantom

readsConflictDetection

Read Uncommitted Yes Yes Yes Yes No

Read Committed No Yes Yes Yes No

Repeatable Read No Yes Yes Yes No

Serializable No No No No No

Snapshot No No No No Yes

Read Committed Snapshot No Yes Yes Yes No

Page 39: SQL Server Blocking Analysis

3 Blocking in the system

Purpose of Blocking.

1

2

What is Blocking in SQL Server?

3 Detecting SQL Server Blocking

Page 40: SQL Server Blocking Analysis

3.1 What is Blocking in SQL Server? Blocking in SQL Server is a scenario where one

connection to SQL Server locks one or more records, and a second connection to SQL Server requires a conflicting lock type on the record or records locked by the first connection.

This causes the second connection to wait until the first connection releases its locks.

By default, a connection will wait an unlimited amount of time for the blocking lock to go away.

Page 41: SQL Server Blocking Analysis

3.2 Purpose of Blocking.

Dirty Reads Lost

Updates

Phantom reads

Nonrepeatable

reads

Page 42: SQL Server Blocking Analysis

3.3 Detecting SQL Server Blocking

What is Dead Lock?a

b Detection SQL Server Blocking.

Page 43: SQL Server Blocking Analysis

3.3.a What is Dead Lock? A deadlock occurs when two or more tasks

permanently block each other by each task having a lock on a resource which the other tasks are trying to lock.

Page 44: SQL Server Blocking Analysis

3.3.b Detection SQL Server Blocking

No User A User B1 Begin tran Begin tran

2

Update dbo.tbl01 Set CA='SNAPSHOT-1‘Where CB=303

Select ID, CA, CB From tbl01 Where CB=303

3

Select ID, CA, CB From tbl01Where CB=303

Update dbo.tbl01 Set CA='SNAPSHOT-2' Where CB=303

4 --Commit Commit

Profiler Trace

ActivityMonitor

ReportService

Page 45: SQL Server Blocking Analysis

SQL Server Profiler TraceEnable Blocked process threshold on Database

sp_configure 'show advanced options', 1GORECONFIGUREGOsp_configure 'blocked process threshold'GOsp_configure 'blocked process threshold', 5 -–5 is time blockedGORECONFIGUREGO

Page 46: SQL Server Blocking Analysis

SQL Server Profiler TraceUse Tool SQL Server Profiler, connect Database to monitor.On Tab ‘General’, call the Trace name as ‘CheckDeadlocks’. Then choose a template ‘Blank’. Check the checkbox ‘Save to File’ and save the file in a preferred location.

Page 47: SQL Server Blocking Analysis

SQL Server Profiler Trace (Cont)Expand the ‘Errors and Warnings’ section and select the ‘Blocked Process Report’ Item.

Page 48: SQL Server Blocking Analysis

SQL Server Profiler Trace (Cont)

After the trace is run the *.trc file can be viewed in SQL Server Profiler or can be loaded into a database. It will show an XML view of what query was being blocked and what query was doing the blocking.

Page 49: SQL Server Blocking Analysis

SQL Server Activity Monitor

This tool is a component of the SQL Server Management Studio. It helps in  getting information about users connection, and locks that may happen because of different reasons. There are 3 pages in the Activity Monitor:1 - Process Info Page - contains information about all connections. 2 - Locks by Process Page - contains sorted information by locks on the connections.3 - Locks by Object Page - Contains information about locks sorted based on the object.Whenever a lock occurs in a database, the Activity Monitor is the best place to view, in order to figure out the cause of the lock. Its important to note here that in order to view the Activity Monitor, the user needs to have the VIEW SERVER STATE permission on the SQL Server he/she is working on.

Page 50: SQL Server Blocking Analysis

SQL Server Activity Monitor (Cont)

Page 51: SQL Server Blocking Analysis

SQL Server Report Services On SQL Server we can use SQL Report Service to detect block

transaction.

Page 52: SQL Server Blocking Analysis
Page 53: SQL Server Blocking Analysis

QUESTION AND ANSWERS

Page 54: SQL Server Blocking Analysis

THANK YOU FOR YOUR ATTENTION!

Page 55: SQL Server Blocking Analysis

Reference document http://www.google.com http://technet.microsoft.com/en-us/library/

ms187101%28SQL.90%29.aspx http://etutorials.org/SQL/

microsoft+sql+server+2000/Part+V+SQL+Server+Internals+and+Performance+Tuning/Chapter+38.+Locking+and+Performance/

http://aboutsqlserver.com/2011/04/14/locking-in-microsoft-sql-server-part-1-lock-types


Recommended