SQL Transactions - What they are good for and how they work

Markus Winand
Markus WinandTrainer, Coach, Author at http://winand.at/
© 2016 by Markus Winand
Transactions
What they are good for and how they work
( )
Transactions are Brackets
Starting a transaction:
Explicit:
START	TRANSACTION		
Implicit:
INSERT / UPDATE /
DELETE

SELECT	
CREATE
Terminating a transaction:
Explicit:
COMMIT	
ROLLBACK	
Implementation defined:
Due to errors
CREATE
…	
Varies

amongst

products
Transactions have Characteristics
Access-Mode:
READ_ONLY	
READ_WRITE	
Constraint Mode:
IMMEDIATE	
DEFERRED
Transaction-Isolation-Level:
READ	UNCOMMITTED	
READ	COMMITTED	
REPEATABLE	READ	
SERIALIZABLE	
Diagnostic Size:
Archaic
Transactions have Characteristics
Access-Mode:
READ_ONLY	
READ_WRITE	
Constraint Mode:
IMMEDIATE	
DEFERRED
Transaction-Isolation-Level:
READ	UNCOMMITTED	
READ	COMMITTED	
REPEATABLE	READ	
SERIALIZABLE	
Diagnostic Size:
Archaic
Not strictly a

transaction


characteristic
Setting Transaction Characteristics
With explicit transaction begin:
START	TRANSACTION		ISOLATION	LEVEL	SERIALIZABLE		READ	WRITE	
For next (implicitly started) transaction:
SET			TRANSACTION		ISOLATION	LEVEL	SERIALIZABLE		READ	WRITE	
The standard uses SERIALIZEABLE as default,

but most implementations default to READ	COMMITTED.
In practice: Use an API if possible.
Use Cases for Transactions
Use Cases for Transactions
When writing
Use Cases for Transactions
Backing out incomplete changes…
… by the program with rollback
… by the RDBMS in case of crash
When writing
Use Cases for Transactions
Backing out incomplete changes…
… by the program with rollback
… by the RDBMS in case of crash
When writing
(Boring)
Use Cases for Transactions
Backing out incomplete changes…
… by the program with rollback
… by the RDBMS in case of crash
When writing When reading
(Boring)
Use Cases for Transactions
Backing out incomplete changes…
… by the program with rollback
… by the RDBMS in case of crash
When writing When reading
Simplify concurrent programs…
… by utilising the right

transaction isolation level
(Boring)
Use Cases for Transactions
Backing out incomplete changes…
… by the program with rollback
… by the RDBMS in case of crash
When writing When reading
Simplify concurrent programs…
… by utilising the right

transaction isolation level
(Boring) Amazing!
Standard Transaction Isolation Levels
Phenomena covered by the Standard
Dirty Read
Non-Repeatable
Read
Phantom Read
READ	UNCOMMITTED
READ	COMMITTED
REPEATABLE	READ
SERIALIZABLE
STOP
STOP STOP
STOPSTOPSTOP
Standard Transaction Isolation Levels
Phenomena covered by the Standard
Dirty Read
Non-Repeatable
Read
Phantom Read
READ	UNCOMMITTED
READ	COMMITTED
REPEATABLE	READ
SERIALIZABLE
STOP
STOP STOP
STOPSTOPSTOP
Incomplete,

unfortunately
How Comes?
SQL is a declarative language:

the standard doesn’t say how to implement it,

it just describes the effects it has.
Unfortunately, SQL-92 was written with locking in mind,

and thus only describes the effects in the granularity

in which they appear whey implemented using locking.
Although a commonly known issue, it was never changed.
https://en.wikipedia.org/wiki/Snapshot_isolation
Transaction Isolation Using Locks
Phenomena covered by the Standard
Dirty Read
Non-Repeatable
Read
Phantom Read
READ	UNCOMMITTED
READ	COMMITTED
REPEATABLE	READ
SERIALIZABLE
▶ Row (short) ▶ Row (TX) ▶ Row (TX)

▶ Predicate (TX)
STOP
STOP STOP
STOPSTOPSTOP
Transaction Isolation Using Locks
Phenomena covered by the Standard
Dirty Read
Non-Repeatable
Read
Phantom Read
READ	UNCOMMITTED
READ	COMMITTED
REPEATABLE	READ
SERIALIZABLE
▶ Row (short) ▶ Row (TX) ▶ Row (TX)

▶ Predicate (TX)
STOP
STOP STOP
STOPSTOPSTOP
Locks preventing

phenomena:

What (how long)
Transaction Isolation Using Locks
Phenomena covered by the Standard
Dirty Read
Non-Repeatable
Read
Phantom Read
READ	UNCOMMITTED
READ	COMMITTED
REPEATABLE	READ
SERIALIZABLE
▶ Row (short) ▶ Row (TX) ▶ Row (TX)

▶ Predicate (TX)
STOP
STOP STOP
STOPSTOPSTOP
Locks preventing

phenomena:

What (how long)
Transaction Isolation Using Locks
Phenomena covered by the Standard
Dirty Read
Non-Repeatable
Read
Phantom Read
READ	UNCOMMITTED
READ	COMMITTED
REPEATABLE	READ
SERIALIZABLE
▶ Row (short) ▶ Row (TX) ▶ Row (TX)

▶ Predicate (TX)
STOP
STOP STOP
STOPSTOPSTOP
Locks preventing

phenomena:

What (how long)
Transaction Isolation Using Locks
Phenomena covered by the Standard
Dirty Read
Non-Repeatable
Read
Phantom Read
READ	UNCOMMITTED
READ	COMMITTED
REPEATABLE	READ
SERIALIZABLE
▶ Row (short) ▶ Row (TX) ▶ Row (TX)

▶ Predicate (TX)
STOP
STOP STOP
STOPSTOPSTOP
Locks preventing

phenomena:

What (how long)
Example: Write Skew
Make sure the sum of two rows is positive

(e.g., two bank accounts belonging to the same person)
Code to withdraw from one account

(naive schema and not considering concurrency)

UPDATE	accounts

			SET	balance	=	balance	-	200

	WHERE	account	=	1	
SELECT	SUM(balance)

		FROM	accounts

	WHERE	account	IN	(1,2)
>= 0 commit					

<0 rollback
Examples: Disclaimer
The following examples just demonstrate

one possible way two transactions could interact.
The examples are by no means exhaustive.
Write Skew in Lock-based READ UNCOMMITTED
Account Balance
1 100
2 100
Transaction 1 Transaction 2
Write Skew in Lock-based READ UNCOMMITTED
Account Balance
1 100
2 100
Transaction 1 Transaction 2
▶ 1 -100
2 100
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=1
Write Skew in Lock-based READ UNCOMMITTED
Account Balance
1 100
2 100
Transaction 1 Transaction 2
▶ 1 -100
2 100
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=1
▶ 1 -100
2 -100 ◀
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=2
Write Skew in Lock-based READ UNCOMMITTED
Account Balance
1 100
2 100
Transaction 1 Transaction 2
▶ 1 -100
2 100
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=1
▶ 1 -100
2 -100 ◀
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=2
▶ 1 -100
2 -100 ◀
SELECT	sum(balance)	
		FROM	accounts	
	WHERE	account	IN	(1,2)
Write Skew in Lock-based READ UNCOMMITTED
Account Balance
1 100
2 100
Transaction 1 Transaction 2
▶ 1 -100
2 100
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=1
▶ 1 -100
2 -100 ◀
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=2
▶ 1 -100
2 -100 ◀
SELECT	sum(balance)	
		FROM	accounts	
	WHERE	account	IN	(1,2)
