I am sure you all know that troubleshooting problems related to locking and blocking (hey, sometimes there are deadlocks too) can be a nightmare! I also know that you struggle to find some time to go a bit deeper and learn what the various isolation levels can do for you. In this one-hour session, you will be able to see and understand why locking happens, how the locking manager actually works and how can we resolve various locking problems with different techniques ranging from query hints to changing the isolation levels.
5. Methods of Concurrency Control
Two main concurrency control types:
1. PessimisYc
2. OpYmisYc
– SQL Server uses locks, causes blocks and who said deadlocks?
– SQL Server generates versions for everyone, but the updates…
6. What Are Locks and what is locking?
Lock – internal memory structure that “tells” us what we all do with the resources
inside the system
Locking – mechanism to protect the resources and guarantee consistent data
7. Common lock types
Intent
Used
for:
Preven&ng
incompa&ble
locks
Dura0on:
End
of
the
transac&on
Shared
(S)
Used
for:
Reading
Dura0on:
Released
almost
immediately
(depends
on
the
isola&on
level)
Update
(U)
Used
for:
Preparing
to
modify
Dura0on:
End
of
the
transac&on
or
un&l
converted
to
exclusive
(X)
Exclusive
(X)
Used
for:
Modifying
Dura0on:
End
of
the
transac&on
8. Lock CompaYbility
Not all locks are compaYble with other locks.
Lock
Shared
Update
Exclusive
Shared (S)
P
P
X
Update (U)
P
X
X
Exclusive (X)
X
X
X
10. Let’s update a row. What do we need?
USE AdventureWorks2012
GO
UPDATE [Person].[Address]
SET AddressLine1='Microsoi, Bulgaria'
WHERE AddressID=2
S
IX
Header
Row
Row
Row
Row
Row
IX
X
A query! J
11. Methods to View Locking InformaYon
Dynamic
Management
Views
SQL Server Profiler
or Extended
Events
Performance
monitor or AcYvity
Monitor
13. Locking and blocking
Locking and blocking are oien confused!
Locking
• The acYon of taking and potenYally holding locks
• Used to implement concurrency control
Blocking is result of locking!
• One process needs to wait for another process to release locked resources
• In a mulYuser environment, there is always, always blocking!
• Only a problem if it lasts too long
14. SQL Server decides (during compilaYon) the granularity of locks to be used:
• Row
• Page
Lock granularity can be controlled via LOCK HINTS (PAGLOCK, TABLOCKX, etc…)
EscalaYon always happens this way:
Row -‐> table lock (or parYYon lock if possible)
Page -‐> table lock (or parYYon lock if possible)
Lock escalaYon can be disabled:
• Trace flag 1211 – disables lock escalaYon on server level
• Trace flag 1224 – disables lock escalaYon on server level unYl 40% of the memory used is
consumed
Lock granularity and escalaYon
16. Switch the escalaYon level (per table)
AUTO – ParYYon-‐level escalaYon if the table is parYYoned
TABLE – Always table-‐level escalaYon
DISABLE – Do not escalate unYl absolutely necessary
Controlling Lock escalaYon
SELECT lock_escalaYon_desc
FROM sys.tables
WHERE name = 'Person.Address'
ALTER TABLE Person.Address SET (LOCK_ESCALATION = {AUTO | TABLE | DISABLE}
17. What Are Deadlocks?
Task A
Task B
Resource 1
Resource 2
Who is vicYm?
• Cost for Rollback
• Deadlock priority – SET DEADLOCK_PRIOIRTY
18. Resolve blocking a.k.a live locking
How to resolve blocking problems:
1. Keep the transacYons as short as possible
2. No user interacYons required in the middle of the
transacYon
3. Use indexes
4. Consider a server to offload some of the workloads
5. Choose isolaYon level
19. DEMO
Monitor for locks with xEvents
Lock escalaYon – both to table and parYYon
Deadlock and the SET DEADLOCK_PRIORITY opYon
21. SET
TRANSACTION
ISOLATION
LEVEL
READ
UNCOMMITTED
(NOLOCK?)
Transaction 1
Transaction 2
Sugges0on:
BePer
offload
the
reads
or
go
with
op&mis&c
level
concurrency!
Select
Update
eXclusive lock
Read Uncommixed
(pessimisYc concurrency control)
Dirty read
22. SET
TRANSACTION
ISOLATION
LEVEL
REPEATABLE
READ
Transaction 1 S(hared) lock
select
No
non-‐repeatable
reads
possible
(updates
during
Transac&on
1)
Phantom
records
s&ll
possible
(inserts
during
Transac&on
1)
Update
Transaction 2
Repeatable Read
(pessimisYc concurrency control)
23. Transaction 1 S(hared) lock
select
Even
phantom
records
are
not
possible!
Highest
pessimis&c
level
of
isola&on,
lowest
level
of
concurrency
Insert
Transaction 2
Serializable
(pessimisYc concurrency control)
SET
TRANSACTION
ISOLATION
LEVEL
SERIALIZABLE
24. Based
on
Row
versioning
(stored
inside
tempdb’s
version
store
area)
◦ No
dirty,
non-‐repeatable
reads
or
phantom
records
◦ Every
single
modifica&on
is
versioned
even
if
not
used
◦ Adds
14
bytes
per
row
Readers
do
not
block
writers
and
writers
do
not
block
readers
Writers
can
and
will
block
writers,
this
can
cause
conflicts
OpYmisYc Concurrency
25. RCSI
–
Read
CommiHed
Snapshot
Isola0on
Level
◦ Statement
level
versioning
◦ Requires
ALTER
DATABASE
SET
READ_COMMITTED_SNAPSHOT
ON
Snapshot
Isola0on
Level
◦ Transac&on
level
versioning
◦ Requires
ALTER
DATABASE
SET
ALLOW_SNAPSHOT_ISOLATION
ON
◦ Requires
SET
TRANSACTION
ISOLATION
LEVEL
SNAPSHOT
RCSI and SI
(opYmisYc concurrency control)
27. Summary
Blocking
is
something
normal
when
it’s
not
for
long
There
are
hundreds
of
thousands
of
ways
to
monitor
locking
and
blocking
Be
extremely
careful
for
lock
escala&ons
Choosing
the
Isola&on
level
is
also
a
business
decision!
28. Resources
MCM Readiness videos on locking lecture and demo
MCM Readiness video on Snapshot IsolaYon Level
hxp://blogs.msdn.com/b/bartd/archive/tags/sql+locking/
hxp://www.sqlskills.com/blogs/paul/category/locking/
Lock hints -‐
hxp://www.techrepublic.com/arYcle/control-‐sql-‐server-‐
locking-‐with-‐hints/5181472