Mysql InnoDB行锁实现方式

只有通过索引条件检索数据,InnoDB才使用行级锁,否则,InnoDB将使用表锁!

新建表并插入数据

 
  
create table test(id int,name varchar(10)) engine=innodb;
insert into test values(1,'1'),(2,'2'),(3,'3'),(4,'4');


 
  

session_1

session_2

mysql> set autocommit=0;

Query OK, 0 rows affected (0.00 sec)

mysql> select * from test where id = 1 ;

+------+------+

| id   | name |

+------+------+

| 1    | 1    |

+------+------+

1 row in set (0.00 sec)

mysql> set autocommit=0;

Query OK, 0 rows affected (0.00 sec)

mysql> select * from test where id = 2 ;

+------+------+

| id   | name |

+------+------+

| 2    | 2    |

+------+------+

1 row in set (0.00 sec)

mysql> select * from tab_no_index where id = 1 for update;

+------+------+

| id   | name |

+------+------+

| 1    | 1    |

+------+------+

1 row in set (0.00 sec)

 

 

mysql> select * from tab_no_index where id = 2 for update;

等待

创建一个id索引即可实现行及锁

你可能感兴趣的:(Mysql InnoDB行锁实现方式)