Isolation Levels with examples in SQL Server
Isolation Levels with examples in SQL Server
In SQL Server, transaction isolation levels determine how transaction integrity is handled when multiple
transactions are executed concurrently. Isolation levels control the visibility of changes made by one transaction
to other concurrent transactions. They provide a balance between data consistency and concurrency, allowing
developers to choose how much interaction should be allowed between transactions.
1. Read Uncommitted
2. Read Committed
3. Repeatable Read
4. Serializable
5. Snapshot
Each of these isolation levels protects against certain types of concurrency issues, such as dirty reads,
non-repeatable reads, and phantom reads.
1. Read Uncommitted
This is the lowest isolation level and offers the highest level of concurrency but the least protection against data
inconsistencies.
https://www.sqldbachamps.com
● Key Characteristics:
○ Transactions can read data that has been modified by other transactions but not yet committed
(i.e., uncommitted changes or dirty reads).
○ No shared locks are acquired, and no exclusive locks are honored, which means transactions can
read data that is being modified by others.
○ It provides the least overhead but at the risk of inconsistent data.
● Concurrency Problems: Dirty reads, non-repeatable reads, and phantom reads can all occur.
● Example:
BEGIN TRANSACTION;
SELECT * FROM Orders; -- This can read uncommitted changes from other transactions.
COMMIT;
● Here, the query may read uncommitted data (dirty reads) from other transactions that may later be rolled
back.
https://www.sqldbachamps.com Praveen Madupu +91 98661 30093
Sr SQL Server DBA, Dubai
[email protected]
2. Read Committed
This is the default isolation level in SQL Server. It prevents dirty reads by ensuring that transactions can only read
data that has been committed.
● Key Characteristics:
○ It prevents dirty reads but allows non-repeatable reads and phantom reads.
○ When a transaction reads data, it acquires a shared lock, but it releases it immediately after
reading. This ensures that no dirty data is read, but other transactions can modify data between
reads in the same transaction.
● Concurrency Problems: Non-repeatable reads and phantom reads can occur.
● Example:
BEGIN TRANSACTION;
https://www.sqldbachamps.com
●
COMMIT;
In this example, the query ensures that only committed data is read, but if another transaction modifies the
data after it has been read and before the transaction ends, a subsequent read will reflect the changes
(non-repeatable read).
https://www.sqldbachamps.com Praveen Madupu +91 98661 30093
Sr SQL Server DBA, Dubai
[email protected]
3. Repeatable Read
This isolation level ensures that if a transaction reads data, no other transactions can modify that data until the
first transaction completes.
● Key Characteristics:
○ It prevents dirty reads and non-repeatable reads.
○ Once a transaction reads a row, it holds a lock on that row, preventing other transactions from
updating or deleting it until the first transaction completes.
○ However, it still allows phantom reads, where new rows can be inserted by other transactions,
and they will appear if the same query is run again within the same transaction.
● Concurrency Problems: Phantom reads can occur.
● Example:
BEGIN TRANSACTION;
https://www.sqldbachamps.com
-- Another transaction cannot update or delete rows with CustomerID = 1 until the transaction completes.
COMMIT;
● In this example, the rows that have been read are locked, preventing other transactions from changing
those rows until the transaction is complete. However, new rows that satisfy the query condition can still
be inserted (phantom reads).
https://www.sqldbachamps.com Praveen Madupu +91 98661 30093
Sr SQL Server DBA, Dubai
[email protected]
4. Serializable
This is the strictest isolation level, where the transaction behaves as though it is the only one running in the
system, serializing access to data.
● Key Characteristics:
○ It prevents dirty reads, non-repeatable reads, and phantom reads.
○ The transaction acquires range locks on the data set, which prevents any other transaction from
inserting, updating, or deleting rows in the range until the transaction completes.
○ It has the most overhead and reduces concurrency the most because it locks both the data that
has been read and any possible data that might be read (by locking ranges).
● Concurrency Problems: None (full isolation).
● Example:
BEGIN TRANSACTION;
https://www.sqldbachamps.com
SELECT * FROM Orders WHERE CustomerID = 1; -- Locks the range, preventing inserts, updates, and deletes.
COMMIT;
● Here, not only are the rows with CustomerID = 1 locked, but new rows that would satisfy this condition
cannot be inserted by other transactions, ensuring full isolation.
https://www.sqldbachamps.com Praveen Madupu +91 98661 30093
Sr SQL Server DBA, Dubai
[email protected]
5. Snapshot
Snapshot isolation provides a versioning mechanism that allows transactions to work with a consistent view of
the data as it was when the transaction started, even if other transactions are making changes.
● Key Characteristics:
○ It prevents dirty reads, non-repeatable reads, and phantom reads.
○ Instead of locking rows, SQL Server uses row versioning to maintain a copy of the data as it
existed at the start of the transaction. Any changes made by other transactions are not visible to
the transaction.
○ It is useful in high-concurrency systems because it avoids locking but still ensures consistency.
● Concurrency Problems: None (for the reading transaction), but write-write conflicts can occur if two
transactions attempt to modify the same data.
● Example:
BEGIN TRANSACTION;
https://www.sqldbachamps.com
SELECT * FROM Orders WHERE CustomerID = 1;
COMMIT;
In this example, the query sees the state of the data as it was at the beginning of the transaction.
Even if other transactions modify the data during the transaction, the snapshot isolation ensures that those
changes are not visible to this transaction.
1. Dirty Read: A transaction reads data that has been modified by another transaction but not yet
committed. If the second transaction is rolled back, the first transaction has read invalid data.
2. Non-Repeatable Read: A transaction reads the same row twice and gets different data because another
transaction has modified the data between reads.
3. Phantom Read: A transaction reads a set of rows based on a condition, but when the query is executed
again within the same transaction, new rows that satisfy the condition appear because another transaction
https://www.sqldbachamps.com
inserted them.
Summary
Choosing the right isolation level in SQL Server is crucial for balancing performance and data consistency.
Lower isolation levels like Read Uncommitted provide better performance and higher concurrency but sacrifice
data integrity, while higher isolation levels like Serializable ensure full consistency at the cost of performance and
reduced concurrency.