您好,登錄后才能下訂單哦!
關于SQL的隔離級別
SQL標準定義了4類隔離級別,如下所示:
1. Read Uncommitted (讀取未提交內容)
在該隔離級別,所有事務都可以看到其他未提交事務的執行結果。本隔離級別很少用于實際應用,因為它的性能也不比其他級別好多少。讀取未提交的數據,也被稱之為臟讀(Dirty Read)
2. Read Committed (讀取提交內容)
這是大多數數據庫系統的默認隔離級別(但不是MySQL默認的)。它滿足了隔離的簡單定義:一個事務只能看見已經提交事務所做的改變。這種隔離級別 也支持所謂的不可重復讀(Nonrepeatable Read),因為同一事務的其他實例在該實例處理其間可能會有新的commit,所以同一select可能返回不同結果。
3. Repeatable Read (可重復讀)
這是MySQL的默認事務隔離級別,它確保同一事務的多個實例在并發讀取數據時,會看到同樣的數據行。不過理論上,這會導致另一個棘手的問題:幻讀 (Phantom Read)。簡單的說,幻讀指當用戶讀取某一范圍的數據行時,另一個事務又在該范圍內插入了新行,當用戶再讀取該范圍的數據行時,會發現有新的“幻影” 行。InnoDB和Falcon存儲引擎通過多版本并發控制(MVCC,Multiversion Concurrency Control)機制解決了該問題。
4. Serializable (可串行化)
這是最高的隔離級別,它通過強制事務排序,使之不可能相互沖突,從而解決幻讀問題。簡言之,它是在每個讀的數據行上加上共享鎖。在這個級別,可能導致大量的超時現象和鎖競爭。
在Mariadb中,這4種隔離級別有可能產生的問題如下圖所示:
相關實驗
下面分別針對不同的隔離狀態進行測試:
準備的環境如下所示:
在Mariadb服務器端,創建一個數據庫名為hldb,創建一個INNODB引擎的數據表名為test,并提前插入兩條數據
準備兩個MySQL客戶端線程,連接到服務器上面:
MariaDB [hldb]> select connection_id(); +-----------------+ | connection_id() | +-----------------+ | 5 | +-----------------+ MariaDB [hldb]> select connection_id(); +-----------------+ | connection_id() | +-----------------+ | 6 | +-----------------+
1. Read uncommitted(讀未提交)
首先將兩個客戶端的隔離級別分別都設置為Read uncommitted模式:
MariaDB [hldb]> select connection_id(); +-----------------+ | connection_id() | +-----------------+ | 5 | +-----------------+ 1 row in set (0.00 sec) MariaDB [hldb]> set @@session.tx_isolation='read-uncommitted'; Query OK, 0 rows affected (0.00 sec) MariaDB [hldb]> select @@session.tx_isolation; +------------------------+ | @@session.tx_isolation | +------------------------+ | READ-UNCOMMITTED | +------------------------+ 1 row in set (0.00 sec) ...... ...... MariaDB [hldb]> select connection_id(); +-----------------+ | connection_id() | +-----------------+ | 6 | +-----------------+ 1 row in set (0.00 sec) MariaDB [hldb]> set @@session.tx_isolation='read-uncommitted'; Query OK, 0 rows affected (0.00 sec) MariaDB [hldb]> select @@session.tx_isolation; +------------------------+ | @@session.tx_isolation | +------------------------+ | READ-UNCOMMITTED | +------------------------+ 1 row in set (0.00 sec)
第一步,在再在id=6的客戶端上面(以后簡稱為id6)上面完成一次查詢。第二步,在兩個客戶端上面同時打開start transaction。第三步,兩個客戶端都打開了start transaction之后,再在id=5的客戶端上面(以后簡稱為id5)上面插入一條數據。第四步,在id5自身的會話上面再用select進行查詢。第五步,在id6會話上面用select進行查詢。
在id5上面看到的結果如下:
MariaDB [hldb]> start transaction; Query OK, 0 rows affected (0.00 sec) MariaDB [hldb]> select connection_id(); +-----------------+ | connection_id() | +-----------------+ | 5 | +-----------------+ 1 row in set (0.00 sec) MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 1 | a | | 21 | b | +----+------+ 2 rows in set (0.00 sec) MariaDB [hldb]> insert into test(nm) values('c'); Query OK, 1 row affected (0.00 sec) MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 1 | a | | 21 | b | | 22 | c | +----+------+ 3 rows in set (0.00 sec)
在id6上面看到的結果如下:
MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 1 | a | | 21 | b | +----+------+ 2 rows in set (0.00 sec) MariaDB [hldb]> start transaction; Query OK, 0 rows affected (0.00 sec) MariaDB [hldb]> select connection_id(); +-----------------+ | connection_id() | +-----------------+ | 6 | +-----------------+ 1 row in set (0.00 sec) MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 1 | a | | 21 | b | | 22 | c | +----+------+ 3 rows in set (0.00 sec)
結論:
可以看到,如果事務級別設定成為Read Uncommitted(讀未提交),在id5的事務并未提交的狀態下,id6的事務是可以將其未提交的事務查詢到的。這種能夠讀取到未提交事務的現象,稱為臟讀
2. Read Committed (讀取提交內容)
將兩個客戶端的事務隔離級別均設定為Read Committed,并且查詢一下test數據表里面的內容
MariaDB [hldb]> set @@session.tx_isolation='read-committed'; Query OK, 0 rows affected (0.00 sec) MariaDB [hldb]> select connection_id(); +-----------------+ | connection_id() | +-----------------+ | 5 | +-----------------+ 1 row in set (0.00 sec) MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 1 | a | | 21 | b | | 22 | c | +----+------+ 3 rows in set (0.00 sec) ...... ...... MariaDB [hldb]> set @@session.tx_isolation='read-committed'; Query OK, 0 rows affected (0.00 sec) MariaDB [hldb]> select connection_id(); +-----------------+ | connection_id() | +-----------------+ | 6 | +-----------------+ 1 row in set (0.00 sec) MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 1 | a | | 21 | b | | 22 | c | +----+------+ 3 rows in set (0.00 sec)
第一步,在兩個客戶端上面開啟start transaction。第二步,在id5上面添加一條記錄,同時在id5, id6上面查詢。第三步,在id5上面刪除一條記錄,同時在id5, id6上面查詢。第四步,在id5上面提交事務,并且在id5, id6上面查詢。
在id5客戶端上面的操作結果如下所示:
第一步: MariaDB [hldb]> start transaction; Query OK, 0 rows affected (0.00 sec) 第二步: MariaDB [hldb]> insert into test(nm) values('d'); Query OK, 1 row affected (0.00 sec) MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 1 | a | | 21 | b | | 22 | c | | 23 | d | +----+------+ 4 rows in set (0.00 sec) 第三步: MariaDB [hldb]> delete from test where nm='b'; Query OK, 1 row affected (0.00 sec) MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 1 | a | | 22 | c | | 23 | d | +----+------+ 3 rows in set (0.00 sec) 第四步: MariaDB [hldb]> commit; Query OK, 0 rows affected (0.00 sec) MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 1 | a | | 22 | c | | 23 | d | +----+------+ 3 rows in set (0.00 sec)
在id6客戶端上面的操作如下所示:
第一步: MariaDB [hldb]> start transaction; Query OK, 0 rows affected (0.00 sec) 第二步: MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 1 | a | | 21 | b | | 22 | c | +----+------+ 3 rows in set (0.00 sec) 第三步: MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 1 | a | | 21 | b | | 22 | c | +----+------+ 3 rows in set (0.00 sec) 第四步: MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 1 | a | | 22 | c | | 23 | d | +----+------+ 3 rows in set (0.01 sec)
結論:
在Read Committed(讀取提交內容)的隔離級別下,解決了臟讀現象,但是帶來了另外一種現象:不可重復讀。id5事務提交的前后,id6在同一個事務中,所查詢到的內容不一致。
3. Repeatable Read(可重復讀)
首先將兩個客戶端的事務隔離級別都設置為Repeatable Read,并查詢test數據表里面的內容:
MariaDB [hldb]> set @@session.tx_isolation='repeatable-read'; Query OK, 0 rows affected (0.00 sec) MariaDB [hldb]> select connection_id(); +-----------------+ | connection_id() | +-----------------+ | 5 | +-----------------+ 1 row in set (0.00 sec) MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 1 | a | +----+------+ 1 row in set (0.00 sec) ...... ...... MariaDB [hldb]> set @@session.tx_isolation='repeatable-read'; Query OK, 0 rows affected (0.00 sec) MariaDB [hldb]> select connection_id(); +-----------------+ | connection_id() | +-----------------+ | 6 | +-----------------+ 1 row in set (0.00 sec) MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 1 | a | +----+------+ 1 row in set (0.00 sec)
第一步,在兩個客戶端上面開啟start transaction。第二步,在id5上面添加一條記錄,同時在id5, id6上面查詢。第三步,在id5上面刪除一條記錄,同時在id5, id6上面查詢。第四步,在id5上面提交事務,并且在id5, id6上面查詢。第五步,在id6上面提交事務,并且在id6上面查詢。
在id5客戶端上面的操作結果如下所示:
第一步: MariaDB [hldb]> start transaction; Query OK, 0 rows affected (0.00 sec) 第二步: MariaDB [hldb]> insert into test(nm) values('b'); Query OK, 1 row affected (0.00 sec) MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 1 | a | | 25 | b | +----+------+ 2 rows in set (0.00 sec) 第三步: MariaDB [hldb]> delete from test where id=1; Query OK, 1 row affected (0.00 sec) MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 25 | b | +----+------+ 1 row in set (0.00 sec) 第四步: MariaDB [hldb]> commit; Query OK, 0 rows affected (0.01 sec) MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 25 | b | +----+------+ 1 row in set (0.00 sec)
在id6上面的操作結果如下所示:
第一步: MariaDB [hldb]> start transaction; Query OK, 0 rows affected (0.00 sec) 第二步: MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 1 | a | +----+------+ 1 row in set (0.00 sec) 第三步: MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 1 | a | +----+------+ 1 row in set (0.01 sec) 第四步: MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 1 | a | +----+------+ 1 row in set (0.00 sec) 第五步: MariaDB [hldb]> commit; Query OK, 0 rows affected (0.00 sec) MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 25 | b | +----+------+ 1 row in set (0.00 sec)
結論:
由上述實驗可以看出,事務隔離級別Repeatable read(可重復讀)和Read Committed(已提交讀)的不同之處在于,在同一事務環境下,前后兩次讀取的內容是一致的,而不受其他事務是否提交的影響。
注:
在Repeatable read(可重復讀)的條件下,有可能會出現Phantom Read(幻讀)現象。該現象可以通過模擬update來實現:
第一步,客戶端兩邊都開啟start transaction。第二步,在id6上面新增一個字段,并在id5和id6上面分別查詢。第三步,在id6上面提交,并在id5和id6上面分別查詢。第四步,在id5上面對所有行的nm字段進行更新,并在id5和id6上面分別查詢。
id6上面的結果如下所示:
第一步: MariaDB [hldb]> start transaction; Query OK, 0 rows affected (0.00 sec) MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 1 | a | +----+------+ 1 row in set (0.00 sec) 第二步: MariaDB [hldb]> insert into test(nm) values('b'); Query OK, 1 row affected (0.00 sec) MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 1 | a | | 2 | b | +----+------+ 2 rows in set (0.00 sec) 第三步: MariaDB [hldb]> commit; Query OK, 0 rows affected (0.00 sec) MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 1 | a | | 2 | b | +----+------+ 2 rows in set (0.00 sec)
在id5上面的操作如下所示:
第一步: MariaDB [hldb]> start transaction; Query OK, 0 rows affected (0.00 sec) MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 1 | a | +----+------+ 1 row in set (0.00 sec) 第二步: MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 1 | a | +----+------+ 1 row in set (0.00 sec) 第三步: MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 1 | a | +----+------+ 1 row in set (0.00 sec) 第四步: MariaDB [hldb]> update test set nm='c'; Query OK, 2 rows affected (0.00 sec) Rows matched: 2 Changed: 2 Warnings: 0 MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 1 | c | | 2 | c | +----+------+ 2 rows in set (0.00 sec)
可以看到,通過更新命令,在未結束的事務里面竟然也讀到了“新的數據”,這便是一種Phantom Read(幻讀)。
4. Serializable(可序列化)
首先將兩個客戶端的事務隔離級別都設置為Serializable,并查詢test數據表里面的內容:
MariaDB [hldb]> set @@session.tx_isolation='serializable'; Query OK, 0 rows affected (0.00 sec) MariaDB [hldb]> select connection_id(); +-----------------+ | connection_id() | +-----------------+ | 5 | +-----------------+ 1 row in set (0.00 sec) MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 26 | a | | 27 | b | +----+------+ 2 rows in set (0.00 sec) ...... ...... MariaDB [hldb]> set @@session.tx_isolation='serializable'; Query OK, 0 rows affected (0.00 sec) MariaDB [hldb]> select connection_id(); +-----------------+ | connection_id() | +-----------------+ | 6 | +-----------------+ 1 row in set (0.00 sec) MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 26 | a | | 27 | b | +----+------+ 2 rows in set (0.00 sec)
第一步,在兩個客戶端上面開啟start transaction。第二步,在id5上面添加一條記錄,同時在id5, id6上查詢
在id5上面查詢的結果如下:
MariaDB [hldb]> start transaction; Query OK, 0 rows affected (0.00 sec) MariaDB [hldb]> insert into test(nm) values('c'); Query OK, 1 row affected (0.00 sec) MariaDB [hldb]> select * from test; +----+------+ | id | nm | +----+------+ | 26 | a | | 27 | b | | 28 | c | +----+------+ 3 rows in set (0.00 sec)
在id6上面查詢的結果如下:
MariaDB [hldb]> start transaction; Query OK, 0 rows affected (0.00 sec) MariaDB [hldb]> select * from test; ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
由上述結果可以看到,在id5未提交事務之前,即使是select查詢操作,在其他事務里面也是不允許執行的,只有等待id5提交事務之后,其他事務才可以進行更新或者修改的操作。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。