Database Basics
Transaction T1 ran SELECT COUNT(*) FROM orders WHERE amount >= 1000 and got 8. Immediately afterwards, T2 inserted an order row with an amount of 2000 and committed. When T1 ran the same SELECT statement again, it got 9. What is this phenomenon?
- ANon-repeatable read
- BDirty read
- CPhantom read
Show answer and explanation
Answer: C. Phantom read
When a search with the same condition is repeated within one transaction and the set of result rows changes because another transaction added (or deleted) matching rows and committed in between, the phenomenon is called a phantom read (or phantom). The name comes from the newly appearing rows being like ghosts (phantoms). A non-repeatable read is when a row already read has a different value (or has disappeared) on rereading, because another transaction updated or deleted it and committed. Locks on existing rows cannot prevent rows that do not yet exist from being added, so preventing phantoms requires mechanisms such as locking the range covered by the search condition. Under the SQL standard, phantoms do not occur at the SERIALIZABLE isolation level. T2 committed, so this is not a dirty read.
- A. A non-repeatable read is when the value of a row already read changes because of another transaction's update.
- B. T2 committed, so this is not a dirty read, which reads data before it is committed.
- C. Correct. Rows added by another transaction newly appear in a search with the same condition.