欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mysql InnoDB的鎖定機(jī)制實(shí)例詳解

 更新時(shí)間:2021年01月06日 09:22:31   作者:tfzh  
這篇文章主要給大家介紹了關(guān)于Mysql InnoDB的鎖定機(jī)制,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

1.InnoDB的鎖定機(jī)制

InnoDB存儲引擎支持行級鎖,支持事務(wù)處理,事務(wù)是有一組SQL語句組成的邏輯處理單元,他的ACID特性如下:

  • 原子性(Atomicity): 事務(wù)具有原子不可分割的特性,要么一起執(zhí)行,要么都不執(zhí)行。
  • 一致性(Consistency): 在事務(wù)開始和事務(wù)結(jié)束時(shí),數(shù)據(jù)都保持一致狀態(tài)。
  • 隔離性(Isolation): 在事務(wù)開始和結(jié)束過程中,事務(wù)保持著一定的隔離特性,保證事務(wù)不受外部并發(fā)數(shù)據(jù)操作的影響。
  • 持久性(Durability): 在事務(wù)完成后,數(shù)據(jù)將會被持久化到數(shù)據(jù)庫中。

并發(fā)事務(wù)能提高數(shù)據(jù)庫資源的利用率,提高了數(shù)據(jù)庫的事務(wù)吞吐量,但并發(fā)事務(wù)也存在一些問題,主要包括:

  • 更新丟失(Lost Update): 兩個(gè)事務(wù)更新同一條數(shù)據(jù),但第二個(gè)事務(wù)中途失敗退出,導(dǎo)致兩個(gè)修改都失效了;因?yàn)榇藭r(shí)數(shù)據(jù)庫沒有執(zhí)行任何鎖操作,并發(fā)事務(wù)并沒有被隔離。(現(xiàn)代數(shù)據(jù)庫已經(jīng)不存在這種問題)
  • 臟讀(Dirty Reads): 一個(gè)事務(wù)讀了某行數(shù)據(jù),但是另一個(gè)事務(wù)已經(jīng)更新了這行數(shù)據(jù),這是非常危險(xiǎn)的,很可能導(dǎo)致所有的操作被回滾。
  • 不可重復(fù)讀: 一個(gè)事務(wù)對一行數(shù)據(jù)重復(fù)讀取兩次(多次),可是得到了不同的結(jié)果,在兩次讀取過程中,有可能存在另一個(gè)事務(wù)對數(shù)據(jù)進(jìn)行了修改。
  • 幻讀:事務(wù)在操作過程中進(jìn)行兩次查詢,第二次查詢結(jié)果包含了第一次沒有出現(xiàn)的數(shù)據(jù)。出現(xiàn)幻讀的主要原因是兩次查詢過程中另一個(gè)事務(wù)插入新的數(shù)據(jù)。

數(shù)據(jù)庫并發(fā)中的“更新丟失”通常應(yīng)該是完全避免的,但防止更新丟失數(shù)據(jù),并不能單靠數(shù)據(jù)庫事務(wù)控制來解決,需要應(yīng)用程序?qū)σ碌臄?shù)據(jù)加必要的鎖來解決,而以上出現(xiàn)的數(shù)據(jù)庫問題都必要由數(shù)據(jù)庫提供一定的事務(wù)隔離機(jī)制來解決。為了避免數(shù)據(jù)庫事務(wù)并發(fā)帶來的問題,在標(biāo)準(zhǔn)SQL規(guī)范中定義了4個(gè)事務(wù)的隔離級別,不同的隔離級別對事務(wù)處理不一樣。

數(shù)據(jù)庫隔離級別的比較

隔離級別 讀數(shù)據(jù)一致性 臟讀 不可重復(fù)讀 幻讀
未提交讀
(Read uncommitted)
最低級別,只能保證不讀取物理上損壞的數(shù)據(jù)
已提交讀
(Read committed)
語句級
可重復(fù)讀
(Repeatable read)
事務(wù)級
可序列化
(Serializable)
最高級別,事務(wù)級

InnoDB存儲引擎實(shí)現(xiàn)了4中行鎖,分別時(shí)共享鎖(S)、排他鎖(X)、意向共享鎖(IS)、意向排他鎖(IX)。

  • 共享鎖:大家都能讀,但是不能改,只有其中一個(gè)獨(dú)占共享鎖時(shí)候才能改;
  • 排它鎖:我要改,你們都不能改,也不能讀(但可以MVCC快照讀)

理解意向鎖

意向鎖不會和行級的S和X鎖沖突,只會和表級的S和X鎖沖突

意向鎖是為了避免遍歷全部行鎖

考慮這個(gè)例子:

事務(wù)A鎖住了表中的一行,讓這一行只能讀,不能寫。

之后,事務(wù)B申請整個(gè)表的寫鎖。

如果事務(wù)B申請成功,那么理論上它就能修改表中的任意一行,這與A持有的行鎖是沖突的。

數(shù)據(jù)庫需要避免這種沖突,就是說要讓B的申請被阻塞,直到A釋放了行鎖。

數(shù)據(jù)庫要怎么判斷這個(gè)沖突呢?

step1:判斷表是否已被其他事務(wù)用表鎖鎖表

step2:判斷表中的每一行是否已被行鎖鎖住。

注意step2,這樣的判斷方法效率實(shí)在不高,因?yàn)樾枰闅v整個(gè)表。

于是就有了意向鎖。

在意向鎖存在的情況下,事務(wù)A必須先申請表的意向共享鎖,成功后再申請一行的行鎖。

在意向鎖存在的情況下,上面的判斷可以改成

step1:不變

step2:發(fā)現(xiàn)表上有意向共享鎖,說明表中有些行被共享行鎖鎖住了,因此,事務(wù)B申請表的寫鎖會被阻塞。

1.1通過索引檢索數(shù)據(jù),上共享鎖,行鎖(如果不通過索引,會使用表鎖)

1.1通過索引檢索數(shù)據(jù),上共享鎖,行鎖
SessionA                    SessionB
mysql> set autocommit=0;            mysql> set autocommit=0;
Query OK, 0 rows affected (0.02 sec)      Query OK, 0 rows affected (0.02 sec)
mysql> select * from test;           mysql> select * from test;
--------------------------------------------------------------------------------
+----+-------+-------+-------+         +----+-------+-------+-------+              
| id | name | money | level |         | id | name | money | level |
+----+-------+-------+-------+         +----+-------+-------+-------+
| 1 | tom  |  100 |   1 |         | 1 | tom  |  100 |   1 |
| 2 | jack |  200 |   2 |         | 2 | jack |  200 |   2 |
| 3 | lucas |  300 |   3 |         | 3 | lucas |  300 |   3 |
+----+-------+-------+-------+         +----+-------+-------+-------+
3 rows in set (0.00 sec)            3 rows in set (0.00 sec)
--------------------------------------------------------------------------------
對主鍵索引上共享鎖,其他事務(wù)也能獲取到共享鎖
mysql> select * from test where         
id=1 lock in share mode;
+----+------+-------+-------+
| id | name | money | level |
+----+------+-------+-------+
| 1 | tom |  100 |   1 |
+----+------+-------+-------+
1 row in set (0.01 sec)
--------------------------------------------------------------------------------
                        事務(wù)B也能繼續(xù)加共享鎖
                        mysql> select * from test where         
                        id=1 lock in share mode;
                        +----+------+-------+-------+
                        | id | name | money | level |
                        +----+------+-------+-------+
                        | 1 | tom |  100 |   1 |
                        +----+------+-------+-------+
                        1 row in set (0.01 sec)
                        但無法更新,因?yàn)槭聞?wù)A也加了共享鎖
                        mysql> update test set level=11 where id=1;
                        ERROR 1205 (HY000): Lock wait timeout exceeded;
                        try restarting transaction
                        
                        MORE:
                        無法加排它鎖
                        select *from test where id=1 for update;
                        ERROR 1205 (HY000): Lock wait timeout exceeded;
                        try restarting transaction
                        
                        可以更新未加鎖的,比如
                        mysql> update test set level=11 where id=2;
                        Query OK, 1 row affected (0.00 sec)
                        Rows matched: 1 Changed: 1 Warnings: 0
--------------------------------------------------------------------------------
事務(wù)A也無法更新,因?yàn)槭聞?wù)B加了共享鎖
mysql> update test set level=11 where id=1;
ERROR 1205 (HY000): Lock wait timeout excee
ded; try restarting transaction
--------------------------------------------------------------------------------
                        任意一個(gè)釋放共享鎖,則獨(dú)占共享鎖的事務(wù)可以更新
                        mysql> commit;
                        Query OK, 0 rows affected (0.00 sec)
--------------------------------------------------------------------------------
事務(wù)B釋放鎖,事務(wù)A獨(dú)占,可以更新了
mysql> update test set level=11 where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

1.2通過索引檢索數(shù)據(jù),上排他鎖,行鎖

1.2通過索引檢索數(shù)據(jù),上排他鎖,行鎖
SessionA                    SessionB
mysql> set autocommit=0;            mysql> set autocommit=0;
Query OK, 0 rows affected (0.02 sec)      Query OK, 0 rows affected (0.02 sec)
mysql> select * from test;           mysql> select * from test;
--------------------------------------------------------------------------------
+----+-------+-------+-------+         +----+-------+-------+-------+              
| id | name | money | level |         | id | name | money | level |
+----+-------+-------+-------+         +----+-------+-------+-------+
| 1 | tom  |  100 |   1 |         | 1 | tom  |  100 |   1 |
| 2 | jack |  200 |   2 |         | 2 | jack |  200 |   2 |
| 3 | lucas |  300 |   3 |         | 3 | lucas |  300 |   3 |
+----+-------+-------+-------+         +----+-------+-------+-------+
3 rows in set (0.00 sec)            3 rows in set (0.00 sec)
--------------------------------------------------------------------------------
對主鍵索引上排他鎖,其他事務(wù)也能獲取到共享鎖
mysql> select *from test where
id=1 for update;
+----+------+-------+-------+
| id | name | money | level |
+----+------+-------+-------+
| 1 | tom |  100 |   1 |
+----+------+-------+-------+
1 row in set (0.01 sec)
--------------------------------------------------------------------------------
                        事務(wù)B則不能繼續(xù)上排它鎖,會發(fā)生等待
                        mysql> select *from test where id=1 for update;
                        ERROR 1205 (HY000): Lock wait timeout exceeded;
                        try restarting transaction
                        
                        MORE:
                        也不能更新,因?yàn)楦乱彩巧吓潘i
                        mysql> update test set level=2 where id=1;
                        ERROR 1205 (HY000): Lock wait timeout exceeded;
                        try restarting transaction
                        
                        也不能上共享鎖
                        mysql> select * from test where level=1 lock in share mode;
                        ERROR 1205 (HY000): Lock wait timeout exceeded;
                        try restarting transaction
--------------------------------------------------------------------------------
事務(wù)A可以更新
mysql> update test set level=11 where id=1;
Query OK, 1 row affected (0.08 sec)
Rows matched: 1 Changed: 1 Warnings: 0
--------------------------------------------------------------------------------
釋放排它鎖
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
--------------------------------------------------------------------------------
                        事務(wù)A釋放鎖,事務(wù)B就可以加排它鎖了
                        mysql> select * from test where id=1 for update;
                        +----+------+-------+-------+
                        | id | name | money | level |
                        +----+------+-------+-------+
                        | 1 | tom |  100 |   1 |
                        +----+------+-------+-------+
                        1 row in set (0.00 sec)

1.3通過索引更新數(shù)據(jù),也是上排他鎖,行鎖

對于 update,insert,delete 語句會自動加排它鎖

1.3通過索引更新數(shù)據(jù),也是上排他鎖,行鎖
SessionA                    SessionB
mysql> set autocommit=0;            mysql> set autocommit=0;
Query OK, 0 rows affected (0.02 sec)      Query OK, 0 rows affected (0.02 sec)
mysql> select * from test;           mysql> select * from test;
--------------------------------------------------------------------------------
+----+-------+-------+-------+         +----+-------+-------+-------+              
| id | name | money | level |         | id | name | money | level |
+----+-------+-------+-------+         +----+-------+-------+-------+
| 1 | tom  |  100 |   1 |         | 1 | tom  |  100 |   1 |
| 2 | jack |  200 |   2 |         | 2 | jack |  200 |   2 |
| 3 | lucas |  300 |   3 |         | 3 | lucas |  300 |   3 |
+----+-------+-------+-------+         +----+-------+-------+-------+
3 rows in set (0.00 sec)            3 rows in set (0.00 sec)
--------------------------------------------------------------------------------
更新id=1的行,就給該行上了排它鎖,其他事務(wù)
無法更新該行
mysql> update test set level=11 where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
--------------------------------------------------------------------------------
                        事務(wù)B則不能更新id=1的行,會發(fā)生等待
                        mysql> update test set level=21 where id=1;
                        ERROR 1205 (HY000): Lock wait timeout exceeded;
                        try restarting transaction
                        
                        MORE:
                        也不能上排它鎖
                        mysql> select *from test where id=1 for update;
                        ERROR 1205 (HY000): Lock wait timeout exceeded;
                        try restarting transaction
                        
                        也不能上共享鎖
                        mysql> select * from test where level=1 lock in share mode;
                        ERROR 1205 (HY000): Lock wait timeout exceeded;
                        try restarting transaction
--------------------------------------------------------------------------------
釋放排它鎖
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
--------------------------------------------------------------------------------
                        事務(wù)A釋放鎖,事務(wù)B就可以加排它鎖了
                        mysql> select * from test where id=1 for update;
                        +----+------+-------+-------+
                        | id | name | money | level |
                        +----+------+-------+-------+
                        | 1 | tom |  100 |   11|
                        +----+------+-------+-------+
                        1 row in set (0.00 sec)

2.1臟讀

//臟讀
//2.1臟讀
SessionA                    SessionB
mysql> set autocommit=0;            mysql> set autocommit=0;
Query OK, 0 rows affected (0.02 sec)      Query OK, 0 rows affected (0.02 sec)
set session transaction isolation        set session transaction isolation level read uncommitted;
level read uncommitted;             Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)      
mysql> select * from test;           mysql> select * from test;
--------------------------------------------------------------------------------
+----+-------+-------+-------+         +----+-------+-------+-------+              
| id | name | money | level |         | id | name | money | level |
+----+-------+-------+-------+         +----+-------+-------+-------+
| 1 | tom  |  100 |   1 |         | 1 | tom  |  100 |   1 |
| 2 | jack |  200 |   2 |         | 2 | jack |  200 |   2 |
| 3 | lucas |  300 |   3 |         | 3 | lucas |  300 |   3 |
+----+-------+-------+-------+         +----+-------+-------+-------+
3 rows in set (0.00 sec)            3 rows in set (0.00 sec)
--------------------------------------------------------------------------------
                        mysql> update test set level=100 where id=1;
                        Query OK, 1 row affected (0.00 sec)
                        Rows matched: 1 Changed: 1 Warnings: 0
--------------------------------------------------------------------------------
//臟讀
mysql> select *from test where id=1;
+----+------+-------+-------+
| id | name | money | level |
+----+------+-------+-------+
| 1 | tom |  100 |  100 |
+----+------+-------+-------+
1 row in set (0.00 sec)
--------------------------------------------------------------------------------
                        rollback;
                        Query OK, 0 rows affected (0.01 sec)
                        
                        mysql> select *from test where id=1;
                        +----+------+-------+-------+
                        | id | name | money | level |
                        +----+------+-------+-------+
                        | 1 | tom |  100 |   1 |
                        +----+------+-------+-------+
                        1 row in set (0.00 sec)

2.2不可重復(fù)讀

2.2不可重復(fù)讀
//臟讀
SessionA                    SessionB
mysql> set autocommit=0;            mysql> set autocommit=0;
Query OK, 0 rows affected (0.02 sec)      Query OK, 0 rows affected (0.02 sec)
set session transaction isolation        set session transaction isolation level read uncommitted;
level read uncommitted;             Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)      
mysql> select * from test;           mysql> select * from test;
--------------------------------------------------------------------------------
+----+-------+-------+-------+         +----+-------+-------+-------+              
| id | name | money | level |         | id | name | money | level |
+----+-------+-------+-------+         +----+-------+-------+-------+
| 1 | tom  |  100 |   1 |         | 1 | tom  |  100 |   1 |
| 2 | jack |  200 |   2 |         | 2 | jack |  200 |   2 |
| 3 | lucas |  300 |   3 |         | 3 | lucas |  300 |   3 |
+----+-------+-------+-------+         +----+-------+-------+-------+
3 rows in set (0.00 sec)            3 rows in set (0.00 sec)
--------------------------------------------------------------------------------
                        mysql> update test set level=100 where id=1;
                        Query OK, 1 row affected (0.00 sec)
                        Rows matched: 1 Changed: 1 Warnings: 0
