Read phenomena (dirty reads, phantom reads) in SQL - Time & Space Complexity
When databases read data during transactions, the way they handle changes affects how long queries take.
We want to understand how reading data that might be changing impacts the work done by the database.
Analyze the time complexity of this transaction reading data with possible dirty or phantom reads.
BEGIN TRANSACTION;
SELECT * FROM Orders WHERE CustomerID = 123;
-- Another transaction may insert or update Orders here
SELECT COUNT(*) FROM Orders WHERE CustomerID = 123;
COMMIT;
This code reads orders for a customer, but other changes might happen during the transaction causing dirty or phantom reads.
Look for repeated work that affects time.
- Primary operation: Scanning or searching the Orders table for matching CustomerID rows.
- How many times: Twice in this example, once for the SELECT * and once for the COUNT(*).
As the number of orders grows, the database must check more rows each time it reads.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 row checks (2 reads x 10 rows) |
| 100 | About 200 row checks |
| 1000 | About 2000 row checks |
Pattern observation: The work roughly doubles with the number of rows because the query runs twice, each scanning the data.
Time Complexity: O(n)
This means the time to read grows directly with the number of rows matching the query.
[X] Wrong: "Reading data during a transaction always takes the same time regardless of data changes."
[OK] Correct: If other transactions add or change rows, the database may do extra work to handle these reads, making the time grow with data size and changes.
Understanding how reading data during transactions affects performance shows you know how databases handle real-world data changes.
"What if we added an index on CustomerID? How would that change the time complexity of these reads?"
Practice
dirty read in database transactions?Solution
Step 1: Understand dirty read definition
A dirty read happens when a transaction reads data that another transaction has changed but not committed yet.Step 2: Compare options to definition
Reading data that has been modified but not yet committed by another transaction. matches this exactly. Seeing new rows appear in a query result after re-running the same query. describes phantom reads, C describes a safe read, and D is about locking, not dirty reads.Final Answer:
Reading data that has been modified but not yet committed by another transaction. -> Option AQuick Check:
Dirty read = reading uncommitted data [OK]
- Confusing dirty read with phantom read
- Thinking dirty read only reads committed data
- Mixing locking with read phenomena
dirty reads?Solution
Step 1: Recall isolation levels and dirty reads
Only the READ UNCOMMITTED isolation level allows dirty reads, meaning it can read uncommitted changes.Step 2: Match options to isolation levels
READ COMMITTED and higher levels prevent dirty reads. SERIALIZABLE is the strictest. So, READ UNCOMMITTED is correct.Final Answer:
READ UNCOMMITTED -> Option DQuick Check:
Dirty reads allowed only in READ UNCOMMITTED [OK]
- Choosing READ COMMITTED as allowing dirty reads
- Confusing phantom reads with dirty reads
- Not knowing isolation level hierarchy
Orders. Transaction 1 reads all rows where status = 'pending'. Transaction 2 inserts a new row with status = 'pending' and commits. If Transaction 1 repeats the read, what phenomenon might it observe?Solution
Step 1: Analyze the scenario with inserted rows
Transaction 1 reads rows with status 'pending'. Transaction 2 inserts a new 'pending' row and commits.Step 2: Identify the phenomenon on repeated read
When Transaction 1 reads again, it sees the new row that wasn't there before. This is a phantom read, where new rows appear between reads.Final Answer:
Phantom read -> Option CQuick Check:
New rows appearing = Phantom read [OK]
- Confusing phantom read with dirty read
- Thinking non-repeatable read applies to new rows
- Mixing lost update with read phenomena
BEGIN TRANSACTION;
SELECT * FROM Products WHERE price > 100;
-- Another transaction inserts a new product with price 150 and commits
SELECT * FROM Products WHERE price > 100;
COMMIT;
What is the likely cause?
Solution
Step 1: Understand the scenario with repeated SELECTs
The transaction reads products with price > 100 twice. Between reads, another transaction inserts a new matching row and commits.Step 2: Identify the read phenomenon
Because the isolation level is likely READ COMMITTED, the second read sees the new row, causing a phantom read.Final Answer:
Using READ COMMITTED isolation level causing phantom reads -> Option BQuick Check:
Phantom reads occur at READ COMMITTED [OK]
- Assuming dirty reads cause this behavior
- Confusing lost updates with read phenomena
- Thinking REPEATABLE READ allows phantom reads
Solution
Step 1: Review isolation levels and phenomena prevented
READ UNCOMMITTED allows dirty reads. READ COMMITTED prevents dirty reads but allows phantom reads. REPEATABLE READ prevents dirty and non-repeatable reads but may allow phantom reads.Step 2: Identify level preventing both dirty and phantom reads
SERIALIZABLE is the strictest level and prevents dirty reads, non-repeatable reads, and phantom reads by fully isolating transactions.Final Answer:
SERIALIZABLE -> Option AQuick Check:
SERIALIZABLE prevents dirty and phantom reads [OK]
- Choosing REPEATABLE READ thinking it blocks phantom reads
- Confusing READ COMMITTED with preventing all read anomalies
- Ignoring performance impact of SERIALIZABLE
Ready to prove it?
Pass the test and this pattern moves unknown
