怎么理解mysql innodb的行锁方式

35次阅读
没有评论

共计 1167 个字符,预计需要花费 3 分钟才能阅读完成。

本篇内容主要讲解“怎么理解 mysql innodb 的行锁方式”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让丸趣 TV 小编来带大家学习“怎么理解 mysql innodb 的行锁方式”吧!

真正的方式是:基于索引找到对应的行(不是使用所有的 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

到此,相信大家对“怎么理解 mysql innodb 的行锁方式”有了更深的了解,不妨来实际操作一番吧!这里是丸趣 TV 网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-19发表,共计1167字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)