29
#sqlsat257 #sqlsatverona November 9 th , 2013 Welcome to the nightmare of locking, blocking and isolation levels!

The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

Embed Size (px)

DESCRIPTION

I am sure you all know that troubleshooting problems related to locking and blocking (hey, sometimes there are deadlocks too) can be a real nightmare! In this session, you will be able to see and understand why and how locking actually works, what problems it causes and how can we use isolation levels and various other techniques to resolve them!

Citation preview

Page 1: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

Welcome to the nightmare of locking, blocking and isolation levels!

Page 2: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

So who am I?

@BorisHristov

Page 3: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

Agenda…

Locks. What is there for us?

Troubleshooting locking problems

Transaction Isolation Levels

Page 4: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

Locks. What is there for us?

Page 5: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

Methods of Concurrency Control 1. Pessimistic

– SQL Server uses locks, causes blocks and who said deadlocks?

2. Optimistic – SQL Server generates versions for everyone, but the updates…

Page 6: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

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

Page 7: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

Common lock types

Intent

Used for: Preventing incompatible locksDuration: End of the transaction

Shared (S)

Used for: ReadingDuration: Released almost immediately

(depends on the isolation level)

Update (U)

Used for: Preparing to modifyDuration: End of the transaction or until

converted to exclusive (X)

Exclusive (X)

Used for: ModifyingDuration: End of the transaction

Page 8: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

Lock Compatibility

Not all locks are compatible with other locks.

Lock Shared Update Exclusive

Shared (S) XUpdate (U) X X

Exclusive (X) X X X

Page 9: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

Lock Hierarchy

Database

Table

Page

Row

Page 10: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

Let’s update a row! What do we need?

USE AdventureWorks2012GOUPDATE [Person].[Address]SET AddressLine1=’Verona,Italy' WHERE AddressID=2

S

IX

HeaderRow

Row

Row

Row

Row

IX

X

A query!

Page 11: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

Methods to View Locking Information

Dynamic Management

Views

SQL Server Profiler or Extended

Events

Performance monitor or Activity

Monitor

Page 12: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

Troubleshooting locking problems

Page 13: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

Locking and blocking

Locking and blocking are often confused!

Locking• The action of taking and potentially 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 multiuser environment, there is always, always blocking!• Only a problem if it lasts too long

Page 14: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

SQL Server decides (during compilation) the granularity of locks to be used:• Row• Page

Lock granularity can be controlled via LOCK HINTS (PAGLOCK, TABLOCKX…)

Escalation always happens this way:

Row -> table lock (or partition lock if possible)

Page -> table lock (or partition lock if possible)

Lock escalation can be disabled:• Trace flag 1211 – disables lock escalation on server level• Trace flag 1224 – disables lock escalation on server level until 40% of the

memory used is consumed

Lock granularity and escalation

Page 15: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

Lock escalation S

S

X

>= 5000

IX

Header

Row

Row

Row

Row

Row

X

X

X

IX

X

Page 16: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

Switch the escalation level (per table)

AUTO – Partition-level escalation if the table is partitionedTABLE – Always table-level escalationDISABLE – Do not escalate until absolutely necessary

Controlling Lock escalation

SELECT lock_escalation_descFROM sys.tablesWHERE name = 'Person.Address'

ALTER TABLE Person.Address SET (LOCK_ESCALATION = {AUTO | TABLE | DISABLE}

Page 17: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

What Are Deadlocks?

Task A

Task B

Resource 1

Resource 2

Who is victim?

• Cost for Rollback

• Deadlock priority – SET DEADLOCK_PRIOIRTY

Page 18: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

Resolve blocking a.k.a live locking

1. Keep the transactions as short as possible

2. No user interactions required in the middle of the transaction

3. Use indexes (proper ones)

4. Consider a server to offload some of the workloads

5. Choose isolation level

Page 19: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

DEMO Monitor for locks with xEvents

Lock escalation – both to table and partition

Deadlock and the SET DEADLOCK_PRIORITY option

Page 20: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

Transaction isolation levels

Page 21: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED (NOLOCK?)

Transaction 1

Transaction 2

Suggestion: Better offload the reads or go with optimistic level concurrency!

Select

Update

eXclusive lock

Read Uncommitted(pessimistic concurrency control)

Dirty read

Page 22: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

SET TRANSACTION ISOLATION LEVEL REPEATABLE READ

Transaction 1 S(hared) lock

select

No non-repeatable reads possible (updates during Transaction 1)

Phantom records still possible (inserts during Transaction 1)

Update

Transaction 2

Repeatable Read(pessimistic concurrency control)

Page 23: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

Transaction 1 S(hared) lock

select

Even phantom records are not possible!

Highest pessimistic level of isolation, lowest level of concurrency

Insert

Transaction 2

Serializable(pessimistic concurrency control)

SET TRANSACTION ISOLATION LEVEL SERIALIZABLE

Page 24: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

Based on Row versioning (stored inside tempdb’s version store area)

• No dirty, non-repeatable reads or phantom records

• Every single modification 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

Optimistic Concurrency

Page 25: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

RCSI – Read Committed Snapshot Isolation Level

• Statement level versioning

• Requires ALTER DATABASE SET READ_COMMITTED_SNAPSHOT ON

Snapshot Isolation Level

• Transaction level versioning

• Requires ALTER DATABASE SET ALLOW_SNAPSHOT_ISOLATION ON

• Requires SET TRANSACTION ISOLATION LEVEL SNAPSHOT

RCSI and SI(optimistic concurrency control)

V1 V2Transaction 1

Transaction 2

Select in RCSISelect

Select in SI

Page 26: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

DEMO Playing around with the Isolation levels

Page 27: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

Summary

1. Blocking is something normal when it’s not for long

2. There are numerous of ways to monitor locking and blocking

3. Be extremely careful for lock escalations

4. Choosing the Isolation level is also a business decision!

Page 29: The nightmare of locking, blocking and deadlocking. SQLSaturday #257, Verona

#sqlsat257#sqlsatveronaNovember 9th, 2013

Thank you.