-200
Write Skew in Lock-based READ UNCOMMITTED
Account Balance
1 100
2 100
Transaction 1 Transaction 2
▶ 1 -100
2 100
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=1
▶ 1 -100
2 -100 ◀
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=2
▶ 1 -100
2 -100 ◀
SELECT	sum(balance)	
		FROM	accounts	
	WHERE	account	IN	(1,2)
▶ 1 -100
2 -100 ◀
SELECT	sum(balance)	
		FROM	accounts	
	WHERE	account	IN	(1,2)
-200
Write Skew in Lock-based READ UNCOMMITTED
Account Balance
1 100
2 100
Transaction 1 Transaction 2
▶ 1 -100
2 100
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=1
▶ 1 -100
2 -100 ◀
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=2
▶ 1 -100
2 -100 ◀
SELECT	sum(balance)	
		FROM	accounts	
	WHERE	account	IN	(1,2)
▶ 1 -100
2 -100 ◀
SELECT	sum(balance)	
		FROM	accounts	
	WHERE	account	IN	(1,2)
-200
-200
Write Skew in Lock-based READ COMMITTED
Write Skew in Lock-based READ COMMITTED
Account Balance
1 100
2 100
Transaction 1 Transaction 2
Write Skew in Lock-based READ COMMITTED
Account Balance
1 100
2 100
Transaction 1 Transaction 2
▶ 1 -100
2 100
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=1
Write Skew in Lock-based READ COMMITTED
Account Balance
1 100
2 100
Transaction 1 Transaction 2
▶ 1 -100
2 100
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=1
▶ 1 -100
2 -100 ◀
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=2
Write Skew in Lock-based READ COMMITTED
Account Balance
1 100
2 100
Transaction 1 Transaction 2
▶ 1 -100
2 100
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=1
▶ 1 -100
2 -100 ◀
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=2
▶ 1 -100
2 -100 ◀
SELECT	sum(balance)	
		FROM	accounts	
	WHERE	account	IN	(1,2)
Blocked
▶
Write Skew in Lock-based READ COMMITTED
Account Balance
1 100
2 100
Transaction 1 Transaction 2
▶ 1 -100
2 100
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=1
▶ 1 -100
2 -100 ◀
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=2
▶ 1 -100
2 -100 ◀
SELECT	sum(balance)	
		FROM	accounts	
	WHERE	account	IN	(1,2)
Blocked
Dead lock
▶
◀▶ 1 -100
2 -100 ◀
SELECT	sum(balance)	
		FROM	accounts	
	WHERE	account	IN	(1,2)▶
First Write, then Read: Conclusion
For this code, READ	COMMITTED is enough

to make one of the transactions succeed

(assuming a proper deadlock detection).
First Write, then Read: Conclusion
For this code, READ	COMMITTED is enough

to make one of the transactions succeed

(assuming a proper deadlock detection).
Exercise: Does this hold true in these cases too?
First Write, then Read: Conclusion
For this code, READ	COMMITTED is enough

to make one of the transactions succeed

(assuming a proper deadlock detection).
Exercise: Does this hold true in these cases too?
SELECT	sum(balance)

		FROM	accounts

	WHERE	account	IN	(1,2)	
(iff >= 200)
UPDATE	accounts

			SET	balance	=	balance	-	200

	WHERE	account	=	:a
First Write, then Read: Conclusion
For this code, READ	COMMITTED is enough

to make one of the transactions succeed

(assuming a proper deadlock detection).
Exercise: Does this hold true in these cases too?
SELECT	sum(balance)

		FROM	accounts

	WHERE	account	IN	(1,2)	
(iff >= 200)
UPDATE	accounts

			SET	balance	=	balance	-	200

	WHERE	account	=	:a
SELECT	balance	FROM	accounts

	WHERE	account	=	:a	
UPDATE	accounts	SET	balance	=	:b

	WHERE	account	=	:a	
SELECT	sum(balance)

		FROM	accounts

	WHERE	account	IN	(1,2)
Write-Skew: General Case
When using lock-based isolation 

REPEATABLE	READ

covers with write-skew too.
What’s Bad About Locking?
What’s Bad About Locking?
Writers will always need to block writers anyway
What’s Bad About Locking?
Writers will always need to block writers anyway
Readers never block readers
What’s Bad About Locking?
Writers will always need to block writers anyway
Readers never block readers
Writers block readers,

readers block writers

(except in READ	UNCOMMITTED)
What’s Bad About Locking?
Writers will always need to block writers anyway
Readers never block readers
Writers block readers,

readers block writers

(except in READ	UNCOMMITTED)
Great!
What’s Bad About Locking?
Writers will always need to block writers anyway
Readers never block readers
Writers block readers,

readers block writers

(except in READ	UNCOMMITTED)
Not so
good
Great!
Multi Version Concurrency Control (MVCC)
Multi Version Concurrency Control (MVCC)
Instead of waiting for writers,

just use the previous committed version of the data

(pretend the read happened before the write)
Multi Version Concurrency Control (MVCC)
Instead of waiting for writers,

just use the previous committed version of the data

(pretend the read happened before the write)
The reader effectively works on a snapshot
Multi Version Concurrency Control (MVCC)
Instead of waiting for writers,

just use the previous committed version of the data

(pretend the read happened before the write)
The reader effectively works on a snapshot
This prevents all three phenomena mentioned in the standard:

dirty read

non-repeatable read

phantom read
Write Skew When Using a Snapshot
Transaction 1 Transaction 2Account Balance
1 100
2 100
Write Skew When Using a Snapshot
Transaction 1 Transaction 2
1 -100
2 100
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=1
Account Balance
1 100
2 100
Write Skew When Using a Snapshot
Transaction 1 Transaction 2
1 100
2 -100
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=2
1 -100
2 100
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=1
Account Balance
1 100
2 100
Write Skew When Using a Snapshot
Transaction 1 Transaction 2
1 100
2 -100
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=2
1 -100
2 100
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=1
Account Balance
1 100
2 100
Write Skew When Using a Snapshot
Transaction 1 Transaction 2
1 100
2 -100
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=2
1 -100
2 100
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=1
Account Balance
1 100
2 100
Write Skew When Using a Snapshot
Transaction 1 Transaction 2
1 100
2 -100
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=2
1 -100
2 100
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=1
Account Balance
1 100
2 100
SELECT	sum(balance)	
		FROM	accounts	
	WHERE	account	IN	(1,2)
1 -100
2 100
Write Skew When Using a Snapshot
Transaction 1 Transaction 2
1 100
2 -100
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=2
1 -100
2 100
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=1
Account Balance
1 100
2 100
SELECT	sum(balance)	
		FROM	accounts	
	WHERE	account	IN	(1,2)
1 -100
2 100
1 100
2 -100
SELECT	sum(balance)	
		FROM	accounts	
	WHERE	account	IN	(1,2)
Write Skew When Using a Snapshot
Transaction 1 Transaction 2
1 100
2 -100
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=2
Account Balance
1 -100
2 -100
1 -100
2 100
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=1
Account Balance
1 100
2 100
SELECT	sum(balance)	
		FROM	accounts	
	WHERE	account	IN	(1,2)
1 -100
2 100
1 100
2 -100
SELECT	sum(balance)	
		FROM	accounts	
	WHERE	account	IN	(1,2)
Write Skew When Using a Snapshot
Transaction 1 Transaction 2
1 100
2 -100
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=2
Account Balance
1 -100
2 -100
1 -100
2 100
UPDATE	accounts	
			SET	balance=balance-200	
	WHERE	account=1
Account Balance
1 100
2 100
SELECT	sum(balance)	
		FROM	accounts	
	WHERE	account	IN	(1,2)
1 -100
2 100
1 100
2 -100
SELECT	sum(balance)	
		FROM	accounts	
	WHERE	account	IN	(1,2)
