溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Repeatable-Read及Read-Committed有哪些區(qū)別

發(fā)布時(shí)間:2021-11-06 09:02:48 來源:億速云 閱讀:250 作者:小新 欄目:MySQL數(shù)據(jù)庫(kù)

這篇文章主要為大家展示了“Repeatable-Read及Read-Committed有哪些區(qū)別”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Repeatable-Read及Read-Committed有哪些區(qū)別”這篇文章吧。

mysql 默認(rèn)提供的是 Repeatable-Read 可重復(fù)讀,更適用于oltp
Read-Committed 不可重復(fù)讀 也可以叫做提交讀
在MySQL中基本有這兩種事務(wù)隔離級(jí)別的設(shè)置,默認(rèn)的RR(Repeatable-Read)和實(shí)際中常見的RC(Read-Committed)。兩者區(qū)別是什么,怎么正確理解,用幾個(gè)SQL語句就能說明白,就用簡(jiǎn)單的實(shí)驗(yàn)來說明白。

   我們開始吧。    

   首先創(chuàng)建一個(gè)測(cè)試表test,插入一些數(shù)據(jù)。

create table test( id int primary key,name varchar(30),memo varchar(30));
insert into test values(1,'name1','aaaa'),(2,'name2','aaaa'),(3,'name3','aaaa'),(4,'name4','aaaa'),(5,'name5','aaaa');     很多情況下,我們會(huì)把隔離級(jí)別從默認(rèn)的RR修改為RC,這也是其它很多數(shù)據(jù)庫(kù)默認(rèn)的事務(wù)隔離級(jí)別。

我們打開兩個(gè)窗口,來對(duì)比關(guān)聯(lián)測(cè)試。


RC模式下的測(cè)試

1

窗口1

>show variables like 'tx_isolation';   
+---------------+----------------+
| Variable_name | Value          |
+---------------+----------------+
| tx_isolation  | READ-COMMITTED |
+---------------+----------------+
1 row in set (0.01 sec)

>begin;  --開啟事務(wù) 
>select *from test;  --查看數(shù)據(jù)
+----+-------+------+
| id | name  | memo |
+----+-------+------+
|  1 | name1 | aaaa |
|  2 | name2 | aaaa |
|  3 | name3 | aaaa |
|  4 | name4 | aaaa |
|  5 | name5 | aaaa |
+----+-------+------+
5 rows in set (0.00 sec)

2

窗口2

begin;  --開啟事務(wù)
>update test set name='aaaaa' where id=2;  --修改一條記錄
Query OK, 1 row affected (0.06 sec)
Rows matched: 1  Changed: 1  Warnings: 0
>commit;  --提交事務(wù)
Query OK, 0 rows affected (0.01 sec)

1

窗口1

>select *from test;   --查看窗口1中的數(shù)據(jù),就會(huì)發(fā)現(xiàn)原來窗口的數(shù)據(jù)發(fā)生了變化,這是不可重復(fù)讀的一個(gè)典型例子。
+----+-------+------+
| id | name  | memo |
+----+-------+------+
|  1 | name1 | aaaa |
|  2 | aaaaa | aaaa |
|  3 | name3 | aaaa |
|  4 | name4 | aaaa |
|  5 | name5 | aaaa |
+----+-------+------+
5 rows in set (0.00 sec)

RR模式下的測(cè)試

再來看看RR這個(gè)隔離級(jí)別,其實(shí)有了上面的測(cè)試,就相對(duì)有底了。這是MySQL默認(rèn)的隔離級(jí)別,會(huì)出現(xiàn)幻讀的情況。

1

窗口1

首先修改隔離級(jí)別從RC到RR

>set global transaction isolation level repeatable read; 
Query OK, 0 rows affected (0.00 sec)
?查看事務(wù)隔離級(jí)別。
>show variables like 'tx_isolation';
+---------------+-----------------+
| Variable_name | Value           |
+---------------+-----------------+
| tx_isolation  | REPEATABLE-READ |
+---------------+-----------------+
1 row in set (0.00 sec)

>begin;   --開啟事務(wù)
>select *from test;   --查看表test的數(shù)據(jù)。
+----+-------+------+
| id | name  | memo |
+----+-------+------+
|  1 | name1 | aaaa |
|  2 | aaaaa | aaaa |
|  3 | name3 | aaaa |
|  4 | name4 | aaaa |
|  5 | name5 | aaaa |
+----+-------+------+
5 rows in set (0.00 sec)

2

窗口2

>begin;  --開啟事務(wù)
>update test set name='RR_test';  --修改表test的數(shù)據(jù),所有記錄都發(fā)生變化。
Query OK, 5 rows affected (0.01 sec)
Rows matched: 5  Changed: 5  Warnings: 0
>commit;  --提交事務(wù)
Query OK, 0 rows affected (0.00 sec)

1

窗口1


>select *from test;  --在RR模式下,窗口1中的事務(wù)因?yàn)檫€沒有提交,看到的還是原來的數(shù)據(jù)。
+----+-------+------+
| id | name  | memo |
+----+-------+------+
|  1 | name1 | aaaa |
|  2 | aaaaa | aaaa |
|  3 | name3 | aaaa |
|  4 | name4 | aaaa |
|  5 | name5 | aaaa |
+----+-------+------+
5 rows in set (0.00 sec)
>commit;  --我們提交窗口1的事務(wù)
Query OK, 0 rows affected (0.00 sec)
>select *from test;  --再次查看數(shù)據(jù)就發(fā)生了變化,實(shí)際上窗口1中沒有任何的DMl操作。
+----+---------+------+
| id | name    | memo |
+----+---------+------+
|  1 | RR_test | aaaa |
|  2 | RR_test | aaaa |
|  3 | RR_test | aaaa |
|  4 | RR_test | aaaa |
|  5 | RR_test | aaaa |
+----+---------+------+
5 rows in set (0.00 sec)

以上是“Repeatable-Read及Read-Committed有哪些區(qū)別”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI