命令行窗口1 | 命令行窗口2 | 命令行窗口3 |
---|
mysql> set autocommit = 0; Query OK, 0 rows affected mysql> select * from developerinfo where userid = '1' for update; +--------+--------+----------+ | userID | name | passWord | +--------+--------+----------+ | 1 | liujie | 123456 | +--------+--------+----------+ 1 row in set |mysql> set autocommit = 0; Query OK, 0 rows affected mysql> select * from developerinfo where userid = '1' for update; 等待 |mysql> set autocommit = 0; Query OK, 0 rows affected mysql> select * from developerinfo where userid = '3' for update; +--------+------+----------+ | userID | name | passWord | +--------+------+----------+ | 3 | tong | 123456 | +--------+------+----------+ 1 row in set |mysql> commit; Query OK, 0 rows affected mysql> select * from developerinfo where userid = '1' for update; +--------+--------+----------+ | userID | name | passWord | +--------+--------+----------+ | 1 | liujie | 123456 | +--------+--------+----------+ 1 row in set
(2)查詢非索引的字段來查詢數(shù)據(jù)庫使用行鎖
打開兩個(gè)命令行窗口進(jìn)行測(cè)試
命令行窗口1 | 命令行窗口2 |
---|
|mysql> set autocommit=0; Query OK, 0 rows affected mysql> select * from developerinfo where name = 'liujie' for update; +--------+--------+----------+ userID name passWord +--------+--------+----------+ 1 liujie 123456 +--------+--------+----------+ 1 row in set |mysql> set autocommit=0; Query OK, 0 rows affected mysql> select * from developerinfo where name = 'tong' for update; 等待| mysql> commit; Query OK, 0 rows affected mysql> select * from developerinfo where name = 'liujie' for update; +--------+--------+----------+ | userID | name | passWord | +--------+--------+----------+ | 1 | liujie | 123456 | +--------+--------+----------+ 1 row in set
##### (3)查詢非唯一索引字段來查詢數(shù)據(jù)庫使用行鎖鎖住多行
mysql的行鎖是針對(duì)索引假的鎖,不是針對(duì)記錄,所以可能會(huì)出現(xiàn)鎖住不同記錄的場(chǎng)景
打開三個(gè)命令行窗口進(jìn)行測(cè)試
命令行窗口1 命令行窗口2 命令行窗口3
mysql> set autocommit=0; Query OK, 0 rows affected mysql> select * from developerinfo where password = '123456 ' for update; +--------+--------+----------+ | userID | name | passWord | +--------+--------+----------+ | 1 | liujie | 123456 | | 3 | tong | 123456 | +--------+--------+----------+ 2 rows in set mysql> set autocommit =0 ; Query OK, 0 rows affected mysql> select * from developerinfo where userid = '1' for update;
等待
mysql> set autocommit = 0; Query OK, 0 rows affected mysql> select * from developerinfo where userid = '2 ' for update; +--------+--------+----------+ | userID | name | passWord | +--------+--------+----------+ | 2 | yitong | 123 | +--------+--------+----------+ 1 row in set commit; mysql> select * from developerinfo where userid = '1' for update; +--------+--------+----------+ | userID | name | passWord | +--------+--------+----------+ | 1 | liujie | 123456 | +--------+--------+----------+ 1 row in set
##### (4)條件中使用索引來操作檢索數(shù)據(jù)庫時(shí),是否使用索引還需有mysql通過判斷不同執(zhí)行計(jì)劃來決定,是否使用該索引,如需判定如何使用explain來判斷索引,請(qǐng)聽下回分解
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
標(biāo)簽:鄂爾多斯 黔西 昌都 駐馬店 荊門 梅河口 陜西 北京
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySQL中的行級(jí)鎖定示例詳解》,本文關(guān)鍵詞 MySQL,中的,行級(jí),鎖定,示例,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。