最近一直在优化各个产品的SQL语句,同时还帮一个同事解决deadlock问题,收获就是对InnoDB加锁的理解更加深入了。先来看看今天的这个案例:
- mysql> select version();
- +----------------------+
- | version() |
- +----------------------+
- | 5.5.13.4-log |
- +----------------------+
- mysql> show variables like"%iso%";
- +---------------+-----------------+
- | Variable_name | Value |
- +---------------+-----------------+
- | tx_isolation | REPEATABLE-READ |
- +---------------+-----------------+
- CREATETABLE `t1` (
- `a` int(11) NOTNULL,
- `b` int(11) DEFAULTNULL,
- `c` int(11) DEFAULTNULL,
- PRIMARYKEY (`a`)
- ) ENGINE=InnoDB;
- mysql> select * from t1;
- +----+------+------+
- | a | b | c |
- +----+------+------+
- | 1 | 1 | 1 |
- | 2 | 1 | 1 |
- | 3 | 1 | 1 |
- | 10 | 1 | 1 |
- | 11 | 1 | 1 |
- | 12 | 1 | 1 |
- | 14 | 1 | 1 |
- | 15 | 1 | 1 |
- +----+------+------+
测试环境就是如上,开始下面的测试:
- session1:
- mysql> begin;
- Query OK, 0 rows affected (0.00 sec)
- mysql> select * from t1 where a in (2, 10, 11, 12,14) forupdate;
- +----+------+------+
- | a | b | c |
- +----+------+------+
- | 2 | 1 | 1 |
- | 10 | 1 | 1 |
- | 11 | 1 | 1 |
- | 12 | 1 | 1 |
- | 14 | 1 | 1 |
- +----+------+------+
- 5 rowsinset (0.00 sec)
- session2:
- mysql> insertinto t1 values(7, 1, 1);
- ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
- mysql> select * from t1 where a=15 forupdate;
- ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
- mysql> insertinto t1 values(18,1,1);
- ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
- mysql> insertinto t1 values(18000,1,1);
- ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
从上面可以看出InnoDB仿佛是将[2, 无穷大)这个区间给锁了,那么原因是什么呢? 你可以先思考下
在内存中建立 MySQL 的临时目录MySQL Binlog三种格式介绍及分析相关资讯 InnoDB SQL语句
- MySQL误删除InnoDB数据文件恢复 (07月14日)
- 如何定位SQL语句在共享池里用到了 (03月17日)
- MySQL · 引擎特性 · InnoDB undo (03月02日)
| - 解决数据库Operation not allowed (04月11日)
- MySQL 5.6.23 InnoDB相关Bugfix (03月02日)
- MySQL · 引擎特性 · InnoDB redo (03月02日)
|
本文评论 查看全部评论 (0)