您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關MYSQL中怎么實現偽行級鎖,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
一.數據準備
mysql> use test;
Database changed
mysql> show create table t_kenyon \G
*************************** 1. row ***************************
Table: t_kenyon
Create Table: CREATE TABLE `t_kenyon` (
`id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql> set autocommit = 0;
Query OK, 0 rows affected (0.00 sec)
www.2cto.com
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 |
| 789 |
| 345 |
| 78 |
| 78 |
+------+
6 rows in set (0.00 sec)
以上是測試表t_kenyon,設置提交方式為手動提交.
二.過程(開啟兩個session,分別設置autocommit=off)
www.2cto.com
1.session one update
mysql> 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 |
| 789 |
| 345 |
| 78 |
| 78 |
+------+
6 rows in set (0.00 sec)
2.session two update
mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | OFF |
+---------------+-------+
1 row in set (0.00 sec)
www.2cto.com
mysql> select * from t_kenyon;
+------+
| id |
+------+
| 1 |
| 123 |
| 789 |
| 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
www.2cto.com
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 |
| 789 |
| 999 |
+------+
6 rows in set (0.00 sec)
4.session two update
mysql> select * from t_kenyon;
+------+
| id |
+------+
| 1 |
| 78 |
| 78 |
| 123 |
| 345 |
| 789 |
+------+
6 rows in set (0.00 sec)
www.2cto.com
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 |
| 789 |
| 7777 |
+------+
6 rows in set (0.00 sec)
執行計劃
mysql> explain select * from t_kenyon where id = 345 \G
*************************** 1. row ***************************
id: 1 www.2cto.com
select_type: SIMPLE
table: t_kenyon
type: ref
possible_keys: ind_kenyon
key: ind_kenyon
key_len: 5
ref: const
rows: 1
Extra: Using where; Using index
1 row in set (0.00 sec)
可以看到加了索引后,不同的數據更新并沒有被阻塞,實現了真正意義上行鎖
以上就是MYSQL中怎么實現偽行級鎖,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。