mysql行级锁实现原理是什么

coldplay.xixi
发布: 2020-10-26 15:47:23
原创
6440人浏览过
mysql行级锁实现原理:1、InnoDB行锁是通过给索引项加锁来实现的,这一点mysql和 oracle不同;2、InnoDB这种行级锁决定,只有通过索引条件来检索数据,才能使用行级锁,否则, 直接使用表级锁。

mysql行级锁实现原理是什么

mysql行级锁实现原理:

锁是在执行多线程时用于强行限定资源访问的同步机制,数据库锁根据锁的粒度可分为行级锁, 表级锁和页级锁

行级锁

行级锁是mysql中粒度最细的一种锁机制,表示只对当前所操作的行进行加锁,行级锁发生冲突 的概率很低,其粒度最小,但是加锁的代价最大。行级锁分为共享锁和排他锁。

特点:

开销大,加锁慢,会出现死锁;锁定粒度最小,发生锁冲突的概率最大,并发性也高;

实现原理:

InnoDB行锁是通过给索引项加锁来实现的,这一点mysql和oracle不同,后者是通过在数据库中 对相应的数据行加锁来实现的,InnoDB这种行级锁决定,只有通过索引条件来检索数据,才能使用行 级锁,否则,直接使用表级锁。

特别注意: 使用行级锁一定要使用索引

举个栗子:

创建表结构

CREATE TABLE `developerinfo` (
  `userID` bigint(20) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  `passWord` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`userID`),
  KEY `PASSWORD_INDEX` (`passWord`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
登录后复制

插入数据

INSERT INTO `developerinfo` VALUES ('1', 'liujie', 

'123456');
INSERT INTO `developerinfo` VALUES ('2', 'yitong', '123');
INSERT INTO `developerinfo` VALUES ('3', 'tong', 

'123456');
登录后复制

(1)通过主键索引来查询数据库使用行锁

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

命令行窗口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行级锁实现原理是什么的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号