【MySQL】select for update 的Row Lock 與Table Lock

楊奇龍發表於2012-12-06
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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章