视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
MYSQL的伪行级锁_MySQL
2020-11-09 18:18:13 责编:小采
文档


bitsCN.com
MYSQL的伪行级锁 之前一直以为mysql的innodb引擎所支持的行级锁和oracle,postgresql是一样的,是对数据行上加锁。但其实是不一样的,理解不一样,对mysql的锁机制就容易产生误解。innodb的行级锁实际上是基于索引项来锁定的。以下是验证测试过程 一.数据准备mysql> use test;Database changedmysql> show create table t_kenyon /G*************************** 1. row *************************** Table: t_kenyonCreate Table: CREATE TABLE `t_kenyon` ( `id` int(11) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf81 row in set (0.00 sec) mysql> set autocommit = 0;Query OK, 0 rows affected (0.00 sec) mysql> show variables like '%autocommit%';+---------------+-------+| Variable_name | Value |+---------------+-------+| autocommit | OFF |+---------------+-------+1 row in set (0.00 sec) mysql> select * from t_kenyon;+------+| id |+------+| 1 || 123 || 7 || 345 || 78 || 78 |+------+6 rows in set (0.00 sec)以上是测试表t_kenyon,设置提交方式为手动提交. 二.过程(开启两个session,分别设置autocommit=off) 1.session one updatemysql> update t_kenyon set id = 999 where id = 1;Query OK, 1 row affected (0.04 sec)Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from t_kenyon;+------+| id |+------+| 999 || 123 || 7 || 345 || 78 || 78 |+------+6 rows in set (0.00 sec)2.session two updatemysql> show variables like 'autocommit';+---------------+-------+| Variable_name | Value |+---------------+-------+| autocommit | OFF |+---------------+-------+1 row in set (0.00 sec) mysql> select * from t_kenyon;+------+| id |+------+| 1 || 123 || 7 || 345 || 78 || 78 |+------+6 rows in set (0.00 sec) mysql> update t_kenyon set id = 88888 where id = 345;第二个session更新的值是345,但是也一直被阻塞,直到session1被rollback或者commit,如果session1未做回滚或者提交,session2中的该阻塞在超出mysql的锁时间时自动回滚,该参数为innodb_lock_wait_timeout,默认值50秒 现象如下ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction加索引后的测试 3.session one update mysql> create index ind_kenyon on t_kenyon(id);Query OK, 0 rows affected (28.58 sec)Records: 0 Duplicates: 0 Warnings: 0 mysql> update t_kenyon set id = 999 where id = 1;Query OK, 1 row affected (0.03 sec)Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from t_kenyon;+------+| id |+------+| 78 || 78 || 123 || 345 || 7 || 999 |+------+6 rows in set (0.00 sec)4.session two update mysql> select * from t_kenyon;+------+| id |+------+| 1 || 78 || 78 || 123 || 345 || 7 |+------+6 rows in set (0.00 sec) mysql> update t_kenyon set id = 7777 where id = 345;Query OK, 1 row affected (0.03 sec)Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from t_kenyon;+------+| id |+------+| 1 || 78 || 78 || 123 || 7 || 7777 |+------+6 rows in set (0.00 sec)执行计划mysql> explain select * from t_kenyon where id = 345 /G*************************** 1. row *************************** id: 1 select_type: SIMPLE table: t_kenyon type: refpossible_keys: ind_kenyon key: ind_kenyon key_len: 5 ref: const rows: 1 Extra: Using where; Using index1 row in set (0.00 sec)可以看到加了索引后,不同的数据更新并没有被阻塞,实现了真正意义上行锁 三.行级锁的扩展 参考:http:///database/201208/145888.html 作者 kenyon bitsCN.com

下载本文
显示全文
专题