SlideShare a Scribd company logo
Blocking Analysis
Vo Tan Hau
July 08 ,2011
MS SQL Server
CONTENTS
 Overview SQL Server.
 Locks And Transaction isolation levels
 SQL Server Blocking.
 Q&A.
1.Overview SQL Server
Relational Database
Management System
SQL ServerClient
Results
Client Application
OLAP
OLTPQuery
Client-Server Communication Process
Client Application
Client Net-Library
Client
SQL Server
Relational
Engine
Storage Engine
Server
Local
Database
Database API
(OLE DB, ODBC,
DB-Library)
Memory
Open Data Services
Server Net-Libraries
Query
Result Set
Result Set
Query
1
2
3
4
5
Tabular Data Stream(TDS)
Processor
Lock in MS SQL Server.
2. Locks & Transaction Isolation Levels
1
2 Transaction Isolation Levels.
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.
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.
2.1.b Lock Granularity and Hierarchies
Resource Description
RID A row identifier used to lock a single row within a heap.
KEY
A 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.
HOBT
A 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.
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
2.1.c Locks Modes (cont)
ID CA CB
1 AA01 100
2 AA02 101
3 AA03 102
4 BB01 200
5 BB02 201
6 BB03 202
7 CC01 301
8 CC02 302
9 CC03 303
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
(03000d8f0
ecc)
Begin tran
Select ID,CA,CB
from dbo.tbl01 with( HOLDLOCK)
where ID=3
SELECT resource_type RT,
request_mode RM,
resource_description RD
FROM sys.dm_tran_locks
WHERE resource_type <> 'DATABASE'
Commit tran
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 RD
FROM sys.dm_tran_locks
WHERE resource_type <>
'DATABASE'
ROLLBACK
RT RM RD
PAGE IX 1:163
OBJECT IX
OBJECT IX
KEY X (0500d1d065e9)
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 tran
Select ID,CA,CB from dbo.tbl01
WITH (UPDLOCK)
where CB >300
SELECT resource_type RT,
request_mode RM,
resource_description RD
FROM sys.dm_tran_locks
WHERE resource_type <>
'DATABASE'
Commit tran
RT RM RD
PAGE IU 1:163
KEY U (0a0087c006b1)
OBJECT IX
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 tran
UPDATE dbo.tbl01
SET CA = 'Test Intent locks (I)'
WHERE ID = 5
SELECT resource_type RT,
request_mode RM,
resource_description RD
FROM sys.dm_tran_locks
WHERE resource_type <>
'DATABASE'
ROLLBACK
RT RM RD
PAGE IX 1:163
KEY X (0500d1d065e9)
OBJECT IX
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 RD
FROM sys.dm_tran_locks
WHERE resource_type <>
'DATABASE'
ROLLBACK
RT RM RD
HOBT Sch-M
METADATA Sch-S data_space_id = 1
OBJECT Sch-M
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 tran
Update dbo.tbl01 WITH (UPDLOCK) Set CA = 'Key-Range Locks'
where CB >300
SELECT resource_type RT, request_mode RM, resource_description RD
FROM 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)
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 tbl03
FROM 'D:Bulk.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = 'n'
)
GO
AA01,101
BB02,202
CC03,303
DD04,404
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.
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 N
Shared 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
2.2 Transaction Isolation Levels
What is Isolation Levels in SQL Server?a
b Types of Isolation Level
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.
2.2.b Types of Isolation Level
Traditionally, SQL Server has
supported six isolation levels:
 Read Uncommitted.
 Read Committed.
 Repeatable Read.
 Serializable Read.
 Snapshot.
 Read Committed Snapshot
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 A
Begin tran
UPDATE dbo.tbl01
SET CA = ‘Read Uncommitted'
WHERE ID = 5
Commit Tran
--Connection B
Select ID,CA,CB
From dbo.tbl01
WHERE ID = 5
--Change Isolation Level of Connection B
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
Select ID,CA,CB
From dbo.tbl01
WHERE ID = 5
Connection B can see data but It not correct.
This is call Dirty-Reading.
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 int
AS
BEGIN TRAN
If 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
Return
End
Else
RAISERROR (‘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
CB
EXEC UpdateCB ‘AA01’,999
User A made the update and
no error message are
returned but it has lost its
update.
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.
2.2.b Types of Isolation Level (cont)
CREATE PROCEDURE dbo.UpdateCB
@CA NVarchar(100), @CB int
AS
SET TRANSACTION ISOLATION LEVEL
REPEATABLE READ
BEGIN TRAN
If 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
Return
End
Else
RAISERROR (‘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.
--Create new table tbl02 and Add Column CC into tbl01
CREATE 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>300
Create PROCEDURE dbo.AddColCC
AS
BEGIN
BEGIN TRAN
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
INSERT INTO dbo.tbl02(CB, CC)
SELECT CB,100 FROM dbo.tbl01
WHERE CC = 0 AND CB > 300
WAITFOR DELAY '00:00:05'
UPDATE tbl01 SET CC = 1 WHERE CC = 0 AND CB > 300
COMMIT TRAN
END
--User A call sp AddColCC
exec AddColCC
--User B insert data to tbl01 with CB>300
insert into tbl01(CA,CB)
Values('Test REPEATABLE READ',304)
Step01
Step02
Step03 Step04
Result of User A & B
tbl01 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.
2.2.b Types of Isolation Level (cont)
 Serializable Isolation Level
This 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.
--Alter sp to insert data tbl01 to tbl02 with condition CB>300
Alter PROCEDURE dbo.AddColCC
AS
BEGIN
SET TRANSACTION ISOLATION LEVEL Serializable
BEGIN TRAN
INSERT INTO dbo.tbl02(CB, CC)
SELECT CB,100 FROM dbo.tbl01
WHERE CC = 0 AND CB > 300
WAITFOR DELAY '00:00:05'
UPDATE tbl01 SET CC = 1 WHERE CC = 0 AND CB > 300
COMMIT TRAN
END
Step01
--Bring tbl01 & tbl02 table to original state
Update dbo.tbl01 set CC=0
Delete FROM dbo.tbl01 where ID>10
Delete FROM dbo.tbl02
--User A call sp AddColCC
exec AddColCC
--User B insert data to tbl01 with CB>300
insert into tbl01(CA,CB)
Values('Test REPEATABLE READ',304)Step03 Step04
Step02
Result of User A & B
tbl01 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
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
--Alter sp to insert data tbl01 to tbl02 with condition CB>300
Alter PROCEDURE dbo.AddColCC
AS
BEGIN
SET TRANSACTION ISOLATION LEVEL SNAPSHOT
BEGIN TRAN
INSERT INTO dbo.tbl02(CB, CC)
SELECT CB,100 FROM dbo.tbl01
WHERE CC = 0 AND CB > 300
WAITFOR DELAY '00:00:05'
UPDATE tbl01 SET CC = 1 WHERE CC = 0 AND CB > 300
COMMIT TRAN
END
Step02
--Bring tbl01 & tbl02 table to original state
Update dbo.tbl01 set CC=0
Delete FROM dbo.tbl01 where ID>10
Delete FROM dbo.tbl02
--User A call sp AddColCC
exec AddColCC
--User B insert data to tbl01 with CB>300
insert into tbl01(CA,CB)
Values(‘ISOLATION LEVEL SNAPSHOT',305)Step04
Step05
Step03
--Enable Allow_Snapshot_Isolation on local DB Test
ALTER DATABASE [DB_Test]
SET ALLOW_SNAPSHOT_ISOLATION ON
Step01
Result of User A & B
tbl01 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
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 tbl01
Where CB=303
Update dbo.tbl01
Set CA='SNAPSHOT-2'
Where CB=303
4 Commit Commit
Example 2
No User A User B
2 (1 row(s) affected) Return data with CA='SNAPSHOT‘
3
Return 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
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
Summarize of Isolation Level
Dirty
Reads
Lost
Updates
Non
repeatable
reads
Phantom
reads
Conflict
Detection
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
3 Blocking in the system
Purpose of Blocking.
1
2
What is Blocking in SQL Server?
3 Detecting SQL Server Blocking
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.
3.2 Purpose of Blocking.
Dirty
Reads
Lost
Updates
Phantom
reads
Non
repeatable
reads
3.3 Detecting SQL Server Blocking
What is Dead Lock?a
b Detection SQL Server Blocking.
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.
3.3.b Detection SQL Server Blocking
No User A User B
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 tbl01
Where CB=303
Update dbo.tbl01
Set CA='SNAPSHOT-2'
Where CB=303
4 --Commit Commit
Profiler
Trace
Activity
Monitor
Report
Service
SQL Server Profiler Trace
Enable Blocked process threshold on Database
sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
sp_configure 'blocked process threshold'
GO
sp_configure 'blocked process threshold', 5 -–5 is time blocked
GO
RECONFIGURE
GO
SQL Server Profiler Trace
Use 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.
SQL Server Profiler Trace (Cont)
Expand the ‘Errors and Warnings’ section and select the
‘Blocked Process Report’ Item.
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.
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.
SQL Server Activity Monitor (Cont)
SQL Server Report Services
 On SQL Server we can use SQL Report Service to detect block
transaction.
QUESTION AND ANSWERS
THANK YOU FOR YOUR ATTENTION!
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+Perfo
rmance+Tuning/Chapter+38.+Locking+and+Perf
ormance/
 http://aboutsqlserver.com/2011/04/14/locking-
in-microsoft-sql-server-part-1-lock-types

More Related Content

What's hot

SQL Queries
SQL QueriesSQL Queries
SQL Queries
Nilt1234
 
Sql Server Performance Tuning
Sql Server Performance TuningSql Server Performance Tuning
Sql Server Performance Tuning
Bala Subra
 
Oracle Apps - Forms
Oracle Apps - FormsOracle Apps - Forms
Oracle Apps - Forms
Bhaskara Reddy Sannapureddy
 
Database Transactions and SQL Server Concurrency
Database Transactions and SQL Server ConcurrencyDatabase Transactions and SQL Server Concurrency
Database Transactions and SQL Server Concurrency
Boris Hristov
 
SQL JOINS
SQL JOINSSQL JOINS
SQL JOINS
Swapnali Pawar
 
Oracle database performance tuning
Oracle database performance tuningOracle database performance tuning
Oracle database performance tuning
Yogiji Creations
 
MySQL Views
MySQL ViewsMySQL Views
153 Oracle dba interview questions
153 Oracle dba interview questions153 Oracle dba interview questions
153 Oracle dba interview questions
Sandeep Sharma IIMK Smart City,IoT,Bigdata,Cloud,BI,DW
 
Best Pl/sql SQL Interview Questions
Best Pl/sql SQL Interview QuestionsBest Pl/sql SQL Interview Questions
Best Pl/sql SQL Interview Questions
Ashwani Pandey
 
Procesamiento y Mantenimiento de Archivos
Procesamiento y Mantenimiento de ArchivosProcesamiento y Mantenimiento de Archivos
Procesamiento y Mantenimiento de Archivos
Rosmyl Giomar
 
Oracle db performance tuning
Oracle db performance tuningOracle db performance tuning
Oracle db performance tuning
Simon Huang
 
Stored procedure
Stored procedureStored procedure
Oracle EBS R12 Audit trial
Oracle EBS R12 Audit trialOracle EBS R12 Audit trial
Oracle EBS R12 Audit trial
Feras Ahmad
 
The MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer TraceThe MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer Trace
oysteing
 
09 Managing Dependencies
09 Managing Dependencies09 Managing Dependencies
09 Managing Dependencies
rehaniltifat
 
05 Creating Stored Procedures
05 Creating Stored Procedures05 Creating Stored Procedures
05 Creating Stored Procedures
rehaniltifat
 
Oracle EBS 12.1.3 : Integrate OA Framework BC4J components within java concur...
Oracle EBS 12.1.3 : Integrate OA Framework BC4J components within java concur...Oracle EBS 12.1.3 : Integrate OA Framework BC4J components within java concur...
Oracle EBS 12.1.3 : Integrate OA Framework BC4J components within java concur...
Amit Singh
 
Database Change Management as a Service
Database Change Management as a ServiceDatabase Change Management as a Service
Database Change Management as a Service
Andrew Solomon
 
Giải pháp always on trong sql server 2012
Giải pháp always on trong sql server 2012Giải pháp always on trong sql server 2012
Giải pháp always on trong sql server 2012
laonap166
 
PLSQL Cursors
PLSQL CursorsPLSQL Cursors
PLSQL Cursors
spin_naresh
 

What's hot (20)

SQL Queries
SQL QueriesSQL Queries
SQL Queries
 
Sql Server Performance Tuning
Sql Server Performance TuningSql Server Performance Tuning
Sql Server Performance Tuning
 
Oracle Apps - Forms
Oracle Apps - FormsOracle Apps - Forms
Oracle Apps - Forms
 
Database Transactions and SQL Server Concurrency
Database Transactions and SQL Server ConcurrencyDatabase Transactions and SQL Server Concurrency
Database Transactions and SQL Server Concurrency
 
SQL JOINS
SQL JOINSSQL JOINS
SQL JOINS
 
Oracle database performance tuning
Oracle database performance tuningOracle database performance tuning
Oracle database performance tuning
 
MySQL Views
MySQL ViewsMySQL Views
MySQL Views
 
153 Oracle dba interview questions
153 Oracle dba interview questions153 Oracle dba interview questions
153 Oracle dba interview questions
 
Best Pl/sql SQL Interview Questions
Best Pl/sql SQL Interview QuestionsBest Pl/sql SQL Interview Questions
Best Pl/sql SQL Interview Questions
 
Procesamiento y Mantenimiento de Archivos
Procesamiento y Mantenimiento de ArchivosProcesamiento y Mantenimiento de Archivos
Procesamiento y Mantenimiento de Archivos
 
Oracle db performance tuning
Oracle db performance tuningOracle db performance tuning
Oracle db performance tuning
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 
Oracle EBS R12 Audit trial
Oracle EBS R12 Audit trialOracle EBS R12 Audit trial
Oracle EBS R12 Audit trial
 
The MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer TraceThe MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer Trace
 
09 Managing Dependencies
09 Managing Dependencies09 Managing Dependencies
09 Managing Dependencies
 
05 Creating Stored Procedures
05 Creating Stored Procedures05 Creating Stored Procedures
05 Creating Stored Procedures
 
Oracle EBS 12.1.3 : Integrate OA Framework BC4J components within java concur...
Oracle EBS 12.1.3 : Integrate OA Framework BC4J components within java concur...Oracle EBS 12.1.3 : Integrate OA Framework BC4J components within java concur...
Oracle EBS 12.1.3 : Integrate OA Framework BC4J components within java concur...
 
Database Change Management as a Service
Database Change Management as a ServiceDatabase Change Management as a Service
Database Change Management as a Service
 
Giải pháp always on trong sql server 2012
Giải pháp always on trong sql server 2012Giải pháp always on trong sql server 2012
Giải pháp always on trong sql server 2012
 
PLSQL Cursors
PLSQL CursorsPLSQL Cursors
PLSQL Cursors
 

Similar to SQL Server Blocking Analysis

Ibm db2 case study
Ibm db2 case studyIbm db2 case study
Ibm db2 case study
Ishvitha Badhri
 
Sql server
Sql serverSql server
Sql server
Puja Gupta
 
Database concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal OlierDatabase concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal Olier
sqlserver.co.il
 
Sql basics 2
Sql basics   2Sql basics   2
Sql basics 2
Raghu nath
 
Sql server-dba
Sql server-dbaSql server-dba
Sql server-dba
NaviSoft
 
Finding root blocker in oracle database
Finding root blocker in oracle databaseFinding root blocker in oracle database
Finding root blocker in oracle database
Mohsen B
 
Ms sql server architecture
Ms sql server architectureMs sql server architecture
Ms sql server architecture
Ajeet Singh
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
Alex Zaballa
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
Alex Zaballa
 
oracle dba
oracle dbaoracle dba
oracle dba
uday jampani
 
SQL under the hood
SQL under the hoodSQL under the hood
SQL under the hood
Eduardo Castro
 
Oracle notes
Oracle notesOracle notes
Oracle notes
Prashant Dadmode
 
12c Database new features
12c Database new features12c Database new features
12c Database new features
Sandeep Redkar
 
Network and distributed systems
Network and distributed systemsNetwork and distributed systems
Network and distributed systems
Sri Prasanna
 
Welcome to the nightmare of locking, blocking and isolation levels!
Welcome to the nightmare of locking, blocking and isolation levels!Welcome to the nightmare of locking, blocking and isolation levels!
Welcome to the nightmare of locking, blocking and isolation levels!
Boris Hristov
 
MySQL Overview
MySQL OverviewMySQL Overview
MySQL Overview
Andrey Sidelev
 
Database security
Database securityDatabase security
Database security
Javed Khan
 
AWR Sample Report
AWR Sample ReportAWR Sample Report
AWR Sample Report
Devendra Singh
 
MySQL HA with PaceMaker
MySQL HA with  PaceMakerMySQL HA with  PaceMaker
MySQL HA with PaceMaker
Kris Buytaert
 
Introduction to Threading in .Net
Introduction to Threading in .NetIntroduction to Threading in .Net
Introduction to Threading in .Net
webhostingguy
 

Similar to SQL Server Blocking Analysis (20)

Ibm db2 case study
Ibm db2 case studyIbm db2 case study
Ibm db2 case study
 
Sql server
Sql serverSql server
Sql server
 
Database concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal OlierDatabase concurrency and transactions - Tal Olier
Database concurrency and transactions - Tal Olier
 
Sql basics 2
Sql basics   2Sql basics   2
Sql basics 2
 
Sql server-dba
Sql server-dbaSql server-dba
Sql server-dba
 
Finding root blocker in oracle database
Finding root blocker in oracle databaseFinding root blocker in oracle database
Finding root blocker in oracle database
 
Ms sql server architecture
Ms sql server architectureMs sql server architecture
Ms sql server architecture
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
 
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
OOW16 - Oracle Database 12c - The Best Oracle Database 12c New Features for D...
 
oracle dba
oracle dbaoracle dba
oracle dba
 
SQL under the hood
SQL under the hoodSQL under the hood
SQL under the hood
 
Oracle notes
Oracle notesOracle notes
Oracle notes
 
12c Database new features
12c Database new features12c Database new features
12c Database new features
 
Network and distributed systems
Network and distributed systemsNetwork and distributed systems
Network and distributed systems
 
Welcome to the nightmare of locking, blocking and isolation levels!
Welcome to the nightmare of locking, blocking and isolation levels!Welcome to the nightmare of locking, blocking and isolation levels!
Welcome to the nightmare of locking, blocking and isolation levels!
 
MySQL Overview
MySQL OverviewMySQL Overview
MySQL Overview
 
Database security
Database securityDatabase security
Database security
 
AWR Sample Report
AWR Sample ReportAWR Sample Report
AWR Sample Report
 
MySQL HA with PaceMaker
MySQL HA with  PaceMakerMySQL HA with  PaceMaker
MySQL HA with PaceMaker
 
Introduction to Threading in .Net
Introduction to Threading in .NetIntroduction to Threading in .Net
Introduction to Threading in .Net
 

Recently uploaded

怎么办理(lincoln学位证书)英国林肯大学毕业证文凭学位证书原版一模一样
怎么办理(lincoln学位证书)英国林肯大学毕业证文凭学位证书原版一模一样怎么办理(lincoln学位证书)英国林肯大学毕业证文凭学位证书原版一模一样
怎么办理(lincoln学位证书)英国林肯大学毕业证文凭学位证书原版一模一样
kekzed
 
Competition and Regulation in Professions and Occupations – OECD – June 2024 ...
Competition and Regulation in Professions and Occupations – OECD – June 2024 ...Competition and Regulation in Professions and Occupations – OECD – June 2024 ...
Competition and Regulation in Professions and Occupations – OECD – June 2024 ...
OECD Directorate for Financial and Enterprise Affairs
 
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
OECD Directorate for Financial and Enterprise Affairs
 
ASONAM2023_presection_slide_track-recommendation.pdf
ASONAM2023_presection_slide_track-recommendation.pdfASONAM2023_presection_slide_track-recommendation.pdf
ASONAM2023_presection_slide_track-recommendation.pdf
ToshihiroIto4
 
The Intersection between Competition and Data Privacy – KEMP – June 2024 OECD...
The Intersection between Competition and Data Privacy – KEMP – June 2024 OECD...The Intersection between Competition and Data Privacy – KEMP – June 2024 OECD...
The Intersection between Competition and Data Privacy – KEMP – June 2024 OECD...
OECD Directorate for Financial and Enterprise Affairs
 
The Intersection between Competition and Data Privacy – COLANGELO – June 2024...
The Intersection between Competition and Data Privacy – COLANGELO – June 2024...The Intersection between Competition and Data Privacy – COLANGELO – June 2024...
The Intersection between Competition and Data Privacy – COLANGELO – June 2024...
OECD Directorate for Financial and Enterprise Affairs
 
Disaster Management project for holidays homework and other uses
Disaster Management project for holidays homework and other usesDisaster Management project for holidays homework and other uses
Disaster Management project for holidays homework and other uses
RIDHIMAGARG21
 
The Intersection between Competition and Data Privacy – OECD – June 2024 OECD...
The Intersection between Competition and Data Privacy – OECD – June 2024 OECD...The Intersection between Competition and Data Privacy – OECD – June 2024 OECD...
The Intersection between Competition and Data Privacy – OECD – June 2024 OECD...
OECD Directorate for Financial and Enterprise Affairs
 
Pro-competitive Industrial Policy – LANE – June 2024 OECD discussion
Pro-competitive Industrial Policy – LANE – June 2024 OECD discussionPro-competitive Industrial Policy – LANE – June 2024 OECD discussion
Pro-competitive Industrial Policy – LANE – June 2024 OECD discussion
OECD Directorate for Financial and Enterprise Affairs
 
BRIC_2024_2024-06-06-11:30-haunschild_archival_version.pdf
BRIC_2024_2024-06-06-11:30-haunschild_archival_version.pdfBRIC_2024_2024-06-06-11:30-haunschild_archival_version.pdf
BRIC_2024_2024-06-06-11:30-haunschild_archival_version.pdf
Robin Haunschild
 
Artificial Intelligence, Data and Competition – LIM – June 2024 OECD discussion
Artificial Intelligence, Data and Competition – LIM – June 2024 OECD discussionArtificial Intelligence, Data and Competition – LIM – June 2024 OECD discussion
Artificial Intelligence, Data and Competition – LIM – June 2024 OECD discussion
OECD Directorate for Financial and Enterprise Affairs
 
IEEE CIS Webinar Sustainable futures.pdf
IEEE CIS Webinar Sustainable futures.pdfIEEE CIS Webinar Sustainable futures.pdf
IEEE CIS Webinar Sustainable futures.pdf
Claudio Gallicchio
 
原版制作贝德福特大学毕业证(bedfordhire毕业证)硕士文凭原版一模一样
原版制作贝德福特大学毕业证(bedfordhire毕业证)硕士文凭原版一模一样原版制作贝德福特大学毕业证(bedfordhire毕业证)硕士文凭原版一模一样
原版制作贝德福特大学毕业证(bedfordhire毕业证)硕士文凭原版一模一样
gpww3sf4
 
Carrer goals.pptx and their importance in real life
Carrer goals.pptx  and their importance in real lifeCarrer goals.pptx  and their importance in real life
Carrer goals.pptx and their importance in real life
artemacademy2
 
Using-Presentation-Software-to-the-Fullf.pptx
Using-Presentation-Software-to-the-Fullf.pptxUsing-Presentation-Software-to-the-Fullf.pptx
Using-Presentation-Software-to-the-Fullf.pptx
kainatfatyma9
 
Artificial Intelligence, Data and Competition – OECD – June 2024 OECD discussion
Artificial Intelligence, Data and Competition – OECD – June 2024 OECD discussionArtificial Intelligence, Data and Competition – OECD – June 2024 OECD discussion
Artificial Intelligence, Data and Competition – OECD – June 2024 OECD discussion
OECD Directorate for Financial and Enterprise Affairs
 
XP 2024 presentation: A New Look to Leadership
XP 2024 presentation: A New Look to LeadershipXP 2024 presentation: A New Look to Leadership
XP 2024 presentation: A New Look to Leadership
samililja
 
Artificial Intelligence, Data and Competition – ČORBA – June 2024 OECD discus...
Artificial Intelligence, Data and Competition – ČORBA – June 2024 OECD discus...Artificial Intelligence, Data and Competition – ČORBA – June 2024 OECD discus...
Artificial Intelligence, Data and Competition – ČORBA – June 2024 OECD discus...
OECD Directorate for Financial and Enterprise Affairs
 
The Intersection between Competition and Data Privacy – CAPEL – June 2024 OEC...
The Intersection between Competition and Data Privacy – CAPEL – June 2024 OEC...The Intersection between Competition and Data Privacy – CAPEL – June 2024 OEC...
The Intersection between Competition and Data Privacy – CAPEL – June 2024 OEC...
OECD Directorate for Financial and Enterprise Affairs
 
The remarkable life of Sir Mokshagundam Visvesvaraya.pptx
The remarkable life of Sir Mokshagundam Visvesvaraya.pptxThe remarkable life of Sir Mokshagundam Visvesvaraya.pptx
The remarkable life of Sir Mokshagundam Visvesvaraya.pptx
JiteshKumarChoudhary2
 

Recently uploaded (20)

怎么办理(lincoln学位证书)英国林肯大学毕业证文凭学位证书原版一模一样
怎么办理(lincoln学位证书)英国林肯大学毕业证文凭学位证书原版一模一样怎么办理(lincoln学位证书)英国林肯大学毕业证文凭学位证书原版一模一样
怎么办理(lincoln学位证书)英国林肯大学毕业证文凭学位证书原版一模一样
 
Competition and Regulation in Professions and Occupations – OECD – June 2024 ...
Competition and Regulation in Professions and Occupations – OECD – June 2024 ...Competition and Regulation in Professions and Occupations – OECD – June 2024 ...
Competition and Regulation in Professions and Occupations – OECD – June 2024 ...
 
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
Artificial Intelligence, Data and Competition – SCHREPEL – June 2024 OECD dis...
 
ASONAM2023_presection_slide_track-recommendation.pdf
ASONAM2023_presection_slide_track-recommendation.pdfASONAM2023_presection_slide_track-recommendation.pdf
ASONAM2023_presection_slide_track-recommendation.pdf
 
The Intersection between Competition and Data Privacy – KEMP – June 2024 OECD...
The Intersection between Competition and Data Privacy – KEMP – June 2024 OECD...The Intersection between Competition and Data Privacy – KEMP – June 2024 OECD...
The Intersection between Competition and Data Privacy – KEMP – June 2024 OECD...
 
The Intersection between Competition and Data Privacy – COLANGELO – June 2024...
The Intersection between Competition and Data Privacy – COLANGELO – June 2024...The Intersection between Competition and Data Privacy – COLANGELO – June 2024...
The Intersection between Competition and Data Privacy – COLANGELO – June 2024...
 
Disaster Management project for holidays homework and other uses
Disaster Management project for holidays homework and other usesDisaster Management project for holidays homework and other uses
Disaster Management project for holidays homework and other uses
 
The Intersection between Competition and Data Privacy – OECD – June 2024 OECD...
The Intersection between Competition and Data Privacy – OECD – June 2024 OECD...The Intersection between Competition and Data Privacy – OECD – June 2024 OECD...
The Intersection between Competition and Data Privacy – OECD – June 2024 OECD...
 
Pro-competitive Industrial Policy – LANE – June 2024 OECD discussion
Pro-competitive Industrial Policy – LANE – June 2024 OECD discussionPro-competitive Industrial Policy – LANE – June 2024 OECD discussion
Pro-competitive Industrial Policy – LANE – June 2024 OECD discussion
 
BRIC_2024_2024-06-06-11:30-haunschild_archival_version.pdf
BRIC_2024_2024-06-06-11:30-haunschild_archival_version.pdfBRIC_2024_2024-06-06-11:30-haunschild_archival_version.pdf
BRIC_2024_2024-06-06-11:30-haunschild_archival_version.pdf
 
Artificial Intelligence, Data and Competition – LIM – June 2024 OECD discussion
Artificial Intelligence, Data and Competition – LIM – June 2024 OECD discussionArtificial Intelligence, Data and Competition – LIM – June 2024 OECD discussion
Artificial Intelligence, Data and Competition – LIM – June 2024 OECD discussion
 
IEEE CIS Webinar Sustainable futures.pdf
IEEE CIS Webinar Sustainable futures.pdfIEEE CIS Webinar Sustainable futures.pdf
IEEE CIS Webinar Sustainable futures.pdf
 
原版制作贝德福特大学毕业证(bedfordhire毕业证)硕士文凭原版一模一样
原版制作贝德福特大学毕业证(bedfordhire毕业证)硕士文凭原版一模一样原版制作贝德福特大学毕业证(bedfordhire毕业证)硕士文凭原版一模一样
原版制作贝德福特大学毕业证(bedfordhire毕业证)硕士文凭原版一模一样
 
Carrer goals.pptx and their importance in real life
Carrer goals.pptx  and their importance in real lifeCarrer goals.pptx  and their importance in real life
Carrer goals.pptx and their importance in real life
 
Using-Presentation-Software-to-the-Fullf.pptx
Using-Presentation-Software-to-the-Fullf.pptxUsing-Presentation-Software-to-the-Fullf.pptx
Using-Presentation-Software-to-the-Fullf.pptx
 
Artificial Intelligence, Data and Competition – OECD – June 2024 OECD discussion
Artificial Intelligence, Data and Competition – OECD – June 2024 OECD discussionArtificial Intelligence, Data and Competition – OECD – June 2024 OECD discussion
Artificial Intelligence, Data and Competition – OECD – June 2024 OECD discussion
 
XP 2024 presentation: A New Look to Leadership
XP 2024 presentation: A New Look to LeadershipXP 2024 presentation: A New Look to Leadership
XP 2024 presentation: A New Look to Leadership
 
Artificial Intelligence, Data and Competition – ČORBA – June 2024 OECD discus...
Artificial Intelligence, Data and Competition – ČORBA – June 2024 OECD discus...Artificial Intelligence, Data and Competition – ČORBA – June 2024 OECD discus...
Artificial Intelligence, Data and Competition – ČORBA – June 2024 OECD discus...
 
The Intersection between Competition and Data Privacy – CAPEL – June 2024 OEC...
The Intersection between Competition and Data Privacy – CAPEL – June 2024 OEC...The Intersection between Competition and Data Privacy – CAPEL – June 2024 OEC...
The Intersection between Competition and Data Privacy – CAPEL – June 2024 OEC...
 
The remarkable life of Sir Mokshagundam Visvesvaraya.pptx
The remarkable life of Sir Mokshagundam Visvesvaraya.pptxThe remarkable life of Sir Mokshagundam Visvesvaraya.pptx
The remarkable life of Sir Mokshagundam Visvesvaraya.pptx
 

SQL Server Blocking Analysis

  • 1. Blocking Analysis Vo Tan Hau July 08 ,2011 MS SQL Server
  • 2. CONTENTS  Overview SQL Server.  Locks And Transaction isolation levels  SQL Server Blocking.  Q&A.
  • 3. 1.Overview SQL Server Relational Database Management System SQL ServerClient Results Client Application OLAP OLTPQuery
  • 4. Client-Server Communication Process Client Application Client Net-Library Client SQL Server Relational Engine Storage Engine Server Local Database Database API (OLE DB, ODBC, DB-Library) Memory Open Data Services Server Net-Libraries Query Result Set Result Set Query 1 2 3 4 5 Tabular Data Stream(TDS) Processor
  • 5. Lock in MS SQL Server. 2. Locks & Transaction Isolation Levels 1 2 Transaction Isolation Levels.
  • 6. 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.
  • 7. 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.
  • 8. 2.1.b Lock Granularity and Hierarchies Resource Description RID A row identifier used to lock a single row within a heap. KEY A 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. HOBT A 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.
  • 9. 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
  • 10. 2.1.c Locks Modes (cont) ID CA CB 1 AA01 100 2 AA02 101 3 AA03 102 4 BB01 200 5 BB02 201 6 BB03 202 7 CC01 301 8 CC02 302 9 CC03 303
  • 11. 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 (03000d8f0 ecc) Begin tran Select ID,CA,CB from dbo.tbl01 with( HOLDLOCK) where ID=3 SELECT resource_type RT, request_mode RM, resource_description RD FROM sys.dm_tran_locks WHERE resource_type <> 'DATABASE' Commit tran
  • 12. 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 RD FROM sys.dm_tran_locks WHERE resource_type <> 'DATABASE' ROLLBACK RT RM RD PAGE IX 1:163 OBJECT IX OBJECT IX KEY X (0500d1d065e9)
  • 13. 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 tran Select ID,CA,CB from dbo.tbl01 WITH (UPDLOCK) where CB >300 SELECT resource_type RT, request_mode RM, resource_description RD FROM sys.dm_tran_locks WHERE resource_type <> 'DATABASE' Commit tran RT RM RD PAGE IU 1:163 KEY U (0a0087c006b1) OBJECT IX
  • 14. 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 tran UPDATE dbo.tbl01 SET CA = 'Test Intent locks (I)' WHERE ID = 5 SELECT resource_type RT, request_mode RM, resource_description RD FROM sys.dm_tran_locks WHERE resource_type <> 'DATABASE' ROLLBACK RT RM RD PAGE IX 1:163 KEY X (0500d1d065e9) OBJECT IX
  • 15. 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 RD FROM sys.dm_tran_locks WHERE resource_type <> 'DATABASE' ROLLBACK RT RM RD HOBT Sch-M METADATA Sch-S data_space_id = 1 OBJECT Sch-M
  • 16. 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 tran Update dbo.tbl01 WITH (UPDLOCK) Set CA = 'Key-Range Locks' where CB >300 SELECT resource_type RT, request_mode RM, resource_description RD FROM 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)
  • 17. 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 tbl03 FROM 'D:Bulk.txt' WITH ( FIELDTERMINATOR = ',', ROWTERMINATOR = 'n' ) GO AA01,101 BB02,202 CC03,303 DD04,404
  • 18. 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.
  • 19. 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 N Shared 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
  • 20. 2.2 Transaction Isolation Levels What is Isolation Levels in SQL Server?a b Types of Isolation Level
  • 21. 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.
  • 22. 2.2.b Types of Isolation Level Traditionally, SQL Server has supported six isolation levels:  Read Uncommitted.  Read Committed.  Repeatable Read.  Serializable Read.  Snapshot.  Read Committed Snapshot
  • 23. 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 A Begin tran UPDATE dbo.tbl01 SET CA = ‘Read Uncommitted' WHERE ID = 5 Commit Tran --Connection B Select ID,CA,CB From dbo.tbl01 WHERE ID = 5 --Change Isolation Level of Connection B SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED Select ID,CA,CB From dbo.tbl01 WHERE ID = 5 Connection B can see data but It not correct. This is call Dirty-Reading.
  • 24. 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 int AS BEGIN TRAN If 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 Return End Else RAISERROR (‘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 CB EXEC UpdateCB ‘AA01’,999 User A made the update and no error message are returned but it has lost its update.
  • 25. 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.
  • 26. 2.2.b Types of Isolation Level (cont) CREATE PROCEDURE dbo.UpdateCB @CA NVarchar(100), @CB int AS SET TRANSACTION ISOLATION LEVEL REPEATABLE READ BEGIN TRAN If 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 Return End Else RAISERROR (‘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.
  • 27. --Create new table tbl02 and Add Column CC into tbl01 CREATE 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>300 Create PROCEDURE dbo.AddColCC AS BEGIN BEGIN TRAN SET TRANSACTION ISOLATION LEVEL REPEATABLE READ INSERT INTO dbo.tbl02(CB, CC) SELECT CB,100 FROM dbo.tbl01 WHERE CC = 0 AND CB > 300 WAITFOR DELAY '00:00:05' UPDATE tbl01 SET CC = 1 WHERE CC = 0 AND CB > 300 COMMIT TRAN END --User A call sp AddColCC exec AddColCC --User B insert data to tbl01 with CB>300 insert into tbl01(CA,CB) Values('Test REPEATABLE READ',304) Step01 Step02 Step03 Step04
  • 28. Result of User A & B tbl01 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.
  • 29. 2.2.b Types of Isolation Level (cont)  Serializable Isolation Level This 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.
  • 30. --Alter sp to insert data tbl01 to tbl02 with condition CB>300 Alter PROCEDURE dbo.AddColCC AS BEGIN SET TRANSACTION ISOLATION LEVEL Serializable BEGIN TRAN INSERT INTO dbo.tbl02(CB, CC) SELECT CB,100 FROM dbo.tbl01 WHERE CC = 0 AND CB > 300 WAITFOR DELAY '00:00:05' UPDATE tbl01 SET CC = 1 WHERE CC = 0 AND CB > 300 COMMIT TRAN END Step01 --Bring tbl01 & tbl02 table to original state Update dbo.tbl01 set CC=0 Delete FROM dbo.tbl01 where ID>10 Delete FROM dbo.tbl02 --User A call sp AddColCC exec AddColCC --User B insert data to tbl01 with CB>300 insert into tbl01(CA,CB) Values('Test REPEATABLE READ',304)Step03 Step04 Step02
  • 31. Result of User A & B tbl01 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
  • 32. 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
  • 33. --Alter sp to insert data tbl01 to tbl02 with condition CB>300 Alter PROCEDURE dbo.AddColCC AS BEGIN SET TRANSACTION ISOLATION LEVEL SNAPSHOT BEGIN TRAN INSERT INTO dbo.tbl02(CB, CC) SELECT CB,100 FROM dbo.tbl01 WHERE CC = 0 AND CB > 300 WAITFOR DELAY '00:00:05' UPDATE tbl01 SET CC = 1 WHERE CC = 0 AND CB > 300 COMMIT TRAN END Step02 --Bring tbl01 & tbl02 table to original state Update dbo.tbl01 set CC=0 Delete FROM dbo.tbl01 where ID>10 Delete FROM dbo.tbl02 --User A call sp AddColCC exec AddColCC --User B insert data to tbl01 with CB>300 insert into tbl01(CA,CB) Values(‘ISOLATION LEVEL SNAPSHOT',305)Step04 Step05 Step03 --Enable Allow_Snapshot_Isolation on local DB Test ALTER DATABASE [DB_Test] SET ALLOW_SNAPSHOT_ISOLATION ON Step01
  • 34. Result of User A & B tbl01 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
  • 35. 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 tbl01 Where CB=303 Update dbo.tbl01 Set CA='SNAPSHOT-2' Where CB=303 4 Commit Commit Example 2
  • 36. No User A User B 2 (1 row(s) affected) Return data with CA='SNAPSHOT‘ 3 Return 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
  • 37. 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
  • 38. Summarize of Isolation Level Dirty Reads Lost Updates Non repeatable reads Phantom reads Conflict Detection 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
  • 39. 3 Blocking in the system Purpose of Blocking. 1 2 What is Blocking in SQL Server? 3 Detecting SQL Server Blocking
  • 40. 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.
  • 41. 3.2 Purpose of Blocking. Dirty Reads Lost Updates Phantom reads Non repeatable reads
  • 42. 3.3 Detecting SQL Server Blocking What is Dead Lock?a b Detection SQL Server Blocking.
  • 43. 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.
  • 44. 3.3.b Detection SQL Server Blocking No User A User B 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 tbl01 Where CB=303 Update dbo.tbl01 Set CA='SNAPSHOT-2' Where CB=303 4 --Commit Commit Profiler Trace Activity Monitor Report Service
  • 45. SQL Server Profiler Trace Enable Blocked process threshold on Database sp_configure 'show advanced options', 1 GO RECONFIGURE GO sp_configure 'blocked process threshold' GO sp_configure 'blocked process threshold', 5 -–5 is time blocked GO RECONFIGURE GO
  • 46. SQL Server Profiler Trace Use 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.
  • 47. SQL Server Profiler Trace (Cont) Expand the ‘Errors and Warnings’ section and select the ‘Blocked Process Report’ Item.
  • 48. 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.
  • 49. 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.
  • 50. SQL Server Activity Monitor (Cont)
  • 51. SQL Server Report Services  On SQL Server we can use SQL Report Service to detect block transaction.
  • 52.
  • 54. THANK YOU FOR YOUR ATTENTION!
  • 55. 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+Perfo rmance+Tuning/Chapter+38.+Locking+and+Perf ormance/  http://aboutsqlserver.com/2011/04/14/locking- in-microsoft-sql-server-part-1-lock-types