--------------------------------------------------------------------------------
mysql> select *from test where id=1;
+----+------+-------+-------+
| id | name | money | level |
+----+------+-------+-------+
| 1 | tom |  100 |  100 |
+----+------+-------+-------+
1 row in set (0.00 sec)
--------------------------------------------------------------------------------
                        mysql> update test set level=1000 where id=1;
                        Query OK, 1 row affected (0.00 sec)
                        Rows matched: 1 Changed: 1 Warnings: 0
--------------------------------------------------------------------------------
//不可重復(fù)讀
//讀三次,第一次是level是1,第二次是100,第三次是1000
mysql> select *from test where id=1;
+----+------+-------+-------+
| id | name | money | level |
+----+------+-------+-------+
| 1 | tom |  100 |  1000|
+----+------+-------+-------+
1 row in set (0.00 sec)

2.3幻讀

//2.3幻讀
SessionA                    SessionB
mysql> set autocommit=0;            mysql> set autocommit=0;
Query OK, 0 rows affected (0.02 sec)      Query OK, 0 rows affected (0.02 sec)
set session transaction isolation        set session transaction isolation level read uncommitted;
level read uncommitted;             Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)      
mysql> select * from test;           mysql> select * from test;
--------------------------------------------------------------------------------
+----+-------+-------+-------+         +----+-------+-------+-------+              
| id | name | money | level |         | id | name | money | level |
+----+-------+-------+-------+         +----+-------+-------+-------+
| 1 | tom  |  100 |   1 |         | 1 | tom  |  100 |   1 |
| 2 | jack |  200 |   2 |         | 2 | jack |  200 |   2 |
| 3 | lucas |  300 |   3 |         | 3 | lucas |  300 |   3 |
+----+-------+-------+-------+         +----+-------+-------+-------+
3 rows in set (0.00 sec)            3 rows in set (0.00 sec)
--------------------------------------------------------------------------------
                        mysql> update test set level=100 where id=1;
                        Query OK, 1 row affected (0.00 sec)
                        Rows matched: 1 Changed: 1 Warnings: 0
--------------------------------------------------------------------------------
mysql> select *from test where id=1;
+----+------+-------+-------+
| id | name | money | level |
+----+------+-------+-------+
| 1 | tom |  100 |  100 |
+----+------+-------+-------+
1 row in set (0.00 sec)
--------------------------------------------------------------------------------
                        mysql> insert into test (name, money,level) VALUES                             ('tim',250,4);
                        Query OK, 1 row affected (0.01 sec)
--------------------------------------------------------------------------------
//幻讀
//讀兩次,第二次多了tim的數(shù)據(jù)
//如果是rr級別,需要使用當(dāng)前讀select * from test lock in share mode;否則因?yàn)镸VCC的緣故,是讀不到tim的
mysql> select * from test;
+----+-------+-------+-------+
| id | name | money | level |
+----+-------+-------+-------+
| 1 | tom  |  100 |   1 |
| 2 | jack |  200 |   2 |
| 3 | lucas |  300 |   3 |
| 4 | tim  |  250 |   4 |
+----+-------+-------+-------+
4 row in set (0.00 sec)

3 間隙鎖(Net-Key鎖)

MVCC使RR級別下,事務(wù)當(dāng)前讀,來避免了讀情況下的幻讀問題,但如果寫更新時(shí)候呢?在范圍更新的同時(shí),往范圍內(nèi)插入新數(shù)據(jù),怎么辦?

于是就有了間隙鎖,在更新某個(gè)區(qū)間數(shù)據(jù)時(shí),將會鎖定這個(gè)區(qū)間的所有記錄。例如update XXX where id between 1 and 100, 就會鎖住id從1到100之間的所有的記錄。值得注意的是,在這個(gè)區(qū)間中假設(shè)某條記錄并不存在,該條記錄也會被鎖住,這時(shí),如果另一個(gè)事務(wù)往這個(gè)區(qū)間添加數(shù)據(jù),就必須等待上一個(gè)事務(wù)釋放鎖資源。

使用間隙鎖有兩個(gè)目的,一是防止幻讀;二是滿足其恢復(fù)和賦值的需求。

3.1范圍間隙鎖,顯式左開右閉區(qū)間

//間隙鎖(Net-Key鎖) 范圍間隙鎖,左開右閉區(qū)間
SessionA                    SessionB
mysql> set autocommit=0;            mysql> set autocommit=0;
Query OK, 0 rows affected (0.02 sec)      Query OK, 0 rows affected (0.02 sec)
     
