Welcome to the nightmare of locking, blocking and isolation levels!
-
Upload
boris-hristov -
Category
Technology
-
view
111 -
download
7
description
Transcript of Welcome to the nightmare of locking, blocking and isolation levels!
Българска SQL & BI потребителска група
http://bgsqlgroup.com SQL & BI User Group Bulgaria on FB
WELCOME TO THE NIGHTMARE OF
LOCKING, BLOCKING
AND ISOLATION LEVELS!
Magi NaumovaBoris Hristov
BG SQL User Group Meeting, 4/4/2013
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
About Bobi SQL Server DBA at HP
Trainer at New Bulgarian University and FMI
MCT & MCITP DBA SQL Server 2008
www.borishristov.com
Agenda…
Transactions. What are they?Locks. What is there for us?Troubleshooting locking problemsTransaction Isolation Levels
Transactions. What are they?
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
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
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
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;
Locks. What is there for us?
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…
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
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
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
Lock Hierarchy
Database
Table
Page
Row
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!
Methods to View Locking Information
Dynamic Management
Views
SQL Server Profiler or Extended
Events
Performance monitor or
Activity Monitor
Troubleshooting locking problems
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
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
Lock escalation S
S
X
>= 5000
IX
X
X
X
X
IX
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)
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
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
What Are Deadlocks?
Task A
Task B
Resource 1
Resource 2
Who is victim?
• Cost for Rollback
• Deadlock priority – SET DEADLOCK_PRIOIRTY
DEMO Capturing locking information
Who is blocking who
Lock escalation – both to table and partition
Deadlock and the SET DEADLOCK_PRIORITY option
Transaction isolation levels
Transaction Isolation Levels control when locks are taken and how long they are held
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
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
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
DEMO Pessimistic 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
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
DEMO Optimistic Concurrency 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
DEMO Versioning details and managing version store
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
ResourcesMCM Readiness videos on locking lecture and demo
MCM Readiness video on Snapshot Isolation Level
http://blogs.msdn.com/b/bartd/archive/tags/sql+locking/
http://www.sqlskills.com/blogs/paul/category/locking/
Lock hints - http://www.techrepublic.com/article/control-sql-server-locking-with-hints/5181472
Thank you.
• www.maginaumova.com
• www.borishristov.com ?