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

40
Българска SQL & BI потребителска група http://bgsqlgroup.com SQL & BI User Group Bulgaria on FB

description

There will always be locking inside your SQL Server box! In this session we go deep into how locking mechanism works, what are the main problems around locking, how we can resolve them and when isolation levels can actually be of help!

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

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

Българска SQL & BI потребителска група

http://bgsqlgroup.com SQL & BI User Group Bulgaria on FB

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

WELCOME TO THE NIGHTMARE OF

LOCKING, BLOCKING

AND ISOLATION LEVELS!

Magi NaumovaBoris Hristov

BG SQL User Group Meeting, 4/4/2013

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

About Magi

www.SQLMasterAcademy.com

Working with SQL Server from v6.5

MCT from 1998

SQL Server Trainer and Consultant for more than 15 years, having more over 100 projects in Bulgaria, Finland, Germany, UK, Greece..

5 years Senior Consultant in Microsoft

Microsoft Certified Master SQL Server 2008

MVP SQL Server

SolidQ Mentor & Trainer

Now I am running SQL Master Academy training program

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

About Bobi SQL Server DBA at HP

Trainer at New Bulgarian University and FMI

MCT & MCITP DBA SQL Server 2008

[email protected]

www.borishristov.com

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

Agenda…

Transactions. What are they?Locks. What is there for us?Troubleshooting locking problemsTransaction Isolation Levels

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

Transactions. What are they?

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

What Are Transactions?

A transaction is an Atomic unit of work

A transaction leaves data in a Consistent state

A transaction is Isolated from other concurrent transactions

A transaction is Durable

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

Auto Commit TransactionsDefault transaction mode

Every TSQL statement is committed or rolled back on completion

Compile errors result in entire batch not being executed

Run time errors may allow part of the batch to commit

-- compile errorUSE AdventureWorks2012;GOCREATE TABLE TestBatch (Cola INT PRIMARY KEY, Colb CHAR(3));GOINSERT INTO TestBatch VALUES (1, 'aaa');INSERT INTO TestBatch VALUES (2, 'bbb');INSERT INTO TestBatch VALUSE (3, 'ccc'); --- error, error!GOSELECT * FROM TestBatch; -- Returns no rows.GO

---run time error - partially executed USE AdventureWorks2012;GOCREATE TABLE TestBatch (Cola INT PRIMARY KEY, Colb CHAR(3));GOINSERT INTO TestBatch VALUES (1, 'aaa');INSERT INTO TestBatch VALUES (2, 'bbb');INSERT INTO TestBatch VALUES (1, 'ccc'); -- Duplicate key error.GOSELECT * FROM TestBatch; -- Returns rows 1 and 2.GO

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

SQL Server is responsible for opening the transaction

We are responsible for committing or rolling it back

Can be turned on from Connections tab in Server Properties

Implicit Transactions

SET IMPLICIT_TRANSACTIONS ON USE AdventureWorks2012GOUPDATE [Person].[Address]SET AddressLine1='Microsoft, Bulgaria' WHERE AddressID=2COMMIT – this will write a change to the db

SET IMPLICIT_TRANSACTIONS ON USE AdventureWorks2012GOUPDATE [Person].[Address]SET AddressLine1='Microsoft, Bulgaria' WHERE AddressID=2ROLLBACK – this will not write a change to the db

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

Explicit TransactionsA transaction in which start and end of transaction is explicitly declared

BEGIN TRANSACTION

COMMIT TRANSACTION OR ROLLBACK TRANSACTION

XACT_ABORT ON/OFF – control the rollback behavior

SET XACT_ABORT ON – if run time error is generated everything is rolled backUSE AdventureWorks2012GOBEGIN TRANSACTION FundsTransferGO

EXEC HumanResources.DebitAccount '100', 'account1';EXEC HumanResources.CreditAccount '100', 'account2';

COMMIT TRANSACTION;

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

Locks. What is there for us?

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

Methods of Concurrency Control

Two main concurrency control types:

1. Pessimistic

2. Optimistic

Two main concurrency control types:

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

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

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

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 14: Welcome to the nightmare of locking, blocking and isolation levels!

Shared (S)Used for: Reading

Duration: Released almost immediately(depends on the isolation level)

Update (U)Used for: Preparing to modify

Duration: End of the transaction or until converted to exclusive (X)

Exclusive (X)Used for: Modifying

Duration: End of the transaction

Common lock types

Intent

Used for: Preventing incompatible locks

Duration: End of the transaction

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

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 16: Welcome to the nightmare of locking, blocking and isolation levels!

Lock Hierarchy

Database

Table

Page

Row

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

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

USE AdventureWorks2012GOUPDATE [Person].[Address]SET AddressLine1='Microsoft, Bulgaria' WHERE AddressID=2

S

IX

Header

Row

Row

Row

Row

Row

IX

X

A query!

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

Methods to View Locking Information

Dynamic Management

Views

SQL Server Profiler or Extended

Events

Performance monitor or

Activity Monitor

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

Troubleshooting locking problems

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

Locking and blockingLocking 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 21: Welcome to the nightmare of locking, blocking and isolation levels!

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

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

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 22: Welcome to the nightmare of locking, blocking and isolation levels!