Can Snapshots and Serialisability Coexist?
Can Snapshots and Serialisability Coexist?
Simple snapshots cannot prevent write skew abnormalities.
Can Snapshots and Serialisability Coexist?
Simple snapshots cannot prevent write skew abnormalities.
But most transactions are

not vulnerable to write skew anyway.

(e.g., TPC-C is not vulnerable at all)
Can Snapshots and Serialisability Coexist?
Simple snapshots cannot prevent write skew abnormalities.
But most transactions are

not vulnerable to write skew anyway.

(e.g., TPC-C is not vulnerable at all)
The only two ways to prevent write skew

abnormalities at the database are

(1) putting locks when reading and

(2) proofing the serialization graph is acyclic
Can Snapshots and Serialisability Coexist?
Simple snapshots cannot prevent write skew abnormalities.
But most transactions are

not vulnerable to write skew anyway.

(e.g., TPC-C is not vulnerable at all)
The only two ways to prevent write skew

abnormalities at the database are

(1) putting locks when reading and

(2) proofing the serialization graph is acyclic
READ ONLY
transactions
are an important
special case
Can Snapshots and Serialisability Coexist?
Simple snapshots cannot prevent write skew abnormalities.
But most transactions are

not vulnerable to write skew anyway.

(e.g., TPC-C is not vulnerable at all)
The only two ways to prevent write skew

abnormalities at the database are

(1) putting locks when reading and

(2) proofing the serialization graph is acyclic
READ ONLY
transactions
are an important
special case
InnoDB

SQL Server

DB2
Can Snapshots and Serialisability Coexist?
Simple snapshots cannot prevent write skew abnormalities.
But most transactions are

not vulnerable to write skew anyway.

(e.g., TPC-C is not vulnerable at all)
The only two ways to prevent write skew

abnormalities at the database are

(1) putting locks when reading and

(2) proofing the serialization graph is acyclic
READ ONLY
transactions
are an important
special case
InnoDB

SQL Server

DB2 PostgreSQL
Can Snapshots and Serialisability Coexist?
Simple snapshots cannot prevent write skew abnormalities.
But most transactions are

not vulnerable to write skew anyway.

(e.g., TPC-C is not vulnerable at all)
The only two ways to prevent write skew

abnormalities at the database are

(1) putting locks when reading and

(2) proofing the serialization graph is acyclic
READ ONLY
transactions
are an important
special case
InnoDB

SQL Server

DB2 PostgreSQL
Oracle?
Can Snapshots and Serialisability Coexist?
Simple snapshots cannot prevent write skew abnormalities.
But most transactions are

not vulnerable to write skew anyway.

(e.g., TPC-C is not vulnerable at all)
The only two ways to prevent write skew

abnormalities at the database are

(1) putting locks when reading and

(2) proofing the serialization graph is acyclic
READ ONLY
transactions
are an important
special case
InnoDB

SQL Server

DB2 PostgreSQL
Can Snapshots and Serialisability Coexist?
PostgreSQL 9.1+ is the only(?) database that

uses MVCC and detects serialization graph cycles.

➜ Use SERIALIZABLE and you are done

In InnoDB (MySQL, MariaDB) and SQL Server, MVCC and Lock-Based SERIALIZABLE
isolation can be
How to use Snapshots and Prevent Write Skew
Transaction type
READ ONLY READ/WRITE
DB2 LUW
REPEATABLE	READ

(=SERIALIZABLE)
REPEATABLE	READ

(=SERIALIZALBE)
InnoDB

(MySQL, MariaDB)
REPEATABLE	READ1 SERIALIZABLE
PostgreSQL 9.1+ SERIALIZABLE SERIALIZABLE
Oracle SERIALIZABLE SERIALIZABLE
SQL Server 2005+ SNAPSHOT SERIALIZABLE
1: MySQL’s REPEATABLE READ also protects against phantom reads.
KEY
No shared locks

No write skew issues
Shared locks

No write skew issues
No shared locks

Write skew issues
SQL Server is Special
Except SQL Server, all MVCC capable databases use it per default.
In SQL Server, it must be enabled:

ALTER	DATABASE	…	SET	allow_snapshot_isolation	on;
This will make write operations keep old versions

(needs more space in permanent DB and tempdb)
Then you can use SNAPSHOT isolation (e.g., in read-only transactions).

Staying in SERIALISABLE for read/write transactions prevents write skew issues.

Note: Hint remain effective: NOLOCK will still do dirty-read in SNAPSHOT isolation.
Keep Transactions Focused
Don’t select data that is irrelevant for the transaction.

(an innocent looking function-call might query something you are not aware of)
Do unrelated stuff in distinct transactions.
Work fast.

(Never, ever keep a transaction open waiting for someone)

(In the code, but also not in your ad-hoc SQL session!)
Tune your statements.

(That makes your transactions shorter too)
1 of 72

Recommended

Solving PostgreSQL wicked problems by
Solving PostgreSQL wicked problemsSolving PostgreSQL wicked problems
Solving PostgreSQL wicked problemsAlexander Korotkov
4K views61 slides
Oracle Database performance tuning using oratop by
Oracle Database performance tuning using oratopOracle Database performance tuning using oratop
Oracle Database performance tuning using oratopSandesh Rao
709 views32 slides
PostgreSQL 튜닝워크샵 (2017년8월) by
PostgreSQL 튜닝워크샵 (2017년8월)PostgreSQL 튜닝워크샵 (2017년8월)
PostgreSQL 튜닝워크샵 (2017년8월)DBdemy
10.9K views207 slides
MySQL GTID 시작하기 by
MySQL GTID 시작하기MySQL GTID 시작하기
MySQL GTID 시작하기I Goo Lee
6.3K views17 slides
PostgreSQL Administration for System Administrators by
PostgreSQL Administration for System AdministratorsPostgreSQL Administration for System Administrators
PostgreSQL Administration for System AdministratorsCommand Prompt., Inc
7K views28 slides
Why Use EXPLAIN FORMAT=JSON? by
 Why Use EXPLAIN FORMAT=JSON?  Why Use EXPLAIN FORMAT=JSON?
Why Use EXPLAIN FORMAT=JSON? Sveta Smirnova
1.8K views47 slides

More Related Content

What's hot

Performance tuning a quick intoduction by
Performance tuning   a quick intoductionPerformance tuning   a quick intoduction
Performance tuning a quick intoductionRiyaj Shamsudeen
2.2K views162 slides
MySQL8.0 SYS スキーマ概要 by
MySQL8.0 SYS スキーマ概要MySQL8.0 SYS スキーマ概要
MySQL8.0 SYS スキーマ概要Shinya Sugiyama
14K views65 slides
Analyze Virtual Machine Overhead Compared to Bare Metal with Tracing by
Analyze Virtual Machine Overhead Compared to Bare Metal with TracingAnalyze Virtual Machine Overhead Compared to Bare Metal with Tracing
Analyze Virtual Machine Overhead Compared to Bare Metal with TracingScyllaDB
754 views72 slides
Oracle archi ppt by
Oracle archi pptOracle archi ppt
Oracle archi pptHitesh Kumar Markam
1.6K views38 slides
Oracle RAC 12c Practical Performance Management and Tuning OOW13 [CON8825] by
Oracle RAC 12c Practical Performance Management and Tuning OOW13 [CON8825]Oracle RAC 12c Practical Performance Management and Tuning OOW13 [CON8825]
Oracle RAC 12c Practical Performance Management and Tuning OOW13 [CON8825]Markus Michalewicz
9K views30 slides
MySQL8.0_performance_schema.pptx by
MySQL8.0_performance_schema.pptxMySQL8.0_performance_schema.pptx
MySQL8.0_performance_schema.pptxNeoClova
403 views48 slides

