2018年12月29日 星期六

non-repeatable read 和 phantom read

在 PostgreSQL 的文件中,描述到 SQL 標準對於錯誤的描述,分別有以下的敘述在描述 non-repeatable read 和 phantom read:

nonrepeatable read
A transaction re-reads data it has previously read and finds that data has been modified by another transaction (that committed since the initial read).

phantom read
A transaction re-executes a query returning a set of rows that satisfy a search condition and finds that the set of rows satisfying the condition has changed due to another recently-committed transaction.

本來對於兩者的差別感覺不是非常明顯,因此查了一下。看了 wikipedia 的描述以後,發現其實也蠻好懂的:

A non-repeatable read occurs, when during the course of a transaction, a row is retrieved twice and the values within the row differ between reads.

A phantom read occurs when, in the course of a transaction, two identical queries are executed, and the collection of rows returned by the second query is different from the first.

也就是說,當在交易中連續讀取兩次的時候,non-repeatable read 指的是讀取到的資料列的內容不一樣,而 phantom read 指的是讀取到的資料列不一樣,例如第一次讀到 #1、#2、#3,第二次讀到 #1、#4,每個資料列的內容都一樣,但符合條件的資料列們不同。(這段描述總覺得好難用中文寫 XD)

參考資料
  1. What is the difference between Non-Repeatable Read and Phantom Read?
  2. Wikipedia: Isolation (database systems)

沒有留言: