choosing a concurrency model, optimistic or pessimistic

22
Locking and Blocking with SQL Server Choosing a concurrency Model – Optimistic or Pessimistic Vinod Kumar M Technology Evangelist - Microsoft www.ExtremeExperts.com http://blogs.sqlxml.org/vinodkumar

Upload: vinod-kumar

Post on 14-Dec-2014

3.124 views

Category:

Education


1 download

DESCRIPTION

PPT used for UG Event on Locking and Blocking with SQL Server

TRANSCRIPT

Page 1: Choosing A Concurrency Model, Optimistic Or Pessimistic

Locking and Blocking with SQL

ServerChoosing a concurrency Model –

Optimistic or Pessimistic

Vinod Kumar MTechnology Evangelist - Microsoftwww.ExtremeExperts.comhttp://blogs.sqlxml.org/vinodkumar

Page 2: Choosing A Concurrency Model, Optimistic Or Pessimistic

Why Study Concurrency?Contention Degrades Performance

Most Tuning Tools Don’t Consider Concurrency Issues

You Can Write Better Applications

Page 3: Choosing A Concurrency Model, Optimistic Or Pessimistic

Concurrency Models

Pessimistic ConcurrencyPreventative approachLimited concurrent accessUses locking to avoid conflicts

Optimistic ConcurrencyAllows full concurrent accessRow versioning allows access to consistent dataConflict detection avoids inconsistent updates

Page 4: Choosing A Concurrency Model, Optimistic Or Pessimistic

Transactions in SQL Server

Transaction Basics

Preventable Phenomena

Page 5: Choosing A Concurrency Model, Optimistic Or Pessimistic

Transaction BasicsAllow Correctness of Operations To Be Verified

Exhibit Four Properties (ACID):AtomicityConsistencyIsolationDurability

Page 6: Choosing A Concurrency Model, Optimistic Or Pessimistic

Preventable Phenomena

Lost Updates

Dirty Reads

Non-Repeatable Reads

Phantoms

Page 7: Choosing A Concurrency Model, Optimistic Or Pessimistic

T2

T1

TimeX=10

Read X (10)

Read X (10)

Compute X=X+10(20)

Compute X=X+15(25)

X=20

Write X

X=25X=25

Write X

Lost UpdateClassic unsynchronized update

Page 8: Choosing A Concurrency Model, Optimistic Or Pessimistic

ComputeX=X+15

(25)

T2

T1

TimeX=10

Read X (25)

Read X (10)

X=25

Write X

X=10

Rollback

Use value ofX that was nevercommitted to DB

Dirty ReadT2 sees effects of T1 that were never committed

Page 9: Choosing A Concurrency Model, Optimistic Or Pessimistic

T2

T1

TimeX=10

Read X (10)

Read X (10)

Compute X=X+15(25)

X=25

Write X

Commit

Read X (25)

Non-Repeatable ReadTransaction sees only committed data but data changes when referenced multiple times

Page 10: Choosing A Concurrency Model, Optimistic Or Pessimistic

T2

T1

TimeSmith,4

Brewer,7

Select count (*) where rank > 3

(2)

Insert

Jones,6

Jones,6Smith,4

Brewer,7

Select count (*) where rank > 3

(3)

The “Phantom” ProblemTransaction’s input set defined by a predicate

Transaction should not see additions/deletions from input setMust prevent other transactions from inserting even if we don’t see the effects!

Page 11: Choosing A Concurrency Model, Optimistic Or Pessimistic

Isolation LevelsTrue Isolation Is Expensive Trade off between correctness and concurrency

ANSI SQL Defines Four Isolation Levels based on which phenomena are allowedNo specification as to how to achieve isolation

SQL Server 2000 implements all ANSI isolation levels using Pessimistic Concurrency ModelSQL Server 2005 provides Optimistic Concurrency Alternatives

Optimistic implementation of read committedNew Snapshot Isolation

Page 12: Choosing A Concurrency Model, Optimistic Or Pessimistic

SQL 2008 Isolation Levels

Isolation Levels

Dirty Read

Non-Repeatabl

e ReadPhantom

sUpdate Conflict

Concurrency Model

READ UNCOMMITTED Yes Yes Yes No

READ COMMITTED1 Locking2 Snapshot

NoNo

YesYes

YesYes

NoNo

PessimisticOptimistic

REPEATABLE READ No No Yes No Pessimistic

SNAPSHOT No No No Yes Optimistic

SERIALIZABLE No No No No Pessimistic

Phenomena Allowed

SET TRANSACTION ISOLATION LEVEL …

Page 13: Choosing A Concurrency Model, Optimistic Or Pessimistic

Locking Decision

Isolationlevel

Scan type(Range,

Table, Probe)

# of rowsin scan

# of rows/page

Locking strategy(Table, Page, Row)

Operation type (scan, update)

Current Activity

Lock hintssp_IndexOption

Settings

Page 14: Choosing A Concurrency Model, Optimistic Or Pessimistic

Pessimistic Concurrency Control

Aspects of Locking

Blocking

Controlling Locking

Page 15: Choosing A Concurrency Model, Optimistic Or Pessimistic

Aspects of LockingType of Lock

Duration of Lock

Granularity of Lock

Page 16: Choosing A Concurrency Model, Optimistic Or Pessimistic

BlockingOccurs when one process requests a lock on the same resource held by another process in an incompatible mode

Blocking and Isolation Summary for Pessimistic Concurrency

Writers block writers in all levelsWriters block readers in Read Committed and higherReaders block writers in Repeatable Read and higher

Page 17: Choosing A Concurrency Model, Optimistic Or Pessimistic

DeadlockWhat is Deadlock?

Handling Deadlock

Page 18: Choosing A Concurrency Model, Optimistic Or Pessimistic

What is Deadlock?

Two Processes Mutually Blocking Each Other

Resource A

Process 1

Process 2

Resource B

X

X

Page 19: Choosing A Concurrency Model, Optimistic Or Pessimistic

Blocked Process Threshold

Generates an event when a task(s) blocked for greater than ‘threshold’‘Threshold’ is a configurable optionThere is a background thread (Lock Monitor) that periodically checks tasks that are blocked.You can use event to identify tasks

Page 20: Choosing A Concurrency Model, Optimistic Or Pessimistic

Demo: Blocked Process Threshold

Page 21: Choosing A Concurrency Model, Optimistic Or Pessimistic

SummaryConcurrency Models

Transactions in SQL Server

Isolation Levels

Pessimistic Concurrency Control

Optimistic Concurrency Control

Page 22: Choosing A Concurrency Model, Optimistic Or Pessimistic

Additional Resources

Inside SQL Server 2005: The Storage Engine(Microsoft Press, available now)

Blogs:http://blogs.msdn.com/sqlserverstorageengine http://blogs.msdn.com/sqltips http://blogs.msdn.com/sqlcat http://sqlblog.com/blogs/kalen_delaney

Scripts available at www.InsideSQLServer.com/conferences http://sqlblog.com/blogs/kalen_delaney