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
- MySQL 5.7 EXPLAIN EXTENDED語句說明MySqlAI
- Select for update/lock in share mode 對事務併發性影響
- MySQL 5.5 INSERT ... ON DUPLICATE KEY UPDATE語句說明MySql
- Mysql加鎖過程詳解(4)-select for update/lock in share mode 對事務併發性影響MySql
- 【MySQL】select for update 的Row Lock 與Table LockMySql
- SQL update select語句SQL
- MySQL 5.7 複製控制語句SET GLOBAL sql_slave_skip_counter說明MySql
- MySQL 5.7 使用 SELECT ... INTO 語句匯出資料檔案MySql
- MySQL 的CASE WHEN 語句使用說明MySql
- mysql語句分析工具explain使用說明MySqlAI
- SQLite語句(二):INSERT DELETE UPDATE SELECTSQLitedelete
- mysql5.7 General tablespace使用說明MySql
- MySQL 5.7 mysqlpump 備份工具說明MySql
- MySQL 5.6 sql_mode常用值說明MySql
- MySQL -update語句流程總結MySql
- MySQL的update語句避坑MySql
- MySql與Sql Server Update語句MySqlServer
- MySQL 5.7 LIMIT語句介紹MySqlMIT
- MySQL 5.7 REPLACE語句的用法MySql
- DBeaver如何生成select,update,delete,insert語句delete
- mysql update join,insert select 語法MySql
- 如何在MySQL 5.7中使用SELECT … INTO語句匯出資料檔案?MySql
- 學習MySQL的select語句MySql
- MySQL 5.7 PREPARE、EXECUTE、DEALLOCATE語句介紹MySql
- MySQL 5.7 自增欄位相關引數說明MySql
- mysql 語句不能update的解決辦法MySql
- mysql操作命令梳理(5)-執行sql語句查詢即mysql狀態說明MySql
- Mysql跨表更新 多表update sql語句總結MySql
- MySQL 5.7 的事務控制語句的介紹MySql
- MySQL 5.5 LOCK TABLES 和 UNLOCK TABLES 語句介紹MySql
- MySQL 5.5 FLUSH TABLES WITH READ LOCK語句介紹MySql
- SQL SELECT 語句SQL
- Go select 語句Go
- TM LOCK MODE
- 【SQL】14 UNION 操作符、SELECT INTO 語句、INSERT INTO SELECT 語句、CREATE DATABASE 語句、CREATE TABLE 語句SQLDatabase
- MysqL_select for update鎖詳解MySql