mysql innodb的行鎖(4)

xchui702發表於2017-03-14
真正決定是否執行要上鎖的行不是取出來的行,而是掃描的行。
而是否最索引來掃描記錄,則跟具體的執行計劃有關係。
所以在分析鎖的問題,一定不要忘記看執行計劃.

會話1: 由於name是varchar型別,存在隱式轉換,所以掃描了所有的記錄,因而對所有的記錄上鎖了
root@sakila 10:33:04>explain select * from tab_no_index where name=1 for update;
+----+-------------+--------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table        | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+--------------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | tab_no_index | ALL  | name          | NULL | NULL    | NULL |    6 | Using where |
+----+-------------+--------------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

root@sakila 10:34:30>select * from tab_no_index where name=1 for update;
+------+------+
| id   | name |
+------+------+
|    1 | 1    |
+------+------+
1 row in set (0.00 sec)

會話2: 即使更新的記錄和回話1選出來的記錄不一樣,但是由於該記錄被第一個會話掃描過,被加鎖了,所以也不能上更新鎖
root@sakila 10:34:35>select * from tab_no_index where name='4' for update;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/674865/viewspace-2135287/,如需轉載,請註明出處,否則將追究法律責任。

相關文章