【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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL 5.7 SELECT ... LOCK IN SHARE MODE|FOR UPDATE語句說明MySql
- 深入理解SELECT ... LOCK IN SHARE MODE和SELECT ... FOR UPDATE
- MySQL:Innodb 讓MDL LOCK和ROW LOCK 記錄到errlogMySql
- MySQL 共享鎖 (lock in share mode),排他鎖 (for update)MySql
- enq: TX - row lock contentionENQ
- 等待事件之Row Cache Lock事件
- select the lock block in MSSQLBloCSQL
- enable table lock 的enqueue等待ENQ
- Select for update/lock in share mode 對事務併發性影響
- mysql表鎖與lock tablesMySql
- MySQL 5.6 drop database時,table metadata lock等待MySqlDatabase
- 奇異的enq: TX - row lock contentionENQ
- 等待事件enq: TX - row lock contention事件ENQ
- 【等待事件】-enq: TX - row lock contention事件ENQ
- row lock contention 阻塞程式查詢
- hanganalyze解決row cache lock(ZT)
- WAITED TOO LONG FOR A ROW CACHE ENQUEUE LOCK!AIENQ
- [ORACLE 11G]ROW CACHE LOCK 等待Oracle
- mysql lock操作MySql
- MySQL新增索引偶遇waiting for table metadata lockMySql索引AI
- MySQL DDL Waiting for table metadata lock 解決MySqlAI
- select for update_v$lock_dml小測試(鎖型別及模式)型別模式
- WAITED TOO LONG FOR A ROW CACHE ENQUEUE LOCK!的分析AIENQ
- mysql innodb lock鎖之record lock之一MySql
- 等待事件enq TX row lock contention分析事件ENQ
- 轉)用hanganalyze解決row cache lock
- (轉)用hanganalyze解決row cache lock
- WAITED TOO LONG FOR A ROW CACHE ENQUEUE LOCK! 與 dc_tablespcesAIENQ
- Waiting for table metadata lockAI
- Mysql加鎖過程詳解(4)-select for update/lock in share mode 對事務併發性影響MySql
- MySQL:理解MDL LockMySql
- mysql metadata lock原理與實現MySql
- create table if not exists Waiting for table metadata lockAI
- 由row cache lock等待事件引起的效能問題事件
- 無關的表引起的enq: TX - row lock contentionENQ
- 故障排除:"WAITED TOO LONG FOR A ROW CACHE ENQUEUE LOCK! "AIENQ
- 轉貼_用hanganalyze解決row cache lock
- enq: TX - row lock contention等待事件處理ENQ事件