What's hot(20)

Performance tuning a quick intoduction by Riyaj Shamsudeen
Performance tuning   a quick intoductionPerformance tuning   a quick intoduction
Performance tuning a quick intoduction
Riyaj Shamsudeen2.2K views
Analyze Virtual Machine Overhead Compared to Bare Metal with Tracing by ScyllaDB
Analyze Virtual Machine Overhead Compared to Bare Metal with TracingAnalyze Virtual Machine Overhead Compared to Bare Metal with Tracing
Analyze Virtual Machine Overhead Compared to Bare Metal with Tracing
ScyllaDB754 views
Oracle RAC 12c Practical Performance Management and Tuning OOW13 [CON8825] by Markus Michalewicz
Oracle RAC 12c Practical Performance Management and Tuning OOW13 [CON8825]Oracle RAC 12c Practical Performance Management and Tuning OOW13 [CON8825]
Oracle RAC 12c Practical Performance Management and Tuning OOW13 [CON8825]
MySQL8.0_performance_schema.pptx by NeoClova
MySQL8.0_performance_schema.pptxMySQL8.0_performance_schema.pptx
MySQL8.0_performance_schema.pptx
NeoClova403 views
Parallel Replication in MySQL and MariaDB by Mydbops
Parallel Replication in MySQL and MariaDBParallel Replication in MySQL and MariaDB
Parallel Replication in MySQL and MariaDB
Mydbops860 views
Evolution of MySQL Parallel Replication by Mydbops
Evolution of MySQL Parallel Replication Evolution of MySQL Parallel Replication
Evolution of MySQL Parallel Replication
Mydbops1.3K views
Wars of MySQL Cluster ( InnoDB Cluster VS Galera ) by Mydbops
Wars of MySQL Cluster ( InnoDB Cluster VS Galera ) Wars of MySQL Cluster ( InnoDB Cluster VS Galera )
Wars of MySQL Cluster ( InnoDB Cluster VS Galera )
Mydbops2.5K views
How to Manage Scale-Out Environments with MariaDB MaxScale by MariaDB plc
How to Manage Scale-Out Environments with MariaDB MaxScaleHow to Manage Scale-Out Environments with MariaDB MaxScale
How to Manage Scale-Out Environments with MariaDB MaxScale
MariaDB plc1.2K views
Auditing and Monitoring PostgreSQL/EPAS by EDB
Auditing and Monitoring PostgreSQL/EPASAuditing and Monitoring PostgreSQL/EPAS
Auditing and Monitoring PostgreSQL/EPAS
EDB500 views
Exploring Oracle Multitenant in Oracle Database 12c by Zohar Elkayam
Exploring Oracle Multitenant in Oracle Database 12cExploring Oracle Multitenant in Oracle Database 12c
Exploring Oracle Multitenant in Oracle Database 12c
Zohar Elkayam1.1K views
Oracle Latch and Mutex Contention Troubleshooting by Tanel Poder
Oracle Latch and Mutex Contention TroubleshootingOracle Latch and Mutex Contention Troubleshooting
Oracle Latch and Mutex Contention Troubleshooting
Tanel Poder7.2K views
Disaster Recovery with MySQL InnoDB ClusterSet - What is it and how do I use it? by Miguel Araújo
Disaster Recovery with MySQL InnoDB ClusterSet - What is it and how do I use it?Disaster Recovery with MySQL InnoDB ClusterSet - What is it and how do I use it?
Disaster Recovery with MySQL InnoDB ClusterSet - What is it and how do I use it?
Miguel Araújo293 views
Oracle statistics by example by Mauro Pagano
Oracle statistics by exampleOracle statistics by example
Oracle statistics by example
Mauro Pagano1.6K views
My first 90 days with ClickHouse.pdf by Alkin Tezuysal
My first 90 days with ClickHouse.pdfMy first 90 days with ClickHouse.pdf
My first 90 days with ClickHouse.pdf
Alkin Tezuysal499 views

Similar to SQL Transactions - What they are good for and how they work

Transaction by
TransactionTransaction
TransactionDimara Hakim
560 views32 slides
Database Transactions and SQL Server Concurrency by
Database Transactions and SQL Server ConcurrencyDatabase Transactions and SQL Server Concurrency
Database Transactions and SQL Server ConcurrencyBoris Hristov
7.5K views50 slides
CQRS & event sourcing in the wild by
CQRS & event sourcing in the wildCQRS & event sourcing in the wild
CQRS & event sourcing in the wildMichiel Rook
4K views54 slides
MySQL Transactions by
MySQL TransactionsMySQL Transactions
MySQL TransactionsReggie Niccolo Santos
3.8K views16 slides
Formal Verification of Web Service Interaction Contracts by
Formal Verification of Web Service Interaction ContractsFormal Verification of Web Service Interaction Contracts
Formal Verification of Web Service Interaction ContractsGera Shegalov
622 views36 slides
The Nightmare of Locking, Blocking and Isolation Levels! by
The Nightmare of Locking, Blocking and Isolation Levels!The Nightmare of Locking, Blocking and Isolation Levels!
The Nightmare of Locking, Blocking and Isolation Levels!Boris Hristov
527 views31 slides

Similar to SQL Transactions - What they are good for and how they work(20)

Database Transactions and SQL Server Concurrency by Boris Hristov
Database Transactions and SQL Server ConcurrencyDatabase Transactions and SQL Server Concurrency
Database Transactions and SQL Server Concurrency
Boris Hristov7.5K views
CQRS & event sourcing in the wild by Michiel Rook
CQRS & event sourcing in the wildCQRS & event sourcing in the wild
CQRS & event sourcing in the wild
Michiel Rook4K views
Formal Verification of Web Service Interaction Contracts by Gera Shegalov
Formal Verification of Web Service Interaction ContractsFormal Verification of Web Service Interaction Contracts
Formal Verification of Web Service Interaction Contracts
Gera Shegalov622 views
The Nightmare of Locking, Blocking and Isolation Levels! by Boris Hristov
The Nightmare of Locking, Blocking and Isolation Levels!The Nightmare of Locking, Blocking and Isolation Levels!
The Nightmare of Locking, Blocking and Isolation Levels!
Boris Hristov527 views
Oracle - Program with PL/SQL - Lession 04 by Thuan Nguyen
Oracle - Program with PL/SQL - Lession 04Oracle - Program with PL/SQL - Lession 04
Oracle - Program with PL/SQL - Lession 04
Thuan Nguyen1.9K views
Welcome to the nightmare of locking, blocking and isolation levels! by Boris Hristov
Welcome to the nightmare of locking, blocking and isolation levels!Welcome to the nightmare of locking, blocking and isolation levels!
Welcome to the nightmare of locking, blocking and isolation levels!
Boris Hristov2.3K views
MySQL GTID Concepts, Implementation and troubleshooting by Mydbops
MySQL GTID Concepts, Implementation and troubleshooting MySQL GTID Concepts, Implementation and troubleshooting
MySQL GTID Concepts, Implementation and troubleshooting
Mydbops2.5K views
SQL Performance Solutions: Refactor Mercilessly, Index Wisely by Enkitec
SQL Performance Solutions: Refactor Mercilessly, Index WiselySQL Performance Solutions: Refactor Mercilessly, Index Wisely
SQL Performance Solutions: Refactor Mercilessly, Index Wisely
Enkitec765 views
PROCEDURE FOR MONTH END DAY 1 INCLUDING SCREEN SHOT & SCRIPTS (Final rev 090211) by Shakil Zaman
PROCEDURE FOR MONTH END DAY 1 INCLUDING SCREEN SHOT & SCRIPTS (Final rev 090211)PROCEDURE FOR MONTH END DAY 1 INCLUDING SCREEN SHOT & SCRIPTS (Final rev 090211)
PROCEDURE FOR MONTH END DAY 1 INCLUDING SCREEN SHOT & SCRIPTS (Final rev 090211)
Shakil Zaman130 views
Programming in Oracle with PL/SQL by lubna19
Programming in Oracle with PL/SQLProgramming in Oracle with PL/SQL
Programming in Oracle with PL/SQL
lubna194.9K views
Transaction management and concurrency by Venkata Sreeram
Transaction management and concurrencyTransaction management and concurrency
Transaction management and concurrency
Venkata Sreeram70 views
Transaction Properties(ACID Properties) by Yaksh Jethva
Transaction Properties(ACID Properties)Transaction Properties(ACID Properties)
Transaction Properties(ACID Properties)
Yaksh Jethva1.1K views
How Many Ways Can I Manage Oracle GoldenGate? by Enkitec
How Many Ways Can I Manage Oracle GoldenGate?How Many Ways Can I Manage Oracle GoldenGate?
How Many Ways Can I Manage Oracle GoldenGate?
Enkitec1.8K views
The road to continuous deployment (PHPCon Poland 2016) by Michiel Rook
The road to continuous deployment (PHPCon Poland 2016)The road to continuous deployment (PHPCon Poland 2016)
The road to continuous deployment (PHPCon Poland 2016)
Michiel Rook2.7K views

