Documente Academic
Documente Profesional
Documente Cultură
Optimizing Transactional
Code in SQL Server 2008
Svetlin Nakov
Director Training and Consulting Activities
National Academy for Software Development (NASD)
http://academy.devbg.org
Agenda
ACID Transactions and Transaction Modes
in SQL Server
Understanding Isolation Levels
Concurrency Problems
Locking vs. Versioning
Allowing Snapshot Isolation in SQL Server
SQL Server vs. Oracle Transactions
Transactions in SQL
Server 2008
Database Transactions
Transactions are a sequence of modifications in
the database executed as a single unit of work:
Either all of them execute successfully
Or none of the them
Example:
A bank transfer from one account into another
(withdrawal + deposit)
If either the withdrawal or the deposit fails the
whole operation is cancelled
Consistency
When completed, transactions leave all data and
all related structures in a consistent state
Isolation
Transactions don't see other's transaction's
uncompleted work (intermediate state)
Durability
Transactions persist despite of system failure
6
Transaction Mode
Autocommit transactions (default)
Statement level implicit transaction
Each statement commits as a single unit
Implicit transactions
Session Level Setting
SET IMPLICIT_TRANSACTIONS ON
8
Concurrency Problems
and Isolation
Concurrency Problems
SQL Server transaction isolation solves four
major concurrency problems
Dirty read occur when a reader reads
uncommitted data
Unrepeatable read occurs when existing row
change within a transaction
Lost updates occur when two writers modify
the same piece of state
Phantoms occur when new rows are added and
appear within a transaction
10
Locking Strategies
Optimistic concurrency
Locks are not used readers never wait
Conflicts are possible
Can be resolved before commit
Pessimistic concurrency
Use exclusive and shared locks
Transactions wait for each other
Low concurrency does not scale
11
12
Dirty NonrepeatLost
Phantom Concurrency
Reads able Reads Updates
Reads
Model
Read uncommitted
Yes
Yes
Yes
Yes
Pessimistic
Read committed
snapshot
No
Yes
Yes
Yes
Optimistic*
Read committed
No
Yes
Yes
Yes
Pessimistic
Repeatable read
No
No
No
Yes
Pessimistic
Snapshot
No
No
No**
No
Optimistic*
Serializable
No
No
No
No
Pessimistic
13
Concurrency Problems
Live Demo
Transactions Isolation:
Locking vs. Versioning
Traditional Transactions
SQL Server accomplishes isolation via
locking
Writers wait on read locks
Both readers and writers wait on write
locks
17
Snapshot Reads
Snapshot always reads coherent data
No read lock needed even though data being
changed
Version of each value maintained as long as
needed
Transaction reads version of value
corresponding to its start time
20
Snapshot Writes
Writes store the old version of changed
rows in tempdb
This takes time and consume storage but
allows better concurrency
24
Versioning Drawbacks
Versioning is not without cost
Takes up space in tempdb
Versions kept regardless of usage
Space usage must be planned
Up to twice as much I/O for updates
27
Oracle Isolation
Level
SQL Server
Oracle
Concurrency Concurrency
Pessimistic
Optimistic
Read committed
Pessimistic
Optimistic
Repeatable read
(manual locks)
Pessimistic
Pessimistic
Snapshot
Serializable
Optimistic
Optimistic
Serializable
(manual locks)
Pessimistic
28
Summary
Transaction Isolation with Locking (Pessimistic
concurrency)
Data read as of exact point in time or locked
Four transaction isolation levels
READ UNCOMMITTED, READ COMMITTED,
REPEATABLE READ, SERIALIZABLE
Summary
Transaction Isolation with Versioning
(Optimistic concurrency)
Data read as of beginning of transaction
Two transaction isolation levels
SNAPSHOT and READ_COMMITTED_SNAPSHOT
Questions?
31