SqlSaturday199 - Deadlocks

Preview:

DESCRIPTION

"Deadlock" is a terrible word, isn't it? Is it as scary as it sounds? Why do they occur and how can they affect an application? Significantly important question is how to solve "Deadlock" issues? The answers to these questions can be found in my session, which is completely dedicated to the fundamental principles of locking and isolation levels.

Citation preview

Deadlock detected! All is lost or it's too early to sound the alarm?Denis Reznik

Sponsors

About me

Denis Reznik Kiev, Ukraine Database Architect at The Frayman Group Microsoft MVP Community enthusiast

3 |

Agenda

Locks Lock Types Transaction Isolation Levels

Deadlocks Classic Deadlocks Not obvious deadlocks Deadlock detecting and analyzing

Lock Types- Shared

S S

X

Lock Types - Exclusive

X

XS

Lock Types - Update

U

US

SX

Lock Types – Intent locks

S

ISIS

READ UNCOMMITTED

The less restricted Isolation Level Allow all collisions, which READ

COMMITTED allow Allow Dirty Reads Doesn’t set Shared locks on read operations

DemoREAD UNCOMMITTED

DIRTY READS

ID City

1 Kiev

2 Sofia

3 Sofia

4 Sofia

5 Moscow

6 New York

7 New York

SELECT * FROM UsersWHERE City = 'Kiev'

BEGIN TRANUPDATE Users SET City = 'Sofia'WHERE City = 'Kiev'

ID City

1 Sofia

2 Sofia

3 Sofia

4 Sofia

5 Moscow

6 New York

7 New York

SELECT * FROM UsersWHERE City = 'Kiev'0 Records

ROLLBACK

SELECT * FROM UsersWHERE City = 'Kiev'

X

READ COMMITTED

Default Isolation Level Doesn’t allow Dirty Reads Shared locks released after the read

NO DIRTY READS

ID City

1 Kiev

2 Sofia

3 Sofia

4 Sofia

5 Moscow

6 New York

7 New York

SELECT * FROM UsersWHERE City = 'Kiev'

BEGIN TRANUPDATE Users SET City = 'Sofia'WHERE City = 'Kiev'

ID City

1 Sofia

2 Sofia

3 Sofia

4 Sofia

5 Moscow

6 New York

7 New York

SELECT * FROM UsersWHERE City = 'Kiev'Wait for

Shared lock on the row

X S

DemoREAD COMMITTED

NON-REPEATABLE READS

ID City

1 Kiev

2 Sofia

3 Sofia

4 Sofia

5 Moscow

6 New York

7 New York

BEGIN TRAN

SELECT * FROM UsersWHERE City = 'Sofia'UPDATE Users

SET City = 'Kiev'WHERE Id = 2

ID City

1 Kiev

2 Kiev

3 Sofia

4 Sofia

5 Moscow

6 New York

7 New York

SELECT * FROM UsersWHERE City = 'Sofia'

X

S

S...

S

REPEATABLE READ

More restricted than READ COMMITTED Doesn’t allow Dirty Reads Doesn’t allow Non-Repeatable reads Shared locks are hold to the end of the

transaction

NO NON-REPEATABLE READS

ID City

1 Kiev

2 Sofia

3 Sofia

4 Sofia

5 Moscow

6 New York

7 New York

BEGIN TRAN

SELECT * FROM UsersWHERE City = 'Sofia'UPDATE Users

SET City = ‘Kiev'WHERE Id = 2

ID City

1 Kiev

2 Kiev

3 Sofia

4 Sofia

5 Moscow

6 New York

7 New York

SELECT * FROM UsersWHERE City = 'Sofia'

X

S

S...

S

COMMIT

DemoREPEATABLE READ

PHANTOM RECORDS

ID City

1 Kiev

2 Sofia

3 Sofia

4 Sofia

5 Moscow

6 New York

7 New York

BEGIN TRAN

SELECT * FROM UsersWHERE City = 'Sofia'UPDATE Users

SET City = 'Sofia'WHERE Id = 'Kiev'

ID City

1 Sofia

2 Sofia

3 Sofia

4 Sofia

5 Moscow

6 New York

7 New York

SELECT * FROM UsersWHERE City = 'Sofia'

X

S

S...

S

S

SERIALIZABLE

The most restricted Isolation Level Doesn’t allow Dirty Reads Doesn’t allow Non-Repeatable reads Doesn’t allow Phantom Records Lock range of keys

NO PHANTOM RECORDS

ID City

1 Kiev

2 Sofia

3 Sofia

4 Sofia

5 Moscow

6 New York

7 New York

BEGIN TRAN

SELECT * FROM UsersWHERE City = 'Sofia'UPDATE Users

SET City = 'Sofia'WHERE Id = 'Kiev'

ID City

1 Sofia

2 Sofia

3 Sofia

4 Sofia

5 Moscow

6 New York

7 New York

SELECT * FROM UsersWHERE City = 'Sofia'

X

RA

NG

E S

-S

...

COMMIT

DemoSERIALIZABLE

READ COMMITTED SNAPSHOT

Optimistic concurrency for reads Use row-version store in tempdb No shared locks on reads The same collisions as in READ

COMMITTED

READ COMMITTED SNAPSHOT

ID City

1 Kiev

2 Sofia

3 Sofia

4 Sofia

5 Moscow

6 New York

7 New York

BEGIN TRANUPDATE Users SET City = 'Sofia'WHERE City = 'Kiev'

SELECT * FROM UsersWHERE City = 'Kiev'

ID City

1 Sofia

2 Sofia

3 Sofia

4 Sofia

5 Moscow

6 New York

7 New York

SELECT * FROM UsersWHERE City = 'Kiev'

X

tempdb

ID City

1 Kiev

Version Store

DemoSNAPSHOT READ COMMITTED

SNAPSHOT

Optimistic concurrency for reads Use row-version store in tempdb No shared locks on reads Doesn’t allow Dirty Reads Doesn’t allow Non-Repeatable reads Doesn’t allow Phantom Records Update conflict detection

DemoSNAPSHOT

How to avoid?

Design database so, that it will be no possibility for a deadlock occur

Modify tables in the same order Choose appropriate Transaction Isolation

Level and check possibility of a deadlock in your Isolation Level

There is no unsolvable deadlocks.. But there can be solution, which will not suit you completely

Sponsors

Thank you!

Denis Reznik Twitter: @denisreznik Email: denisreznik@live.ru Blog (in russian): http://reznik.uneta.com.ua Facebook: https://www.facebook.com/denis.reznik.5

LinkedIn: http://ua.linkedin.com/pub/denis-reznik/3/502/234