Lock escalation S

S

X

>= 5000

IX

X

X

X

X

IX

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

Switch the escalation level (per table)

AUTO – Partition-level escalation if the table is partitioned

TABLE – Always table-level escalation

DISABLE – 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 24: Welcome to the nightmare of locking, blocking and isolation levels!

Lock Duration

SET LOCK_TIMEOUT specifies number of milliseconds to wait

Default LOCK_TIMEOUT is -1 = wait indefinitely

Session level option

When timeout expires, error 1222 is returned

Not a good idea to be used! Does not resolve blocking problems!

SET LOCK_TIMEOUT 5000

BEGIN TRAN

UPDATE Production.ProductInventorySET Quantity = 500WHERE ProductID = 1;-- what happens if the update times out?

DELETE FROM Production.ProductModelWHERE Name LIKE 'Mountain%';

COMMIT

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

Blocking a.k.a live lockingBlocking occurs because of locked resources

First incompatible lock request waits

All other locks requests (even if compatible) WAIT

How to resolve blocking problems:1. Keep the transactions as short as possible2. No user interactions required in the middle of the transaction3. Reduce row by row operations (cursors)4. Use indexes5. Consider a server to offload some of the workloads5. Choose isolation level

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

What Are Deadlocks?

Task A

Task B

Resource 1

Resource 2

Who is victim?

• Cost for Rollback

• Deadlock priority – SET DEADLOCK_PRIOIRTY

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

DEMO Capturing locking information

Who is blocking who

Lock escalation – both to table and partition

Deadlock and the SET DEADLOCK_PRIORITY option

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

Transaction isolation levels

Transaction Isolation Levels control when locks are taken and how long they are held

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

Pessimistic Isolation Levels Changing Isolation level

◦ SET TRANSACTION ISOLATION LEVEL ….. on session level

Transaction 1

Transaction 2

Lowering the default Read Committed

◦ NOLOCK hint – set on the table level

◦ SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

You have Dirty Reads in this isolation level! Saying you want less blocking at the cost of inconsistent data!

If you need to use it a lot, that means, you need a reporting database (or optimistic locking)

Read Uncommitted

Select Update

eXclusive lock

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

Pessimistic Isolation Levels Increasing the default level to Repeatable Read

Repeatable ReadTransaction 1 S(hared) lock

select

S lock is held until the end of the transaction in order to read the same data

Every transaction that has to update rows has to wait until T1 completes

Inserts (phantoms) performed by another transaction can still occur in the table

Update

Transaction 2

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

Pessimistic Isolation Levels Increasing to Serializable

SerializableTransaction 1 S(hared) lock

select

Repeatable Read + Holding the locks for a zone of rows

If there is no index (or not used) the whole tale will be locked

Highest level of isolation, lowest level of concurrency

Be careful with using Frameworks (EF, LYNC)

Insert

Transaction 2

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

DEMO Pessimistic Isolation Levels

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

Optimistic Concurrency Based on Row versioning

◦ When updating a row, the previous version is stored in the version store

◦ The new row contains a pointer to the old row in the version store

Readers do not block writers and writers do not block readers.

BUT writers can and will block writers, this can cause conflicts.

Adds 14bytes to every row

Needs Version Store i.e. TempDB space

V1 V2Transaction 1

Transaction 2

Select

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

Implementation – RCSI and SI RCSI

◦ Statement-Level versioning, i.e. any query sees the last version of data as of the beginning of the statement◦ Requires ALTER ATABASE SET READ_COMMITTED_SNAPSHOT ON

Snapshot Isolation Level◦ Session Level versioning, i.e. the most recent committed version of the rows as of the beginning of the

transaction◦ Requires ALTER DATABASE SET ALLOW_SNAPSHOT_ISOLATION ON◦ Requires SET TRANSACTION ISOLATION LEVEL SNAPSHOT◦ If used then better together with RCSI! Else, you have drawbacks of versioning only!

V1 V2Transaction 1

Transaction 2

Select in RCSISelect

Select in SI

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

DEMO Optimistic Concurrency Isolation Levels

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

Managing Version store The Version Store

◦ SQL Server starts generating versions in tempdb as soon as a database is enabled for one of the snapshot-based isolation levels

◦ Maintains link lists of rows, the end of the list is the oldest version of that particular row

Managing Version Store◦ SQL Server maintains a clean-up thread

◦ If file cannot grow, a snapshot query fails (tempdb growth errors)

◦ Monitoring counters◦ Free Space in tempdb

◦ Version Store Size

◦ Version Generation Rate and Version Cleanup Rate

◦ Longest Transaction Running Time

◦ Snapshot Transactions

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

DEMO Versioning details and managing version store

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

Summary If you experience blocking consider implementing Optimistic Concurrency

Consider RCSI only, it is way cheeper

RCSI + SI only if needed higher isolation level per transaction (RR in pesimistic)

If you have to use often no blocking hints the consider either reporting DB or optimistic concurrency

Be careful with READPAST, NOLOCK, ….LOCK TIMEOUT and any other work arounds

If you keep default concurrency, then follow the recommendations for minimizing blocking and lock escalations

Be careful with hidden hints/isolation levels changes using EF, LYNC, etc