【MySQL】select for update 的Row Lock 與Table Lock
select for update 對錶施加的鎖模式分兩種情況:
只有當where 條件中明確地使用指定主鍵時,MySQL 才會對錶執行Row lock (只鎖住被選取的資料) ,
否則MySQL 將會執行Table Lock (將整個資料表單給鎖住)。
下面以具體的例項驗證上面的結論:
注意 表mail_queue 的結構如下
root@127.0.0.1 : test 22:06:52> show create table mail_queue \G
*************************** 1. row ***************************
Table: mail_queue
Create Table: CREATE TABLE `mail_queue` (
`mail_id` int(11) NOT NULL AUTO_INCREMENT,
`is_sent` tinyint(4) NOT NULL COMMENT '是否已經傳送(-1,失敗,0沒傳送,1傳送成功)',
`mail_title` varchar(127) NOT NULL COMMENT '郵件標題',
`mail_from` varchar(127) NOT NULL COMMENT '郵件發件人',
`mail_to` varchar(127) NOT NULL COMMENT '郵件接收人',
`mail_content` text NOT NULL COMMENT '郵件內容',
`add_time` int(11) NOT NULL COMMENT '?'
`send_time` int(11) NOT NULL COMMENT '?'
`agent_id` int(11) NOT NULL COMMENT '?',
`service_id` int(11) NOT NULL COMMENT 'ID',
PRIMARY KEY (`mail_id`),
KEY `ind_mq_atime_isent_mail_id` (`add_time`,`is_sent`,`mail_id`),
KEY `sid` (`service_id`)
) ENGINE=InnoDB AUTO_INCREMENT=7391601 DEFAULT CHARSET=utf8 COMMENT='郵件佇列'
1 row in set (0.00 sec)
例1: (明確指定主鍵,並且有此資料,row lock)
session1
root@127.0.0.1 : test 21:50:40> set autocommit=0;
Query OK, 0 rows affected (0.00 sec)
root@127.0.0.1 : test 21:50:56>
root@127.0.0.1 : test 21:50:56> select mail_id from mail_queue where mail_id=237 for update;
+---------+
| mail_id |
+---------+
| 237 |
+---------+
1 row in set (0.00 sec)
session2
root@127.0.0.1 : test 21:51:42> set autocommit=0;
Query OK, 0 rows affected (0.00 sec)
root@127.0.0.1 : test 21:51:48> select mail_id from mail_queue where mail_id=237;
+---------+
| mail_id |
+---------+
| 237 |
+---------+
1 row in set (0.00 sec)
例2: (明確指定主鍵,若查無此資料,無lock)
session 1
root@127.0.0.1 : test 22:06:05> select mail_id from mail_queue where mail_id=-1 for update;
Empty set (0.00 sec)
session 2
root@127.0.0.1 : test 22:06:45> select mail_id,is_sent from mail_queue where mail_id=237 for update;
+---------+---------+
| mail_id | is_sent |
+---------+---------+
| 237 | 1 |
+---------+---------+
1 row in set (0.00 sec)
例3: (無主鍵,table lock)
mail_to 非主鍵
session 1
root@127.0.0.1 : test 22:09:20> select mail_to from mail_queue where mail_to='slxx_721521@126.com' for update;
+---------------------+
| mail_to |
+---------------------+
| slxx_721521@126.com |
| slxx_721521@126.com |
| slxx_721521@126.com |
| slxx_721521@126.com |
| slxx_721521@126.com |
| slxx_721521@126.com |
| slxx_721521@126.com |
| slxx_721521@126.com |
| slxx_721521@126.com |
| slxx_721521@126.com |
+---------------------+
10 rows in set (50.23 sec)
session 2 查詢被阻塞
root@127.0.0.1 : test 22:10:02> select mail_id,is_sent from mail_queue where mail_id=237 for update;
Ctrl-C -- sending "KILL QUERY 38535" to server ...
Ctrl-C -- query aborted.
ERROR 1317 (70100): Query execution was interrupted
root@127.0.0.1 : test 22:11:30>
例4: (主鍵不明確,table lock)
session 1
root@127.0.0.1 : test 22:11:48> select count(1) from mail_queue where mail_id <> 100000 for update;
+----------+
| count(1) |
+----------+
| 3695458 |
+----------+
1 row in set (27.45 sec)
session 2 會話被鎖
root@127.0.0.1 : test 22:12:35> select mail_id,is_sent from mail_queue where mail_id=237 for update;
Ctrl-C -- sending "KILL QUERY 38535" to server ...
Ctrl-C -- query aborted.
ERROR 1317 (70100): Query execution was interrupted
root@127.0.0.1 : test 22:13:10>
例5: (主鍵不明確,table lock)
session 1
root@127.0.0.1 : test 22:13:46> select count(1) from mail_queue where mail_id like '100000' for update;
+----------+
| count(1) |
+----------+
| 0 |
+----------+
1 row in set (24.31 sec)
root@127.0.0.1 : test 22:14:22>
session 2
root@127.0.0.1 : test 22:13:34> select mail_id,is_sent from mail_queue where mail_id=237 for update;
Ctrl-C -- sending "KILL QUERY 38535" to server ...
Ctrl-C -- query aborted.
ERROR 1317 (70100): Query execution was interrupted
注:FOR UPDATE 僅適用於InnoDB,且必須在事務區塊(BEGIN/COMMIT)中才能生效。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22664653/viewspace-750695/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 深入理解SELECT ... LOCK IN SHARE MODE和SELECT ... FOR UPDATE
- MySQL:Innodb 讓MDL LOCK和ROW LOCK 記錄到errlogMySql
- enq: TX - row lock contentionENQ
- MySQL新增索引偶遇waiting for table metadata lockMySql索引AI
- MySQL DDL Waiting for table metadata lock 解決MySqlAI
- MySQL 共享鎖 (lock in share mode),排他鎖 (for update)MySql
- 奇異的enq: TX - row lock contentionENQ
- 等待事件enq: TX - row lock contention事件ENQ
- [20190522]DISABLE TABLE LOCK.txt
- mysql lock操作MySql
- mysql innodb lock鎖之record lock之一MySql
- MySQL:理解MDL LockMySql
- Oracle Enqueues Wait Events 三 enq: TX - row lock contentionOracleENQAI
- [20190524]DISABLE TABLE LOCK(12c).txt
- [20190530]DISABLE TABLE LOCK(10g).txt
- GaussDB資料庫SQL系列-LOCK TABLE資料庫SQL
- MySQL:MDL LOCK的“穿越行為”MySql
- MySQL的GET_LOCK函式MySql函式
- synchronized 與 Lock 的對比synchronized
- synchronized與Lock的區別synchronized
- 四、Synchronized與Lock原理synchronized
- MySQL-lock(鎖)-v2.0MySql
- mysql觀測METADATA LOCK(MDL)鎖MySql
- MySQL5.7 Waiting for global read lockMySqlAI
- 初識Lock與AbstractQueuedSynchronizer(AQS)AQS
- mysql update join,insert select 語法MySql
- Lock 鎖
- MySQL更新卡住報錯lock wait timeoutMySqlAI
- synchronized與Lock的區別與使用詳解synchronized
- Lock 介面與 AQS 同步器AQS
- mysql metadata lock後設資料鎖之鎖狀態lock_status流轉圖MySql
- MySQL裡的found_row()與row_count()MySql
- select for update
- 當刪除oracle資料庫user時發生row cache lock 等待事件Oracle資料庫事件
- sudo apt update時報錯 E: Unable to lock directory /var/lib/apt/lists/APT
- mysql, Lock wait timeout exceeded; try restarting 解決MySqlAIREST
- V$LOCK(zt)
- MySQL 8.0 Reference Manual(讀書筆記66節--locking read 與lock)MySql筆記
- 關於 mysql 中的 select * from table_a,table_b 的問題MySql