MySQL 5.7 SELECT ... LOCK IN SHARE MODE|FOR UPDATE語句說明
在相同的事務中,如果你在查詢資料後,想插入或更新相關的資料,常規的SELECT語句無法提供足夠的保護。
其他事務可以更新或刪除你所查詢到的行。InnoDB引擎支援兩種讀鎖來提供額外的安全。
一、SELECT ... LOCK IN SHARE MODE在查詢到的行上設定共享鎖。其他回話可以讀取到這些行,但是不能修改這些行,直到提交事務後。
如果將LOCK IN SHARE MODE用在支援頁鎖或行鎖的儲存引擎上,查詢所覆蓋的行會被加上共享鎖,共享鎖允許其他事務讀,但是不允許其他事務更新或刪除這些行。
檢視測試表中的資料,表中只有一個欄位,且欄位為主鍵
mysql> desc t5;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| id | tinyint(3) unsigned | NO | PRI | NULL | |
+-------+---------------------+------+-----+---------+-------+
1 row in set (0.01 sec)
mysql> show create table t5;
+-------+----------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------+
| t5 | CREATE TABLE `t5` (
`id` tinyint(3) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select * from t5;
+----+
| id |
+----+
| 10 |
| 20 |
| 30 |
| 40 |
| 50 |
+----+
5 rows in set (0.00 sec)
會話1,開啟一個事務,並對id範圍為10到30的資料增加共享鎖
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from t5 where id >= 10 and id <=30 lock in share mode;
+----+
| id |
+----+
| 10 |
| 20 |
| 30 |
+----+
3 rows in set (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> delete from t5 where id=30;
Query OK, 1 row affected (15.31 sec)
會話1,提交事務
mysql> commit;
Query OK, 0 rows affected (0.06 sec)
會話2,之前阻塞的DELETE操作會生效
mysql> delete from t5 where id=30;
Query OK, 1 row affected (15.31 sec)
二、如果將FOR UPDATE用在支援頁鎖或行鎖的儲存引擎上,查詢所覆蓋的行會被加上寫鎖,直到當前事務的結束。
其他回話可以讀取到這些行,但是不能修改這些行,直到提交事務後。
會話1,開啟一個事務,並對id範圍為10到30的資料增加寫鎖
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from t5 where id >= 10 and id <=30 for update;
+----+
| id |
+----+
| 10 |
| 20 |
| 30 |
+----+
3 rows in set (0.00 sec)
會話2,可以執行SELECT、INSERT操作,但是對會話1鎖定的行執行UPDATE操作會阻塞,直到超過InnoDB鎖的等待時間,會話2會自動回滾
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
+----+
| 10 |
| 20 |
| 30 |
| 40 |
| 50 |
| 60 |
+----+
6 rows in set (0.00 sec)
mysql> insert into t5 values(70);
Query OK, 1 row affected (0.04 sec)
其他事務可以更新或刪除你所查詢到的行。InnoDB引擎支援兩種讀鎖來提供額外的安全。
一、SELECT ... LOCK IN SHARE MODE在查詢到的行上設定共享鎖。其他回話可以讀取到這些行,但是不能修改這些行,直到提交事務後。
如果將LOCK IN SHARE MODE用在支援頁鎖或行鎖的儲存引擎上,查詢所覆蓋的行會被加上共享鎖,共享鎖允許其他事務讀,但是不允許其他事務更新或刪除這些行。
檢視測試表中的資料,表中只有一個欄位,且欄位為主鍵
mysql> desc t5;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| id | tinyint(3) unsigned | NO | PRI | NULL | |
+-------+---------------------+------+-----+---------+-------+
1 row in set (0.01 sec)
mysql> show create table t5;
+-------+----------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------+
| t5 | CREATE TABLE `t5` (
`id` tinyint(3) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select * from t5;
+----+
| id |
+----+
| 10 |
| 20 |
| 30 |
| 40 |
| 50 |
+----+
5 rows in set (0.00 sec)
會話1,開啟一個事務,並對id範圍為10到30的資料增加共享鎖
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from t5 where id >= 10 and id <=30 lock in share mode;
+----+
| id |
+----+
| 10 |
| 20 |
| 30 |
+----+
3 rows in set (0.00 sec)
會話2,可以執行SELECT、INSERT操作,但是對會話1鎖定的行執行UPDATE操作會阻塞,直到超過InnoDB鎖的等待時間,會話2會自動回滾
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
+----+
| 10 |
| 20 |
| 30 |
| 40 |
| 50 |
| 60 |
+----+
6 rows in set (0.00 sec)
mysql> insert into t5 values(60);
Query OK, 1 row affected (0.04 sec)
mysql> update t5 set id=100 where id=10;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
InnDB事務等待行鎖的時間長度,預設值是50秒
mysql> show variables like 'innodb_lock_wait_timeout';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| innodb_lock_wait_timeout | 50 |
+--------------------------+-------+
1 row in set (0.01 sec)
會話2,對會話1鎖定的行執行DELETE操作會阻塞
mysql> start transaction;mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
+----+
| 10 |
| 20 |
| 30 |
| 40 |
| 50 |
| 60 |
+----+
6 rows in set (0.00 sec)
mysql> insert into t5 values(60);
Query OK, 1 row affected (0.04 sec)
mysql> update t5 set id=100 where id=10;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
InnDB事務等待行鎖的時間長度,預設值是50秒
mysql> show variables like 'innodb_lock_wait_timeout';
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| innodb_lock_wait_timeout | 50 |
+--------------------------+-------+
1 row in set (0.01 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> delete from t5 where id=30;
Query OK, 1 row affected (15.31 sec)
會話1,提交事務
mysql> commit;
Query OK, 0 rows affected (0.06 sec)
mysql> delete from t5 where id=30;
Query OK, 1 row affected (15.31 sec)
mysql> rollback;
Query OK, 0 rows affected (0.02 sec)二、如果將FOR UPDATE用在支援頁鎖或行鎖的儲存引擎上,查詢所覆蓋的行會被加上寫鎖,直到當前事務的結束。
其他回話可以讀取到這些行,但是不能修改這些行,直到提交事務後。
會話1,開啟一個事務,並對id範圍為10到30的資料增加寫鎖
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from t5 where id >= 10 and id <=30 for update;
+----+
| id |
+----+
| 10 |
| 20 |
| 30 |
+----+
3 rows in set (0.00 sec)
mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)
+----+
| 10 |
| 20 |
| 30 |
| 40 |
| 50 |
| 60 |
+----+
6 rows in set (0.00 sec)
mysql> insert into t5 values(70);
Query OK, 1 row affected (0.04 sec)
mysql> update t5 set id=100 where id=10;
會話1,提交事務
mysql> commit;
Query OK, 0 rows affected (0.06 sec)
會話2,之前阻塞的UPDATE操作會執行成功
mysql> update t5 set id=100 where id=10;
Query OK, 1 row affected (15.38 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> rollback;
Query OK, 0 rows affected (0.04 sec)
會話1,提交事務
mysql> commit;
Query OK, 0 rows affected (0.06 sec)
會話2,之前阻塞的UPDATE操作會執行成功
mysql> update t5 set id=100 where id=10;
Query OK, 1 row affected (15.38 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> rollback;
Query OK, 0 rows affected (0.04 sec)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-2123438/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 深入理解SELECT ... LOCK IN SHARE MODE和SELECT ... FOR UPDATE
- MySQL 共享鎖 (lock in share mode),排他鎖 (for update)MySql
- SQLite語句(二):INSERT DELETE UPDATE SELECTSQLitedelete
- DBeaver如何生成select,update,delete,insert語句delete
- mysql update join,insert select 語法MySql
- MySQL -update語句流程總結MySql
- MySQL的update語句避坑MySql
- 如何在MySQL 5.7中使用SELECT … INTO語句匯出資料檔案?MySql
- 學習MySQL的select語句MySql
- MySQL 5.7 mysqlpump 備份工具說明MySql
- mysql5.7 General tablespace使用說明MySql
- insert into select語句與select into from語句
- SQL SELECT 語句SQL
- 【SQL】14 UNION 操作符、SELECT INTO 語句、INSERT INTO SELECT 語句、CREATE DATABASE 語句、CREATE TABLE 語句SQLDatabase
- MySQL5.7 Waiting for global read lockMySqlAI
- Mysql跨表更新 多表update sql語句總結MySql
- Innodb:為什麼lock in share mode在show engine看不到行鎖資訊
- select for update
- ORACLE LOCK MODE 1.2.3.4.5.6Oracle
- 【SQL】10 SQL UPDATE 語句SQL
- Go select語句詳解Go
- SQL語言基礎(SELECT語句)SQL
- ORACLE多表關聯UPDATE語句Oracle
- MySQL5.7中的sql_mode預設值MySql
- 當執行一條select語句時,MySQL到底做了啥?MySql
- MySQL 5.7 新備份工具mysqlpump 使用說明 - 運維小結MySql運維
- 圖解Go select語句原理圖解Go
- Select語句執行順序
- MySQL中SELECT+UPDATE併發更新問題MySql
- MySQL 5.7從庫system lock執行緒解釋MySql執行緒
- Jtti:MySQL 資料庫中update語句會不會發生死鎖JttiMySql資料庫
- 深入解讀MySQL InnoDB儲存引擎Update語句執行過程MySql儲存引擎
- 說說 Python 的 if 語句Python
- TPCH模型規範、測試說明及22條語句模型
- mysql語句MySql
- 記一次 MySQL select for update 死鎖問題MySql
- 一個UPDATE語句引發的血案
- 資料庫學習(一)——select語句資料庫
- mySql常用語句MySql