More from Markus Winand

Standard SQL features where PostgreSQL beats its competitors by
Standard SQL features where PostgreSQL beats its competitorsStandard SQL features where PostgreSQL beats its competitors
Standard SQL features where PostgreSQL beats its competitorsMarkus Winand
58.9K views77 slides
Four* Major Database Releases of 2017 in Review by
Four* Major Database Releases of 2017 in ReviewFour* Major Database Releases of 2017 in Review
Four* Major Database Releases of 2017 in ReviewMarkus Winand
1.5K views29 slides
Row Pattern Matching in SQL:2016 by
Row Pattern Matching in SQL:2016Row Pattern Matching in SQL:2016
Row Pattern Matching in SQL:2016Markus Winand
109.4K views92 slides
Backend to Frontend: When database optimization affects the full stack by
Backend to Frontend: When database optimization affects the full stackBackend to Frontend: When database optimization affects the full stack
Backend to Frontend: When database optimization affects the full stackMarkus Winand
3.7K views52 slides
Modern SQL in Open Source and Commercial Databases by
Modern SQL in Open Source and Commercial DatabasesModern SQL in Open Source and Commercial Databases
Modern SQL in Open Source and Commercial DatabasesMarkus Winand
450.8K views174 slides
Volkskrankheit "Stiefmuetterliche Indizierung" by
Volkskrankheit "Stiefmuetterliche Indizierung"Volkskrankheit "Stiefmuetterliche Indizierung"
Volkskrankheit "Stiefmuetterliche Indizierung"Markus Winand
16K views62 slides

More from Markus Winand(9)

Standard SQL features where PostgreSQL beats its competitors by Markus Winand
Standard SQL features where PostgreSQL beats its competitorsStandard SQL features where PostgreSQL beats its competitors
Standard SQL features where PostgreSQL beats its competitors
Markus Winand58.9K views
Four* Major Database Releases of 2017 in Review by Markus Winand
Four* Major Database Releases of 2017 in ReviewFour* Major Database Releases of 2017 in Review
Four* Major Database Releases of 2017 in Review
Markus Winand1.5K views
Row Pattern Matching in SQL:2016 by Markus Winand
Row Pattern Matching in SQL:2016Row Pattern Matching in SQL:2016
Row Pattern Matching in SQL:2016
Markus Winand109.4K views
Backend to Frontend: When database optimization affects the full stack by Markus Winand
Backend to Frontend: When database optimization affects the full stackBackend to Frontend: When database optimization affects the full stack
Backend to Frontend: When database optimization affects the full stack
Markus Winand3.7K views
Modern SQL in Open Source and Commercial Databases by Markus Winand
Modern SQL in Open Source and Commercial DatabasesModern SQL in Open Source and Commercial Databases
Modern SQL in Open Source and Commercial Databases
Markus Winand450.8K views
Volkskrankheit "Stiefmuetterliche Indizierung" by Markus Winand
Volkskrankheit "Stiefmuetterliche Indizierung"Volkskrankheit "Stiefmuetterliche Indizierung"
Volkskrankheit "Stiefmuetterliche Indizierung"
Markus Winand16K views
SQL Performance - Vienna System Architects Meetup 20131202 by Markus Winand
SQL Performance - Vienna System Architects Meetup 20131202SQL Performance - Vienna System Architects Meetup 20131202
SQL Performance - Vienna System Architects Meetup 20131202
Markus Winand1.8K views
Indexes: The neglected performance all rounder by Markus Winand
Indexes: The neglected performance all rounderIndexes: The neglected performance all rounder
Indexes: The neglected performance all rounder
Markus Winand35.5K views
Pagination Done the Right Way by Markus Winand
Pagination Done the Right WayPagination Done the Right Way
Pagination Done the Right Way
Markus Winand415.2K views

Recently uploaded

KVM Security Groups Under the Hood - Wido den Hollander - Your.Online by
KVM Security Groups Under the Hood - Wido den Hollander - Your.OnlineKVM Security Groups Under the Hood - Wido den Hollander - Your.Online
KVM Security Groups Under the Hood - Wido den Hollander - Your.OnlineShapeBlue
102 views19 slides
Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De... by
Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...
Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...Moses Kemibaro
29 views38 slides
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue by
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlueCloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlueShapeBlue
46 views13 slides
Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava... by
Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava...Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava...
Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava...ShapeBlue
48 views17 slides
Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ... by
Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ...Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ...
Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ...ShapeBlue
77 views12 slides
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N... by
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...James Anderson
133 views32 slides

Recently uploaded(20)

KVM Security Groups Under the Hood - Wido den Hollander - Your.Online by ShapeBlue
KVM Security Groups Under the Hood - Wido den Hollander - Your.OnlineKVM Security Groups Under the Hood - Wido den Hollander - Your.Online
KVM Security Groups Under the Hood - Wido den Hollander - Your.Online
ShapeBlue102 views
Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De... by Moses Kemibaro
Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...
Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...
Moses Kemibaro29 views
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue by ShapeBlue
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlueCloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue
CloudStack Managed User Data and Demo - Harikrishna Patnala - ShapeBlue
ShapeBlue46 views
Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava... by ShapeBlue
Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava...Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava...
Centralized Logging Feature in CloudStack using ELK and Grafana - Kiran Chava...
ShapeBlue48 views
Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ... by ShapeBlue
Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ...Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ...
Backup and Disaster Recovery with CloudStack and StorPool - Workshop - Venko ...
ShapeBlue77 views
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N... by James Anderson
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
GDG Cloud Southlake 28 Brad Taylor and Shawn Augenstein Old Problems in the N...
James Anderson133 views
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f... by TrustArc
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc77 views
Why and How CloudStack at weSystems - Stephan Bienek - weSystems by ShapeBlue
Why and How CloudStack at weSystems - Stephan Bienek - weSystemsWhy and How CloudStack at weSystems - Stephan Bienek - weSystems
Why and How CloudStack at weSystems - Stephan Bienek - weSystems
ShapeBlue111 views
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T by ShapeBlue
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&TCloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T
ShapeBlue56 views
What’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlue by ShapeBlue
What’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlueWhat’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlue
What’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlue
ShapeBlue131 views
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive by Network Automation Forum
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLiveAutomating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Data Integrity for Banking and Financial Services by Precisely
Data Integrity for Banking and Financial ServicesData Integrity for Banking and Financial Services
Data Integrity for Banking and Financial Services
Precisely56 views
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue by ShapeBlue
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlueVNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue
VNF Integration and Support in CloudStack - Wei Zhou - ShapeBlue
ShapeBlue85 views
Setting Up Your First CloudStack Environment with Beginners Challenges - MD R... by ShapeBlue
Setting Up Your First CloudStack Environment with Beginners Challenges - MD R...Setting Up Your First CloudStack Environment with Beginners Challenges - MD R...
Setting Up Your First CloudStack Environment with Beginners Challenges - MD R...
ShapeBlue54 views
PharoJS - Zürich Smalltalk Group Meetup November 2023 by Noury Bouraqadi
PharoJS - Zürich Smalltalk Group Meetup November 2023PharoJS - Zürich Smalltalk Group Meetup November 2023
PharoJS - Zürich Smalltalk Group Meetup November 2023
Noury Bouraqadi141 views
State of the Union - Rohit Yadav - Apache CloudStack by ShapeBlue
State of the Union - Rohit Yadav - Apache CloudStackState of the Union - Rohit Yadav - Apache CloudStack
State of the Union - Rohit Yadav - Apache CloudStack
ShapeBlue145 views
2FA and OAuth2 in CloudStack - Andrija Panić - ShapeBlue by ShapeBlue
2FA and OAuth2 in CloudStack - Andrija Panić - ShapeBlue2FA and OAuth2 in CloudStack - Andrija Panić - ShapeBlue
2FA and OAuth2 in CloudStack - Andrija Panić - ShapeBlue
ShapeBlue50 views
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ... by Jasper Oosterveld
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...
ESPC 2023 - Protect and Govern your Sensitive Data with Microsoft Purview in ...

