InnoDB行锁和表锁的分析
- InnoDB行锁是通过给索引上的索引项加锁来实现的,这一点MySQL与Oracle不同,后者是通过在数据块中对相应数据行加锁来实现的。
- InnoDB这种行锁实现特点意味着:只有通过索引条件检索数据,InnoDB才使用行级锁,否则,InnoDB将使用表锁!
- 在实际应用中,要特别注意InnoDB行锁的这一特性,不然的话,可能导致大量的锁冲突,从而影响并发性能。
- 下面通过一些实际例子来加以说明。
- (1)在不通过索引条件查询的时候,InnoDB使用的是表锁,而不是行锁
- session_1
- mysql> set autocommit=0;
- Query OK, 0 rows affected (0.00 sec)
- mysql> select * from tab_no_index where id = 1 ;
- +------+------+
- | id | name |
- +------+------+
- | 1 | 1 |
- +------+------+
- 1 row in set (0.00 sec)
- session_2
- mysql> set autocommit=0;
- Query OK, 0 rows affected (0.00 sec)
- mysql> select * from tab_no_index where id = 2 ;
- +------+------+
- | id | name |
- +------+------+
- | 2 | 2 |
- +------+------+
- 1 row in set (0.00 sec)
- session_1:
- mysql> select * from tab_no_index where id = 1 for update;
- +------+------+
- | id | name |
- +------+------+
- | 1 | 1 |
- +------+------+
- 1
- session_2:
- mysql> select * from tab_no_index where id=2 for update;
- ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
- mysql> create table tab_with_index(id int,name varchar(10)) engine=innodb;
- Query OK, 0 rows affected (0.15 sec)
- mysql> alter table tab_with_index add index id(id);
- Query OK, 4 rows affected (0.24 sec)
- Records: 4 Duplicates: 0 Warnings: 0
|
- mysql> alter table tab_with_index drop index name;
- Query OK, 4 rows affected (0.22 sec)
- Records: 4 Duplicates: 0 Warnings: 0
- mysql> insert into tab_with_index values(1,'4');
- Query OK, 1 row affected (0.00 sec)
- mysql> select * from tab_with_index where id = 1;
- +------+------+
- | id | name |
- +------+------+
- | 1 | 1 |
- | 1 | 4 |
- +------+------+
- 2 rows in set (0.00 sec)
- session_1
- mysql> set autocommit=0;
- Query OK, 0 rows affected (0.00 sec)
- session_2
- mysql> set autocommit=0;
- Query OK, 0 rows affected (0.00 sec)
- session_1
- mysql> select * from tab_with_index where id = 1 and name = '1' for update;
- +------+------+
- | id | name |
- +------+------+
- | 1 | 1 |
- +------+------+
- 1 row in set (0.00 sec)
- 虽然session_2访问的是和session_1不同的记录,但是因为使用了相同的索引,所以需要等待锁:
- mysql> select * from tab_with_index where id = 1 and name = '4' for update;
- 等待
- mysql> alter table tab_with_index add index name(name);
- Query OK, 5 rows affected (0.23 sec)
- Records: 5 Duplicates: 0 Warnings: 0
|
- Select * from emp where empid > 100 for update;
session_1 | session_2 |
mysql> select @@tx_isolation;
+-----------------+
| @@tx_isolation |
+-----------------+
| REPEATABLE-READ |
+-----------------+
1 row in set (0.00 sec)
mysql> set autocommit = 0;
Query OK, 0 rows affected (0.00 sec)
|
mysql> select @@tx_isolation;
+-----------------+
| @@tx_isolation |
+-----------------+
| REPEATABLE-READ |
+-----------------+
1 row in set (0.00 sec)
mysql> set autocommit = 0;
Query OK, 0 rows affected (0.00 sec)
|
当前session对不存在的记录加for update的锁:
mysql> select * from emp where empid = 102 for update;
Empty set (0.00 sec)
|
|
这时,如果其他session插入empid为201的记录(注意:这条记录并不存在),也会出现锁等待:
mysql>insert into emp(empid,...) values(201,...);
阻塞等待
|
|
Session_1 执行rollback:
mysql> rollback;
Query OK, 0 rows affected (13.04 sec)
|
|
由于其他session_1回退后释放了Next-Key锁,当前session可以获得锁并成功插入记录:
mysql>insert into emp(empid,...) values(201,...);
Query OK, 1 row affected (13.35 sec)
|
注:关于Innodb什么情况下使用行锁,什么情况使用表锁,上面的例子介绍的通俗易通,适合初级DBA学习。原来出自http://brilon.iteye.com/blog/433726,欢迎交流沟通,谢谢!