Mysql InnoDB行锁实现方式

来源:互联网 发布:淘宝导航栏是多少尺寸 编辑:程序博客网 时间:2024/06/04 18:16

只有通过索引条件检索数据,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索引即可实现行及锁
0 1
原创粉丝点击