SQL Transactions - What they are good for and how they work

  • 1. © 2016 by Markus Winand Transactions What they are good for and how they work
  • 2. ( ) Transactions are Brackets Starting a transaction: Explicit: START TRANSACTION Implicit: INSERT / UPDATE / DELETE SELECT CREATE Terminating a transaction: Explicit: COMMIT ROLLBACK Implementation defined: Due to errors CREATE … Varies
 amongst
 products
  • 3. Transactions have Characteristics Access-Mode: READ_ONLY READ_WRITE Constraint Mode: IMMEDIATE DEFERRED Transaction-Isolation-Level: READ UNCOMMITTED READ COMMITTED REPEATABLE READ SERIALIZABLE Diagnostic Size: Archaic
  • 4. Transactions have Characteristics Access-Mode: READ_ONLY READ_WRITE Constraint Mode: IMMEDIATE DEFERRED Transaction-Isolation-Level: READ UNCOMMITTED READ COMMITTED REPEATABLE READ SERIALIZABLE Diagnostic Size: Archaic Not strictly a
 transaction 
 characteristic
  • 5. Setting Transaction Characteristics With explicit transaction begin: START TRANSACTION ISOLATION LEVEL SERIALIZABLE READ WRITE For next (implicitly started) transaction: SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ WRITE The standard uses SERIALIZEABLE as default,
 but most implementations default to READ COMMITTED. In practice: Use an API if possible.
  • 6. Use Cases for Transactions
  • 7. Use Cases for Transactions When writing
  • 8. Use Cases for Transactions Backing out incomplete changes… … by the program with rollback … by the RDBMS in case of crash When writing
  • 9. Use Cases for Transactions Backing out incomplete changes… … by the program with rollback … by the RDBMS in case of crash When writing (Boring)
  • 10. Use Cases for Transactions Backing out incomplete changes… … by the program with rollback … by the RDBMS in case of crash When writing When reading (Boring)
  • 11. Use Cases for Transactions Backing out incomplete changes… … by the program with rollback … by the RDBMS in case of crash When writing When reading Simplify concurrent programs… … by utilising the right
 transaction isolation level (Boring)
  • 12. Use Cases for Transactions Backing out incomplete changes… … by the program with rollback … by the RDBMS in case of crash When writing When reading Simplify concurrent programs… … by utilising the right
 transaction isolation level (Boring) Amazing!
  • 13. Standard Transaction Isolation Levels Phenomena covered by the Standard Dirty Read Non-Repeatable Read Phantom Read READ UNCOMMITTED READ COMMITTED REPEATABLE READ SERIALIZABLE STOP STOP STOP STOPSTOPSTOP
  • 14. Standard Transaction Isolation Levels Phenomena covered by the Standard Dirty Read Non-Repeatable Read Phantom Read READ UNCOMMITTED READ COMMITTED REPEATABLE READ SERIALIZABLE STOP STOP STOP STOPSTOPSTOP Incomplete,
 unfortunately
  • 15. How Comes? SQL is a declarative language:
 the standard doesn’t say how to implement it,
 it just describes the effects it has. Unfortunately, SQL-92 was written with locking in mind,
 and thus only describes the effects in the granularity
 in which they appear whey implemented using locking. Although a commonly known issue, it was never changed. https://en.wikipedia.org/wiki/Snapshot_isolation
  • 16. Transaction Isolation Using Locks Phenomena covered by the Standard Dirty Read Non-Repeatable Read Phantom Read READ UNCOMMITTED READ COMMITTED REPEATABLE READ SERIALIZABLE ▶ Row (short) ▶ Row (TX) ▶ Row (TX) ▶ Predicate (TX) STOP STOP STOP STOPSTOPSTOP
  • 17. Transaction Isolation Using Locks Phenomena covered by the Standard Dirty Read Non-Repeatable Read Phantom Read READ UNCOMMITTED READ COMMITTED REPEATABLE READ SERIALIZABLE ▶ Row (short) ▶ Row (TX) ▶ Row (TX) ▶ Predicate (TX) STOP STOP STOP STOPSTOPSTOP Locks preventing
 phenomena:
 What (how long)
  • 18. Transaction Isolation Using Locks Phenomena covered by the Standard Dirty Read Non-Repeatable Read Phantom Read READ UNCOMMITTED READ COMMITTED REPEATABLE READ SERIALIZABLE ▶ Row (short) ▶ Row (TX) ▶ Row (TX) ▶ Predicate (TX) STOP STOP STOP STOPSTOPSTOP Locks preventing
 phenomena:
 What (how long)
  • 19. Transaction Isolation Using Locks Phenomena covered by the Standard Dirty Read Non-Repeatable Read Phantom Read READ UNCOMMITTED READ COMMITTED REPEATABLE READ SERIALIZABLE ▶ Row (short) ▶ Row (TX) ▶ Row (TX) ▶ Predicate (TX) STOP STOP STOP STOPSTOPSTOP Locks preventing
 phenomena:
 What (how long)
  • 20. Transaction Isolation Using Locks Phenomena covered by the Standard Dirty Read Non-Repeatable Read Phantom Read READ UNCOMMITTED READ COMMITTED REPEATABLE READ SERIALIZABLE ▶ Row (short) ▶ Row (TX) ▶ Row (TX) ▶ Predicate (TX) STOP STOP STOP STOPSTOPSTOP Locks preventing
 phenomena:
 What (how long)
  • 21. Example: Write Skew Make sure the sum of two rows is positive
 (e.g., two bank accounts belonging to the same person) Code to withdraw from one account
 (naive schema and not considering concurrency) UPDATE accounts
 SET balance = balance - 200
 WHERE account = 1 SELECT SUM(balance)
 FROM accounts
 WHERE account IN (1,2) >= 0 commit 
 <0 rollback
  • 22. Examples: Disclaimer The following examples just demonstrate
 one possible way two transactions could interact. The examples are by no means exhaustive.
  • 23. Write Skew in Lock-based READ UNCOMMITTED Account Balance 1 100 2 100 Transaction 1 Transaction 2
  • 24. Write Skew in Lock-based READ UNCOMMITTED Account Balance 1 100 2 100 Transaction 1 Transaction 2 ▶ 1 -100 2 100 UPDATE accounts SET balance=balance-200 WHERE account=1
  • 25. Write Skew in Lock-based READ UNCOMMITTED Account Balance 1 100 2 100 Transaction 1 Transaction 2 ▶ 1 -100 2 100 UPDATE accounts SET balance=balance-200 WHERE account=1 ▶ 1 -100 2 -100 ◀ UPDATE accounts SET balance=balance-200 WHERE account=2
  • 26. Write Skew in Lock-based READ UNCOMMITTED Account Balance 1 100 2 100 Transaction 1 Transaction 2 ▶ 1 -100 2 100 UPDATE accounts SET balance=balance-200 WHERE account=1 ▶ 1 -100 2 -100 ◀ UPDATE accounts SET balance=balance-200 WHERE account=2 ▶ 1 -100 2 -100 ◀ SELECT sum(balance) FROM accounts WHERE account IN (1,2)
  • 27. Write Skew in Lock-based READ UNCOMMITTED Account Balance 1 100 2 100 Transaction 1 Transaction 2 ▶ 1 -100 2 100 UPDATE accounts SET balance=balance-200 WHERE account=1 ▶ 1 -100 2 -100 ◀ UPDATE accounts SET balance=balance-200 WHERE account=2 ▶ 1 -100 2 -100 ◀ SELECT sum(balance) FROM accounts WHERE account IN (1,2) -200
  • 28. Write Skew in Lock-based READ UNCOMMITTED Account Balance 1 100 2 100 Transaction 1 Transaction 2 ▶ 1 -100 2 100 UPDATE accounts SET balance=balance-200 WHERE account=1 ▶ 1 -100 2 -100 ◀ UPDATE accounts SET balance=balance-200 WHERE account=2 ▶ 1 -100 2 -100 ◀ SELECT sum(balance) FROM accounts WHERE account IN (1,2) ▶ 1 -100 2 -100 ◀ SELECT sum(balance) FROM accounts WHERE account IN (1,2) -200
  • 29. Write Skew in Lock-based READ UNCOMMITTED Account Balance 1 100 2 100 Transaction 1 Transaction 2 ▶ 1 -100 2 100 UPDATE accounts SET balance=balance-200 WHERE account=1 ▶ 1 -100 2 -100 ◀ UPDATE accounts SET balance=balance-200 WHERE account=2 ▶ 1 -100 2 -100 ◀ SELECT sum(balance) FROM accounts WHERE account IN (1,2) ▶ 1 -100 2 -100 ◀ SELECT sum(balance) FROM accounts WHERE account IN (1,2) -200 -200
  • 30. Write Skew in Lock-based READ COMMITTED
  • 31. Write Skew in Lock-based READ COMMITTED Account Balance 1 100 2 100 Transaction 1 Transaction 2
  • 32. Write Skew in Lock-based READ COMMITTED Account Balance 1 100 2 100 Transaction 1 Transaction 2 ▶ 1 -100 2 100 UPDATE accounts SET balance=balance-200 WHERE account=1
  • 33. Write Skew in Lock-based READ COMMITTED Account Balance 1 100 2 100 Transaction 1 Transaction 2 ▶ 1 -100 2 100 UPDATE accounts SET balance=balance-200 WHERE account=1 ▶ 1 -100 2 -100 ◀ UPDATE accounts SET balance=balance-200 WHERE account=2
  • 34. Write Skew in Lock-based READ COMMITTED Account Balance 1 100 2 100 Transaction 1 Transaction 2 ▶ 1 -100 2 100 UPDATE accounts SET balance=balance-200 WHERE account=1 ▶ 1 -100 2 -100 ◀ UPDATE accounts SET balance=balance-200 WHERE account=2 ▶ 1 -100 2 -100 ◀ SELECT sum(balance) FROM accounts WHERE account IN (1,2) Blocked ▶
  • 35. Write Skew in Lock-based READ COMMITTED Account Balance 1 100 2 100 Transaction 1 Transaction 2 ▶ 1 -100 2 100 UPDATE accounts SET balance=balance-200 WHERE account=1 ▶ 1 -100 2 -100 ◀ UPDATE accounts SET balance=balance-200 WHERE account=2 ▶ 1 -100 2 -100 ◀ SELECT sum(balance) FROM accounts WHERE account IN (1,2) Blocked Dead lock ▶ ◀▶ 1 -100 2 -100 ◀ SELECT sum(balance) FROM accounts WHERE account IN (1,2)▶
  • 36. First Write, then Read: Conclusion For this code, READ COMMITTED is enough
 to make one of the transactions succeed
 (assuming a proper deadlock detection).
  • 37. First Write, then Read: Conclusion For this code, READ COMMITTED is enough
 to make one of the transactions succeed
 (assuming a proper deadlock detection). Exercise: Does this hold true in these cases too?
  • 38. First Write, then Read: Conclusion For this code, READ COMMITTED is enough
 to make one of the transactions succeed
 (assuming a proper deadlock detection). Exercise: Does this hold true in these cases too? SELECT sum(balance)
 FROM accounts
 WHERE account IN (1,2) (iff >= 200) UPDATE accounts
 SET balance = balance - 200
 WHERE account = :a
  • 39. First Write, then Read: Conclusion For this code, READ COMMITTED is enough
 to make one of the transactions succeed
 (assuming a proper deadlock detection). Exercise: Does this hold true in these cases too? SELECT sum(balance)
 FROM accounts
 WHERE account IN (1,2) (iff >= 200) UPDATE accounts
 SET balance = balance - 200
 WHERE account = :a SELECT balance FROM accounts
 WHERE account = :a UPDATE accounts SET balance = :b
 WHERE account = :a SELECT sum(balance)
 FROM accounts
 WHERE account IN (1,2)
  • 40. Write-Skew: General Case When using lock-based isolation 
 REPEATABLE READ
 covers with write-skew too.
  • 41. What’s Bad About Locking?
  • 42. What’s Bad About Locking? Writers will always need to block writers anyway
  • 43. What’s Bad About Locking? Writers will always need to block writers anyway Readers never block readers
  • 44. What’s Bad About Locking? Writers will always need to block writers anyway Readers never block readers Writers block readers,
 readers block writers
 (except in READ UNCOMMITTED)
  • 45. What’s Bad About Locking? Writers will always need to block writers anyway Readers never block readers Writers block readers,
 readers block writers
 (except in READ UNCOMMITTED) Great!
  • 46. What’s Bad About Locking? Writers will always need to block writers anyway Readers never block readers Writers block readers,
 readers block writers
 (except in READ UNCOMMITTED) Not so good Great!
  • 47. Multi Version Concurrency Control (MVCC)
  • 48. Multi Version Concurrency Control (MVCC) Instead of waiting for writers,
 just use the previous committed version of the data
 (pretend the read happened before the write)
  • 49. Multi Version Concurrency Control (MVCC) Instead of waiting for writers,
 just use the previous committed version of the data
 (pretend the read happened before the write) The reader effectively works on a snapshot
  • 50. Multi Version Concurrency Control (MVCC) Instead of waiting for writers,
 just use the previous committed version of the data
 (pretend the read happened before the write) The reader effectively works on a snapshot This prevents all three phenomena mentioned in the standard:
 dirty read
 non-repeatable read
 phantom read
  • 51. Write Skew When Using a Snapshot Transaction 1 Transaction 2Account Balance 1 100 2 100
  • 52. Write Skew When Using a Snapshot Transaction 1 Transaction 2 1 -100 2 100 UPDATE accounts SET balance=balance-200 WHERE account=1 Account Balance 1 100 2 100
  • 53. Write Skew When Using a Snapshot Transaction 1 Transaction 2 1 100 2 -100 UPDATE accounts SET balance=balance-200 WHERE account=2 1 -100 2 100 UPDATE accounts SET balance=balance-200 WHERE account=1 Account Balance 1 100 2 100
  • 54. Write Skew When Using a Snapshot Transaction 1 Transaction 2 1 100 2 -100 UPDATE accounts SET balance=balance-200 WHERE account=2 1 -100 2 100 UPDATE accounts SET balance=balance-200 WHERE account=1 Account Balance 1 100 2 100
  • 55. Write Skew When Using a Snapshot Transaction 1 Transaction 2 1 100 2 -100 UPDATE accounts SET balance=balance-200 WHERE account=2 1 -100 2 100 UPDATE accounts SET balance=balance-200 WHERE account=1 Account Balance 1 100 2 100
  • 56. Write Skew When Using a Snapshot Transaction 1 Transaction 2 1 100 2 -100 UPDATE accounts SET balance=balance-200 WHERE account=2 1 -100 2 100 UPDATE accounts SET balance=balance-200 WHERE account=1 Account Balance 1 100 2 100 SELECT sum(balance) FROM accounts WHERE account IN (1,2) 1 -100 2 100
  • 57. Write Skew When Using a Snapshot Transaction 1 Transaction 2 1 100 2 -100 UPDATE accounts SET balance=balance-200 WHERE account=2 1 -100 2 100 UPDATE accounts SET balance=balance-200 WHERE account=1 Account Balance 1 100 2 100 SELECT sum(balance) FROM accounts WHERE account IN (1,2) 1 -100 2 100 1 100 2 -100 SELECT sum(balance) FROM accounts WHERE account IN (1,2)
  • 58. Write Skew When Using a Snapshot Transaction 1 Transaction 2 1 100 2 -100 UPDATE accounts SET balance=balance-200 WHERE account=2 Account Balance 1 -100 2 -100 1 -100 2 100 UPDATE accounts SET balance=balance-200 WHERE account=1 Account Balance 1 100 2 100 SELECT sum(balance) FROM accounts WHERE account IN (1,2) 1 -100 2 100 1 100 2 -100 SELECT sum(balance) FROM accounts WHERE account IN (1,2)
  • 59. Write Skew When Using a Snapshot Transaction 1 Transaction 2 1 100 2 -100 UPDATE accounts SET balance=balance-200 WHERE account=2 Account Balance 1 -100 2 -100 1 -100 2 100 UPDATE accounts SET balance=balance-200 WHERE account=1 Account Balance 1 100 2 100 SELECT sum(balance) FROM accounts WHERE account IN (1,2) 1 -100 2 100 1 100 2 -100 SELECT sum(balance) FROM accounts WHERE account IN (1,2)
  • 60. Can Snapshots and Serialisability Coexist?
  • 61. Can Snapshots and Serialisability Coexist? Simple snapshots cannot prevent write skew abnormalities.
  • 62. Can Snapshots and Serialisability Coexist? Simple snapshots cannot prevent write skew abnormalities. But most transactions are
 not vulnerable to write skew anyway.
 (e.g., TPC-C is not vulnerable at all)
  • 63. Can Snapshots and Serialisability Coexist? Simple snapshots cannot prevent write skew abnormalities. But most transactions are
 not vulnerable to write skew anyway.
 (e.g., TPC-C is not vulnerable at all) The only two ways to prevent write skew
 abnormalities at the database are
 (1) putting locks when reading and
 (2) proofing the serialization graph is acyclic
  • 64. Can Snapshots and Serialisability Coexist? Simple snapshots cannot prevent write skew abnormalities. But most transactions are
 not vulnerable to write skew anyway.
 (e.g., TPC-C is not vulnerable at all) The only two ways to prevent write skew
 abnormalities at the database are
 (1) putting locks when reading and
 (2) proofing the serialization graph is acyclic READ ONLY transactions are an important special case
  • 65. Can Snapshots and Serialisability Coexist? Simple snapshots cannot prevent write skew abnormalities. But most transactions are
 not vulnerable to write skew anyway.
 (e.g., TPC-C is not vulnerable at all) The only two ways to prevent write skew
 abnormalities at the database are
 (1) putting locks when reading and
 (2) proofing the serialization graph is acyclic READ ONLY transactions are an important special case InnoDB
 SQL Server
 DB2
  • 66. Can Snapshots and Serialisability Coexist? Simple snapshots cannot prevent write skew abnormalities. But most transactions are
 not vulnerable to write skew anyway.
 (e.g., TPC-C is not vulnerable at all) The only two ways to prevent write skew
 abnormalities at the database are
 (1) putting locks when reading and
 (2) proofing the serialization graph is acyclic READ ONLY transactions are an important special case InnoDB
 SQL Server
 DB2 PostgreSQL
  • 67. Can Snapshots and Serialisability Coexist? Simple snapshots cannot prevent write skew abnormalities. But most transactions are
 not vulnerable to write skew anyway.
 (e.g., TPC-C is not vulnerable at all) The only two ways to prevent write skew
 abnormalities at the database are
 (1) putting locks when reading and
 (2) proofing the serialization graph is acyclic READ ONLY transactions are an important special case InnoDB
 SQL Server
 DB2 PostgreSQL Oracle?
  • 68. Can Snapshots and Serialisability Coexist? Simple snapshots cannot prevent write skew abnormalities. But most transactions are
 not vulnerable to write skew anyway.
 (e.g., TPC-C is not vulnerable at all) The only two ways to prevent write skew
 abnormalities at the database are
 (1) putting locks when reading and
 (2) proofing the serialization graph is acyclic READ ONLY transactions are an important special case InnoDB
 SQL Server
 DB2 PostgreSQL
  • 69. Can Snapshots and Serialisability Coexist? PostgreSQL 9.1+ is the only(?) database that
 uses MVCC and detects serialization graph cycles.
 ➜ Use SERIALIZABLE and you are done
 In InnoDB (MySQL, MariaDB) and SQL Server, MVCC and Lock-Based SERIALIZABLE isolation can be
  • 70. How to use Snapshots and Prevent Write Skew Transaction type READ ONLY READ/WRITE DB2 LUW REPEATABLE READ
 (=SERIALIZABLE) REPEATABLE READ
 (=SERIALIZALBE) InnoDB
 (MySQL, MariaDB) REPEATABLE READ1 SERIALIZABLE PostgreSQL 9.1+ SERIALIZABLE SERIALIZABLE Oracle SERIALIZABLE SERIALIZABLE SQL Server 2005+ SNAPSHOT SERIALIZABLE 1: MySQL’s REPEATABLE READ also protects against phantom reads. KEY No shared locks
 No write skew issues Shared locks
 No write skew issues No shared locks
 Write skew issues
  • 71. SQL Server is Special Except SQL Server, all MVCC capable databases use it per default. In SQL Server, it must be enabled:
 ALTER DATABASE … SET allow_snapshot_isolation on; This will make write operations keep old versions
 (needs more space in permanent DB and tempdb) Then you can use SNAPSHOT isolation (e.g., in read-only transactions).
 Staying in SERIALISABLE for read/write transactions prevents write skew issues.
 Note: Hint remain effective: NOLOCK will still do dirty-read in SNAPSHOT isolation.
  • 72. Keep Transactions Focused Don’t select data that is irrelevant for the transaction.
 (an innocent looking function-call might query something you are not aware of) Do unrelated stuff in distinct transactions. Work fast.
 (Never, ever keep a transaction open waiting for someone)
 (In the code, but also not in your ad-hoc SQL session!) Tune your statements.
 (That makes your transactions shorter too)