mysql - 为什么innodb下更新A行时B行也被锁住?
问题描述
在学习MySQL事务隔离级别时,本来想重现《高性能MySQL》中的死锁现象(Page 9)。结果发现innodb更新单行时,造成全表被锁住,不符合innodb行锁的设置。
使用的版本:
mysql> status--------------mysql Ver 14.14 Distrib 5.6.26, for Linux (x86_64) using EditLine wrapperConnection id: 2Current database: testCurrent user: root@localhostSSL: Not in useCurrent pager: stdoutUsing outfile: ’’Using delimiter:;Server version: 5.6.26 MySQL Community Server (GPL)Protocol version: 10Connection: Localhost via UNIX socketServer characterset: latin1Db characterset: latin1Client characterset: utf8Conn. characterset: utf8UNIX socket: /var/lib/mysql/mysql.sockUptime: 4 hours 52 min 1 secThreads: 3 Questions: 107 Slow queries: 0 Opens: 69 Flush tables: 1 Open tables: 62 Queries per second avg: 0.006--------------mysql> show variables like ’%isolation%’;+---------------+-----------------+| Variable_name | Value |+---------------+-----------------+| tx_isolation | REPEATABLE-READ |+---------------+-----------------+1 row in set (0.00 sec)
测试表
mysql> show create table tG;*************************** 1. row *************************** Table: tCreate Table: CREATE TABLE `t` ( `a1` int(11) DEFAULT NULL, `b` varchar(10) DEFAULT NULL, `c` varchar(10) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin11 row in set (0.00 sec)ERROR: No query specifiedmysql> select * from t;+------+------+------+| a1 | b | c |+------+------+------+| 1 | a | b || 2 | aa | bb |+------+------+------+2 rows in set (0.00 sec)
在两个独立的会话中创建两个事务
会话1
mysql> start transaction;Query OK, 0 rows affected (0.00 sec)mysql> update t set b=’x’ where a1=2;Query OK, 1 row affected (0.00 sec)Rows matched: 1 Changed: 1 Warnings: 0
会话2,在被阻塞一段时间后会出现超时错误。
mysql> start transaction -> ;Query OK, 0 rows affected (0.00 sec)mysql> update t set c=’yy’ where a1=1;ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
原则上来说,会话1中的行锁不会阻塞会话2中的更新才对。
不知道大家是否遇到过这样的问题,感谢提供相应的解答。
问题解答
回答1:在a1上添加索引,才是行锁定。innodb 没有索引,照样是表锁定
相关文章:
1. python round 四舍五入?2. sql语句 - 如何在mysql中批量添加用户?3. javascript - 按钮链接到另一个网址 怎么通过百度统计计算按钮的点击数量4. 事务 - mysql共享锁lock in share mode的实际使用场景5. mysql - PHP定时通知、按时发布怎么做?6. mysql - 数据库建字段,默认值空和empty string有什么区别 1107. 怎么php怎么通过数组显示sql查询结果呢,查询结果有多条,如图。8. node.js - mysql如何通过knex查询今天和七天内的汇总数据9. mysql - JAVA怎么实现一个DAO同时实现查询两个实体类的结果集10. python - 请问这两个地方是为什么呢?
