SQL Server 2005 Engine Optimistic Concurrency Tony Rogerson, SQL Server MVP Independent Consultant ...

Preview:

Citation preview

SQL Server 2005 Engine

Optimistic Concurrency

Tony Rogerson, SQL Server MVPIndependent Consultanthttp://sqlserverfaq.com tonyrogerson@torver.net

26th January 200612:45 – 13:15

Lunch n Learn webcast series

Agenda

• Isolations– Pessimistic

• Default Isolation – Writers block Readers

– Optimistic• READ_COMMITTED_SNAPSHOT

(statement level read consistency)• ALLOW_SNAPSHOT_ISOLATION

(transaction level read consistency)

• Architecture: Row Versioning• Monitoring

Uses

• Anything to reduce locking contention

• Near real-time statistics page (call centre, stock ticker)

• Anywhere using NOLOCK hint• Extraction from OLTP into DW/MIS• Not an answer for deadlocking but

will reduce locking and may help

Demo

Row Versioning

Sess A: UPDATE -> ‘START’

Sess B: XACT ISO – SNAPSHOTBEGIN TRAN SELECT…

Store old row ‘START’Sess A: UPDATE -> ‘UPD 1’

Sess C: XACT ISO – SNAPSHOTBEGIN TRAN SELECT…

Mark active

Mark active

Remove by clean up

Store old row ‘UPD 1’Sess A: UPDATE -> ‘UPD 2’

READ_COMMITTED_SNAPSHOT

• Statement level isolation (gives value of last committed row at time of query)

• Writer no longer blocks reader• Possibly reduced deadlocking

because of reduced locking contention

ALLOW_SNAPSHOT_ISOLATION

• Transaction level isolation (gives last committed row at start of transaction)

• Snapshot of values taken on select and held in version store in tempdb

• Stores full row (changed fragment for lob)

• Queries require SET TRANSACTION ISOLATION LEVEL SNAPSHOT

• Update fails if update on a value changed outside your snapshot

Architecture: Row Versioning

• Used by other stuff…– inserted and deleted tables in triggers– MARS (Multiple Active Result Sets)– Index operations using the ONLINE feature– Transaction Isolations

• Versions held in tempdb (the version store)• Changed row or changed fragment for LOB stored

in tempdb• Background thread runs every minute to remove

versions no longer required.• Short lived transactions will not require a disk IO

on tempdb

Monitoring

• sys.dm_tran_active_snapshot_database_transactions

• sys.dm_tran_version_store• sys.dm_tran_current_transaction

• sys.dm_tran_transactions_snapshot• sys.dm_tran_top_version_generators

• sys.dm_tran_locks

Thanks for Listening

Q & A

Tony Rogerson, SQL Server MVPIndependent Consultanthttp://sqlserverfaq.com tonyrogerson@torver.net

Recommended