前言:
insert into t2 select * from t1;
這條語句會對查詢表 t1 加鎖嗎?不要輕易下結論。對GreatSQL的鎖進行研究之前,首先要確認一下事務的隔離級別,不同的事務隔離級別,鎖的表現是不一樣的。
實驗:
建立測試表t1,t2
greatsql> create table t1(id int primary key ,c1 varchar(10),c2 datetime,key idx_c1(c1));
greatsql> create table t2 like t1;
# id 列為主鍵,c1列上有普通索引
建立儲存過程,向t1表插入測試資料
greatsql> delimiter //
CREATE or replace PROCEDURE p1()
BEGIN
DECLARE p1 int default 0;
while p1<5 do
insert into t1(id,c1,c2) values(p1*2,round(rand()*10000),now());
SET p1 = p1 + 1;
end while;
END;
//
delimiter ;
greatsql> call p1;
greatsql> select * from t1;
+----+------+---------------------+
| id | c1 | c2 |
+----+------+---------------------+
| 0 | 2660 | 2024-02-21 15:45:00 |
| 2 | 4627 | 2024-02-21 15:45:00 |
| 4 | 5158 | 2024-02-21 15:45:00 |
| 6 | 1907 | 2024-02-21 15:45:00 |
| 8 | 4061 | 2024-02-21 15:45:00 |
+----+------+---------------------+
5 rows in set (0.01 sec)
REPEATABLE-READ隔離級別:
查詢當前事務隔離級別:
greatsql> show variables like 'transaction_isolation';
+-----------------------+-----------------+
| Variable_name | Value |
+-----------------------+-----------------+
| transaction_isolation | REPEATABLE-READ |
+-----------------------+-----------------+
1 row in set (0.00 sec)
connection 1:
greatsql> select ps_current_thread_id();
+------------------------+
| ps_current_thread_id() |
+------------------------+
| 92 |
+------------------------+
1 row in set (0.00 sec)
greatsql> begin;
Query OK, 0 rows affected (0.00 sec)
greatsql> insert into t2 select * from t1;
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
connection2:
greatsql> select ps_current_thread_id();
+------------------------+
| ps_current_thread_id() |
+------------------------+
| 93 |
+------------------------+
1 row in set (0.00 sec)
greatsql> begin;
Query OK, 0 rows affected (0.01 sec)
greatsql> insert into t1(id,c1) values(1,'a');
connection3:
greatsql> select THREAD_ID,OBJECT_NAME,INDEX_NAME,LOCK_TYPE,LOCK_MODE,LOCK_STATUS,LOCK_DATA from data_locks;
+-----------+-------------+------------+-----------+------------------------+-------------+------------------------+
| THREAD_ID | OBJECT_NAME | INDEX_NAME | LOCK_TYPE | LOCK_MODE | LOCK_STATUS | LOCK_DATA |
+-----------+-------------+------------+-----------+------------------------+-------------+------------------------+
| 93 | t1 | NULL | TABLE | IX | GRANTED | NULL |
| 93 | t1 | PRIMARY | RECORD | X,GAP,INSERT_INTENTION | WAITING | 2 |
| 92 | t2 | NULL | TABLE | IX | GRANTED | NULL |
| 92 | t1 | NULL | TABLE | IS | GRANTED | NULL |
| 92 | t1 | PRIMARY | RECORD | S | GRANTED | supremum pseudo-record |
| 92 | t1 | PRIMARY | RECORD | S | GRANTED | 0 |
| 92 | t1 | PRIMARY | RECORD | S | GRANTED | 2 |
| 92 | t1 | PRIMARY | RECORD | S | GRANTED | 4 |
| 92 | t1 | PRIMARY | RECORD | S | GRANTED | 6 |
| 92 | t1 | PRIMARY | RECORD | S | GRANTED | 8 |
+-----------+-------------+------------+-----------+------------------------+-------------+------------------------+
10 rows in set (0.00 sec)
connection1
的語句中select的表t1上每條記錄及最大偽記錄supremum pseudo-record
都加了S鎖,這個S鎖是nextkey lock鎖
,當connection2
試圖向t1表中插入一條表中不存在的資料時也會被阻塞,connect1
的S鎖與connect2
需要的 X,GAP,INSERT_INTENTION鎖
不相容。
在 REPEATABLE-READ 隔離級別下,INSERT ... SELECT
操作並未採用MVCC來保證事務一致性和隔離性,而是使用了鎖機制。
加鎖的目的是確保事務在讀取資料時能夠看到一個一致的資料快照。如果在執行 INSERT ... SELECT
時不加鎖,那麼可能會出現以下情況:
- 不可重複讀:如果在
INSERT ... SELECT
執行期間,另一個事務修改了被查詢的資料,那麼 INSERT ... SELECT 可能會讀取到不同的資料,導致插入的資料不一致。 - 幻讀:在某些情況下,另一個事務可能會在
INSERT ... SELECT
執行期間插入新的行,導致插入操作插入到不應該插入的行。
透過加鎖,InnoDB 能夠確保 INSERT ... SELECT
語句在執行期間讀取到的資料是一致的,並且不會被其他事務修改,從而維護了事務的隔離性和一致性。儘管 MVCC 可以在大多數情況下提供高效的資料讀取和寫入,但它並不能完全替代鎖機制。在 INSERT ... SELECT
這樣的操作中,使用 MVCC 可能無法提供足夠的保證。
READ-COMMITTED隔離級別
查詢當前事務隔離級別:
greatsql> show variables like 'transaction_isolation';
+-----------------------+----------------+
| Variable_name | Value |
+-----------------------+----------------+
| transaction_isolation | READ-COMMITTED |
+-----------------------+----------------+
1 row in set (0.00 sec)
connection 1
greatsql> begin;
Query OK, 0 rows affected (0.00 sec)
greatsql> insert into t2 select * from t1;
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0
connection 2
greatsql> begin;
Query OK, 0 rows affected (0.00 sec)
greatsql> insert into t1(id,c1) values(1,'a');
Query OK, 1 row affected (0.00 sec)
connection3
greatsql> select THREAD_ID,OBJECT_NAME,INDEX_NAME,LOCK_TYPE,LOCK_MODE,LOCK_STATUS,LOCK_DATA from data_locks;
+-----------+-------------+------------+-----------+-----------+-------------+-----------+
| THREAD_ID | OBJECT_NAME | INDEX_NAME | LOCK_TYPE | LOCK_MODE | LOCK_STATUS | LOCK_DATA |
+-----------+-------------+------------+-----------+-----------+-------------+-----------+
| 104 | t1 | NULL | TABLE | IX | GRANTED | NULL |
| 103 | t2 | NULL | TABLE | IX | GRANTED | NULL |
+-----------+-------------+------------+-----------+-----------+-------------+-----------+
2 rows in set (0.00 sec)
可以看出事務隔離級別設定為READ-COMMITTED後,表現截然不同。connection2並沒有被阻塞,兩個會話持有的鎖都只有插入表意向排他鎖(IX)。
結論:
INSERT...SELECT
語句是否對查詢表加鎖跟事務隔離級別有關,REPEATABLE-READ隔離級別下加共享讀鎖,此共享讀鎖屬於Nextkey lock,會影響其他事務對查詢表的DML操作;READ-COMMITTED下不加鎖,不影響其他事務對錶進行DML操作。
Enjoy GreatSQL 😃
關於 GreatSQL
GreatSQL是適用於金融級應用的國內自主開源資料庫,具備高效能、高可靠、高易用性、高安全等多個核心特性,可以作為MySQL或Percona Server的可選替換,用於線上生產環境,且完全免費併相容MySQL或Percona Server。
相關連結: GreatSQL社群 Gitee GitHub Bilibili
GreatSQL社群:
社群部落格有獎徵稿詳情:https://greatsql.cn/thread-100-1-1.html
技術交流群:
微信:掃碼新增
GreatSQL社群助手
微信好友,傳送驗證資訊加群
。