choosing a concurrency model, optimistic or pessimistic

Post on 14-Dec-2014

3.124 Views

Category:

Education

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

Locking and Blocking with SQL

ServerChoosing a concurrency Model –

Optimistic or Pessimistic

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

Why Study Concurrency?Contention Degrades Performance

Most Tuning Tools Don’t Consider Concurrency Issues

You Can Write Better Applications

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

Transactions in SQL Server

Transaction Basics

Preventable Phenomena

Transaction BasicsAllow Correctness of Operations To Be Verified

Exhibit Four Properties (ACID):AtomicityConsistencyIsolationDurability

Preventable Phenomena

Lost Updates

Dirty Reads

Non-Repeatable Reads

Phantoms

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

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

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

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!

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

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 …

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

Pessimistic Concurrency Control

Aspects of Locking

Blocking

Controlling Locking

Aspects of LockingType of Lock

Duration of Lock

Granularity of Lock

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

DeadlockWhat is Deadlock?

Handling Deadlock

What is Deadlock?

Two Processes Mutually Blocking Each Other

Resource A

Process 1

Process 2

Resource B

X

X

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

Demo: Blocked Process Threshold

SummaryConcurrency Models

Transactions in SQL Server

Isolation Levels

Pessimistic Concurrency Control

Optimistic Concurrency Control

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

top related