liveBook · Manning

Repeatable read isolation level

As explained in 100 SQL Server Mistakes and How to Avoid Them

Repeatable Read is a transaction isolation level that prevents dirty and nonrepeatable reads but can allow phantom reads.

Repeatable Read prevents dirty reads and nonrepeatable reads: rows read once cannot change before the transaction ends. It can still allow phantom reads, where a repeated query returns a different number of rows. section 10.11

Mistake: choosing Repeatable Read as the default because the data feels important.

  • Repeatable Read is valid when requirements demand it, such as actuarial calculations, but it is often excessive for conventional workloads. section 10.11
  • Stronger isolation can increase lock contention and deadlocks; begin with Read Committed unless the use case requires stronger guarantees. section 10.11 section 10.14
  • Consider optimistic isolation levels such as Read Committed Snapshot or Snapshot when lock contention causes performance problems. section 10.14

A reporting transaction and a data steward

Use two query windows against ImpressionsArchive. A reporting transaction counts rows matching an operating system, while a data steward deletes three matching rows and commits. Repeating the report can return a different count: Repeatable Read protects previously read rows but does not provide Serializable protection against phantoms. section 10.11

Listing 10.27 Beginning the reporting user’s transaction
BEGIN TRANSACTION                              #1
 
SELECT COUNT(*)
FROM dbo.ImpressionsArchive
WHERE OperatingSystemID = (
    SELECT OperatingSystemID
    FROM dbo.ImpressionsArchive
    WHERE ImpressionID = 100
) ;

Start the reporting transaction, then inspect the paired data-steward deletion and final count queries. section 10.11

How 100 SQL Server Mistakes and How to Avoid Them covers it

How deep it goes
Explains dirty, nonrepeatable, and phantom reads through concurrent transactions, but stops short of implementation internals.
Examples
Demonstrates reporting queries and a data steward's updates or deletes against ImpressionsArchive in two query windows.
What it assumes
Assumes basic transactions and Read Committed, while defining dirty, nonrepeatable, and phantom reads through examples.
What you can do after
Evaluate whether Repeatable Read is necessary, then choose a weaker or stronger isolation level according to consistency and contention requirements.
How it connects
Connects Read Committed, Serializable, dirty reads, nonrepeatable reads, phantom reads, lock contention, deadlocks, and optimistic isolation.

Also explained in

Compare how 2 books explain repeatable read isolation level →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 SQL Server Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.