您的位置:宽带测速网 > mysql教程 > 怎么理解mysql innodb的行锁方式

怎么理解mysql innodb的行锁方式

2025-06-22 17:05来源:互联网 [ ]

真正的方式是: 基于索引找到对应的行(不是使用所有的where条件找到对应的行), 然后基于行数据加锁。
root@sakila 10:27:15>show indexes from tab_no_index \G;
*************************** 1. row ***************************
Table: tab_no_index
Non_unique: 1
Key_name: id
Seq_in_index: 1
Column_name: id
Collation: A
Cardinality: 6
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
*************************** 2. row ***************************
Table: tab_no_index
Non_unique: 1
Key_name: name
Seq_in_index: 1
Column_name: name
Collation: A
Cardinality: 6
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
2 rows in set (0.00 sec)

会话1:
root@sakila 10:26:17>select * from tab_no_index where id=1 for update;
+------+------+
| id| name |
+------+------+
|1 | 1|
|1 | 4|
+------+------+
2 rows in set (0.00 sec)

会话2:通过name index 找到 id=1 and name=4 的这条记录,想加锁,但是不成功,因为该条记录已经被会话1加锁了。
root@sakila 10:27:04>select * from tab_no_index where name='4' for update;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction