mysql行级锁实现原理是什么


当前第2页 返回上一页

打开三个命令行窗口进行测试

命令行窗口1命令行窗口2命令行窗口3
mysql> set autocommit = 0;
Query OK, 0 rows affected
mysql> select * from developerinfo where userid = '1' for update;
+-------- +--------+----------+
| userID | name | passWord |
+--------+-------- +----------+
| 1 | liujie | 123456 |
+-------- +--------+----------+
1 row in set
mysql> set autocommit = 0;
Query OK, 0 rows affected

mysql> select * from developerinfo where userid = '1' for update;

等待

mysql> set autocommit = 0;
Query OK, 0 rows affected
mysql> select * from developerinfo where userid = '3' for update;
+--------+------+----------+
| userID | name | passWord |
+-------- +------+----------+
| 3 | tong | 123456 |
+-------- +------+----------+
1 row in set
mysql> commit;
Query OK, 0 rows affected
mysql> select * from developerinfo where userid = '1' for update;
+--------+-------- +----------+
| userID | name | passWord |
+--------+-------- +----------+
| 1 | liujie | 123456 |
+-------- +--------+----------+
1 row in set


(2)查询非索引的字段来查询数据库使用行锁

打开两个命令行窗口进行测试

命令行窗口1命令行窗口2
mysql> set autocommit=0;
Query OK, 0 rows affected
mysql> select * from developerinfo where name = 'liujie' for update;
+-------- +--------+----------+
| userID | name | passWord |
+--------+-------- +----------+
| 1 | liujie | 123456 |
+-------- +--------+----------+
1 row in set
mysql> set autocommit=0;
Query OK, 0 rows affected
mysql> select * from developerinfo where name = 'tong' for update;

等待

mysql> commit;
Query OK, 0 rows affected
mysql> select * from developerinfo where name = 'liujie' for update;
+--------+--------+----------+
| userID | name | passWord |
+--------+--------+----------+
| 1 | liujie | 123456 |
+--------+--------+----------+
1 row in set

(3)查询非唯一索引字段来查询数据库使用行锁锁住多行

mysql的行锁是针对索引假的锁,不是针对记录,所以可能会出现锁住不同记录的场景

打开三个命令行窗口进行测试

命令行窗口1命令行窗口2命令行窗口3
mysql> set autocommit=0;
Query OK, 0 rows affected
mysql> select * from developerinfo where password = '123456
' for update;
+--------+--------+----------+
| userID | name | passWord |
+-------- +--------+----------+
| 1 | liujie | 123456 |
| 3 | tong | 123456 |
+--------+--------+---------- +
2 rows in set
mysql> set autocommit =0 ;
Query OK, 0 rows affected

mysql> select * from developerinfo where userid = '1' for update;

等待

mysql> set autocommit = 0;
Query OK, 0 rows affected
mysql> select * from developerinfo where userid = '2
' for update;
+--------+--------+----------+
| userID | name | passWord |
+--------+--------+----------+
| 2 | yitong | 123 |
+--------+--------+----------+
1 row in set
commit;mysql> select * from developerinfo where userid = '1' for update;
+--------+--------+----------+
| userID | name | passWord |
+--------+--------+----------+
| 1 | liujie | 123456 |
+--------+--------+----------+
1 row in set

(4)条件中使用索引来操作检索数据库时,是否使用索引还需有mysql通过判断不同执行计划来 决定,是否使用该索引,如需判定如何使用explain来判断索引,请听下回分解

更多相关免费学习推荐:mysql教程(视频)

以上就是mysql行级锁实现原理是什么的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

mysql怎么设置行值唯一?

mysql如何增加表中新的列?

mysql中where与on的区别及何时使用详析

怎么查到mysql的账号密码是什么?

mysql分区表管理命令汇总

mysql中什么是临时表

mysql系列之十三 mysql的复制

mysql统计一天产生多少条记录

mysql中的router是什么

mysql中min()和least()的区别

更多相关阅读请进入《mysql》频道 >>


数据库系统概念 第6版
书籍

数据库系统概念 第6版

机械工业出版社

本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...