mysql> select * from test;           mysql> select * from test;
--------------------------------------------------------------------------------
+----+-------+-------+-------+         +----+-------+-------+-------+              
| id | name | money | level |         | id | name | money | level |
+----+-------+-------+-------+         +----+-------+-------+-------+
| 1 | tom  |  100 |   1 |         | 1 | tom  |  100 |   1 |
| 2 | jack |  200 |   2 |         | 2 | jack |  200 |   2 |
| 3 | lucas |  300 |   3 |         | 3 | lucas |  300 |   3 |
+----+-------+-------+-------+         +----+-------+-------+-------+
3 rows in set (0.00 sec)            3 rows in set (0.00 sec)
--------------------------------------------------------------------------------
mysql> update test set level=0
where money between 0 and 200;
Query OK, 2 rows affected (0.02 sec)
Rows matched: 2 Changed: 2 Warnings: 0
理論上應(yīng)該鎖定[0,300)這個(gè)區(qū)間
--------------------------------------------------------------------------------
                        插入money=0等待
                        mysql> insert into test (name, money,level) VALUES ('tim',0,0);
                        ERROR 1205 (HY000): Lock wait timeout exceeded;
                        try restarting transaction
                        
                        插入money=90等待
                        mysql> insert into test (name, money,level) VALUES ('tim',90,0);
                        ERROR 1205 (HY000): Lock wait timeout exceeded;
                        try restarting transaction
                        
                        插入money=100等待
                        mysql> insert into test (name, money,level) VALUES ('tim',100,0);
                        ERROR 1205 (HY000): Lock wait timeout exceeded;
                        try restarting transaction
                        
                        插入money=299等待
                        mysql> insert into test (name, money,level) VALUES ('tim',299,0);
                        ERROR 1205 (HY000): Lock wait timeout exceeded;
                        try restarting transaction
                        
                        插入money=300 ok
                        mysql> insert into test (name, money,level) VALUES ('tim',300,0);
                        Query OK, 1 row affected (0.00 sec)

3.2單個(gè)間隙鎖 隱式區(qū)間

上小節(jié)是指定update某個(gè)區(qū)間,那如果說是只update一個(gè)值呢?還會有間隙鎖么?

//間隙鎖(Net-Key鎖) 單個(gè)間隙鎖,左開右閉區(qū)間
SessionA                    SessionB
mysql> set autocommit=0;            mysql> set autocommit=0;
Query OK, 0 rows affected (0.02 sec)      Query OK, 0 rows affected (0.02 sec)
     
mysql> select * from test;           mysql> select * from test;
--------------------------------------------------------------------------------
+----+-------+-------+-------+         +----+-------+-------+-------+              
| id | name | money | level |         | id | name | money | level |
+----+-------+-------+-------+         +----+-------+-------+-------+
| 1 | tom  |  100 |   1 |         | 1 | tom  |  100 |   1 |
| 2 | jack |  200 |   2 |         | 2 | jack |  200 |   2 |
| 3 | lucas |  300 |   3 |         | 3 | lucas |  300 |   3 |
+----+-------+-------+-------+         +----+-------+-------+-------+
3 rows in set (0.00 sec)            3 rows in set (0.00 sec)
--------------------------------------------------------------------------------
mysql> update test set level=0
where money = 200;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
理論上應(yīng)該鎖定[0,300)這個(gè)區(qū)間
--------------------------------------------------------------------------------
                        插入money=0 ok
                        mysql> insert into test (name, money,level) VALUES ('tim',0,0);
                        Query OK, 1 row affected (0.00 sec)
                        
                        插入money=90 ok
                        mysql> insert into test (name, money,level) VALUES ('tim',90,0);
                        Query OK, 1 row affected (0.00 sec)
                        
                        插入money=100等待
                        mysql> insert into test (name, money,level) VALUES ('tim',100,0);
                        ERROR 1205 (HY000): Lock wait timeout exceeded;
                        try restarting transaction
                        
                        插入money=150等待
                        mysql> insert into test (name, money,level) VALUES ('tim',150,0);
                        ERROR 1205 (HY000): Lock wait timeout exceeded;
                        try restarting transaction
                        
                        插入money=200等待
                        mysql> insert into test (name, money,level) VALUES ('tim',200,0);
                        ERROR 1205 (HY000): Lock wait timeout exceeded;
                        try restarting transaction
                        
                        插入money=240等待
                        mysql> insert into test (name, money,level) VALUES ('tim',240,0);
                        ERROR 1205 (HY000): Lock wait timeout exceeded;
                        try restarting transaction
                        
                        插入money=300 ok
                        mysql> insert into test (name, money,level) VALUES ('tim',300,0);
                        Query OK, 1 row affected (0.00 sec)

當(dāng)不指定區(qū)間時(shí),隱式的區(qū)間為索引B+數(shù)前后兩個(gè)節(jié)點(diǎn)的值所確定的區(qū)間,也是左開右閉,對于上述例子,就是[0,300)這個(gè)區(qū)間。

總結(jié)

到此這篇關(guān)于Mysql InnoDB鎖定機(jī)制的文章就介紹到這了,更多相關(guān)Mysql InnoDB鎖定機(jī)制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • MySQL 集群遷移到 Kubernetes操作步驟

    MySQL 集群遷移到 Kubernetes操作步驟

    這篇文章主要為大家介紹了MySQL 集群遷移到 Kubernetes使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • MySQL數(shù)據(jù)庫定時(shí)任務(wù)舉例講解

    MySQL數(shù)據(jù)庫定時(shí)任務(wù)舉例講解

    最近項(xiàng)目里面的后臺需要用到定時(shí)任務(wù),而MySQL從5.0開始自帶了定時(shí)事件操作,所以學(xué)習(xí)下并做下記錄,下面這篇文章主要給大家介紹了關(guān)于MySQL數(shù)據(jù)庫定時(shí)任務(wù)的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • mysql常用命令匯總介紹

    mysql常用命令匯總介紹

    本文給大家匯總了一下mysql常用的命令行指令,包括設(shè)置更改mysqlroot密碼,連接mysql等,非常的細(xì)致全面,并附上運(yùn)行效果截圖,希望大家能夠喜歡
    2018-03-03
  • Win7 64位 mysql 5.7下載安裝常見問題小結(jié)

    Win7 64位 mysql 5.7下載安裝常見問題小結(jié)

    這篇文章主要介紹了Win7 64位 mysql 5.7下載安裝常見問題小結(jié),本文圖文并茂給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2017-06-06
  • MySQL數(shù)據(jù)庫21條最佳性能優(yōu)化經(jīng)驗(yàn)

    MySQL數(shù)據(jù)庫21條最佳性能優(yōu)化經(jīng)驗(yàn)

    數(shù)據(jù)庫的操作越來越成為整個(gè)應(yīng)用的性能瓶頸了,這點(diǎn)對于Web應(yīng)用尤其明顯。這篇文章主要介紹了MySQL數(shù)據(jù)庫21條最佳性能優(yōu)化經(jīng)驗(yàn)的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • MySQL全局鎖和表鎖的深入理解

    MySQL全局鎖和表鎖的深入理解

    這篇文章主要給大家介紹了關(guān)于MySQL全局鎖和表鎖的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用MySQL具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • mysql 獲取昨天日期、今天日期、明天日期以及前一個(gè)小時(shí)和后一個(gè)小時(shí)的時(shí)間

    mysql 獲取昨天日期、今天日期、明天日期以及前一個(gè)小時(shí)和后一個(gè)小時(shí)的時(shí)間

    這篇文章主要介紹了mysql 獲取昨天日期、今天日期、明天日期以及前一個(gè)小時(shí)和后一個(gè)小時(shí)的時(shí)間,需要的朋友可以參考下
    2018-01-01
  • mysql獲取group by總記錄行數(shù)的方法

    mysql獲取group by總記錄行數(shù)的方法

    這篇文章主要介紹了mysql獲取group by總記錄行數(shù)的方法,主要通過group by SQL_CALC_FOUND_ROWS語句來實(shí)現(xiàn)該功能,具有一定的實(shí)用價(jià)值,需要的朋友可以參考下
    2014-10-10
  • 一次SQL如何查重及去重的實(shí)戰(zhàn)記錄

    一次SQL如何查重及去重的實(shí)戰(zhàn)記錄

    sql去重這可以說算是比較經(jīng)典的面試題了,這篇文章主要給大家介紹了一次SQL如何查重及去重的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-03-03
  • MySQL?開窗函數(shù)

    MySQL?開窗函數(shù)

    這篇文章主要介紹了MySQL?開窗函數(shù)
    2022-02-02

最新評論