真正决定是否执行要上锁的行不是取出来的行,而是扫描的行。
而是否最索引来扫描记录,则跟具体的执行计划有关系。
所以在分析锁的问题,一定不要忘记看执行计划.
会话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
而是否最索引来扫描记录,则跟具体的执行计划有关系。
所以在分析锁的问题,一定不要忘记看执行计划.
会话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/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/674865/viewspace-2135287/