關於MySQL的加鎖機制,其實十分複雜,不同的隔離級別,是否是主鍵或索引,鎖的粒度等等。很多工作了很多年的MySQL DBA也不能把各種加鎖場景一一講清楚。有時候一個簡單的鎖等待場景都值得深入研究,大家更多的是知其然而不知其所以然。本文介紹的是一個很常見的鎖等待問題,但很少有人知道其中的原理。
一、實驗場景
本文實驗和研究的MySQL版本為8.0.31,資料庫的隔離級別設定為RC,建立一張表,並在表中插入資料:
create table siri( id int not null auto_increment, a int not null, b int not null, c int not null, primary key (id), unique key uniq_a (a), key idx_c (c) ) insert into siri values (1,1,1,1),(2,2,2,2),(4,4,4,4),(6,6,6,4);
好的,現在可以開始模擬實驗場景了:
實驗一:
Session1 |
Session2 |
mysql> begin; Query OK, 0 rows affected (0.00 sec)
mysql> select * from siri where b=1 for update; +----+---+---+---+ | id | a | b | c | +----+---+---+---+ | 1 | 1 | 1 | 1 | +----+---+---+---+ 1 row in set (0.00 sec) |
|
mysql> select * from siri where b=4 for update; ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction |
Session1 |
Session2 |
mysql> begin; Query OK, 0 rows affected (0.00 sec)
mysql> select * from siri where id=1 for update; +----+---+---+---+ | id | a | b | c | +----+---+---+---+ | 1 | 1 | 1 | 1 | +----+---+---+---+ 1 row in set (0.00 sec) |
|
mysql> select * from siri where b=4 for update; ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction |
實驗三:
Session1 |
Session2 |
mysql> begin; Query OK, 0 rows affected (0.00 sec)
mysql> select * from siri where b=1 for update; +----+---+---+---+ | id | a | b | c | +----+---+---+---+ | 1 | 1 | 1 | 1 | +----+---+---+---+ 1 row in set (0.00 sec) |
|
mysql> select * from siri where id=4 for update; +----+---+---+---+ | id | a | b | c | +----+---+---+---+ | 4 | 4 | 4 | 4 | +----+---+---+---+ 1 row in set (0.00 sec) |
mysql> select * from performance_schema.data_locks\G *************************** 1. row *************************** ENGINE: INNODB ENGINE_LOCK_ID: 139907486244056:1220:139907418869440 ENGINE_TRANSACTION_ID: 3816000 THREAD_ID: 52900 EVENT_ID: 44 OBJECT_SCHEMA: test OBJECT_NAME: siri PARTITION_NAME: NULL SUBPARTITION_NAME: NULL INDEX_NAME: NULL OBJECT_INSTANCE_BEGIN: 139907418869440 LOCK_TYPE: TABLE LOCK_MODE: IX LOCK_STATUS: GRANTED LOCK_DATA: NULL *************************** 2. row *************************** ENGINE: INNODB ENGINE_LOCK_ID: 139907486244056:59:4:2:139907418866384 ENGINE_TRANSACTION_ID: 3816000 THREAD_ID: 52900 EVENT_ID: 44 OBJECT_SCHEMA: test OBJECT_NAME: siri PARTITION_NAME: NULL SUBPARTITION_NAME: NULL INDEX_NAME: PRIMARY OBJECT_INSTANCE_BEGIN: 139907418866384 LOCK_TYPE: RECORD LOCK_MODE: X,REC_NOT_GAP LOCK_STATUS: GRANTED LOCK_DATA: 1 2 rows in set (0.00 sec)
mysql> select * from sys.innodb_lock_waits\G *************************** 1. row *************************** wait_started: 2023-11-16 14:23:49 wait_age: 00:00:02 wait_age_secs: 2 locked_table: `test`.`siri` locked_table_schema: test locked_table_name: siri locked_table_partition: NULL locked_table_subpartition: NULL locked_index: PRIMARY locked_type: RECORD waiting_trx_id: 3816028 waiting_trx_started: 2023-11-16 14:23:49 waiting_trx_age: 00:00:02 waiting_trx_rows_locked: 1 waiting_trx_rows_modified: 0 waiting_pid: 54820 waiting_query: select * from siri where b=4 for update waiting_lock_id: 139907486245672:59:4:2:139907418878432 waiting_lock_mode: X,REC_NOT_GAP blocking_trx_id: 3816020 blocking_pid: 54783 blocking_query: NULL blocking_lock_id: 139907486244056:59:4:2:139907418866384 blocking_lock_mode: X,REC_NOT_GAP blocking_trx_started: 2023-11-16 14:16:49 blocking_trx_age: 00:07:02 blocking_trx_rows_locked: 1 blocking_trx_rows_modified: 0 sql_kill_blocking_query: KILL QUERY 54783 sql_kill_blocking_connection: KILL 54783 1 row in set (0.01 sec)
查詢上面監控檢視可以發現,在實驗一和實驗二中,session1所申請的鎖資源也是一樣的,一個是表級別的IX鎖,一個是行級別的X鎖。而造成鎖等待的鎖是行鎖。所以這時候就有一個疑問了,行鎖鎖定的是b=1這一行,為啥session2中我們要申請b=4這一行的行鎖會發生鎖等待呢?其實原因也顯而易見了:欄位b無索引,申請b=4這一行的行鎖會掃描全表,也就是說對錶資料的每一行都會申請X鎖。而在實驗三中,可以走主鍵索引直接定位到b=4這一行,所以就不會造成鎖等待了。
下面再看一個實驗四:
Session1 |
Session2 |
mysql> begin; Query OK, 0 rows affected (0.00 sec)
mysql> select * from siri where b=1 for update; +----+---+---+---+ | id | a | b | c | +----+---+---+---+ | 1 | 1 | 1 | 1 | +----+---+---+---+ 1 row in set (0.00 sec) |
|
mysql> update siri set c=4 where b=4; Query OK, 0 rows affected (0.00 sec) Rows matched: 1 Changed: 0 Warnings: 0 |
二、解讀原始碼
為了驗證我的猜想,深究背後的原理,還是得在實際場景下除錯一下原始碼,閱讀原始碼才能更好的瞭解為什麼是這樣的。
在mysql原始碼中,負責給行加鎖的函式是sel_set_rec_lock,我們可以在該函式處打下斷點,看看select for update和update這兩種sql在申請鎖的流程上面有什麼區別。
/** Sets a lock on a record. mostly due to we cannot reposition a record in R-Tree (with the nature of splitting) @param[in] pcur cursor @param[in] rec record @param[in] index index @param[in] offsets rec_get_offsets(rec, index) @param[in] sel_mode select mode: SELECT_ORDINARY, SELECT_SKIP_LOKCED, or SELECT_NO_WAIT @param[in] mode lock mode @param[in] type LOCK_ORDINARY, LOCK_GAP, or LOC_REC_NOT_GAP @param[in] thr query thread @param[in] mtr mtr @return DB_SUCCESS, DB_SUCCESS_LOCKED_REC, or error code */ static inline dberr_t sel_set_rec_lock(btr_pcur_t *pcur, const rec_t *rec, dict_index_t *index, const ulint *offsets, select_mode sel_mode, ulint mode, ulint type, que_thr_t *thr, mtr_t *mtr) { trx_t *trx; dberr_t err = DB_SUCCESS; const buf_block_t *block; block = pcur->get_block(); trx = thr_get_trx(thr); ut_ad(trx_can_be_handled_by_current_thread(trx)); if (UT_LIST_GET_LEN(trx->lock.trx_locks) > 10000) { if (buf_LRU_buf_pool_running_out()) { return (DB_LOCK_TABLE_FULL); } } if (index->is_clustered()) { err = lock_clust_rec_read_check_and_lock( lock_duration_t::REGULAR, block, rec, index, offsets, sel_mode, static_cast<lock_mode>(mode), type, thr); } else { if (dict_index_is_spatial(index)) { if (type == LOCK_GAP || type == LOCK_ORDINARY) { ib::error(ER_IB_MSG_1026) << "Incorrectly request GAP lock " "on RTree"; ut_d(ut_error); ut_o(return (DB_SUCCESS)); } err = sel_set_rtr_rec_lock(pcur, rec, index, offsets, sel_mode, mode, type, thr, mtr); } else { err = lock_sec_rec_read_check_and_lock( lock_duration_t::REGULAR, block, rec, index, offsets, sel_mode, static_cast<lock_mode>(mode), type, thr); } } return (err); }
在mysql的debug模式中執行select * from testdb.siri where b=4 for update,gdb中命中sel_set_rec_lock函式斷點,函式堆疊資訊如下:
#0 sel_set_rec_lock (pcur=0x7f52040e3ef8, rec=0x7f521e05c07d "\200", index=0x7f52040e8028, offsets=0x7f52146f3bc0, sel_mode=SELECT_ORDINARY, mode=3, type=1024, thr=0x7f52040e4700, mtr=0x7f52146f3ef0) at /root/gdb_mysql/mysql-8.0.32/storage/innobase/row/row0sel.cc:1142
執行update testdb.siri set c=2 where b=4,函式堆疊資訊如下:
#0 sel_set_rec_lock (pcur=0x7f52040e3ef8, rec=0x7f521e05c07d "\200", index=0x7f52040e8028, offsets=0x7f52146f3630, sel_mode=SELECT_SKIP_LOCKED, mode=3, type=1024, thr=0x7f52040e4700, mtr=0x7f52146f3960) at /root/gdb_mysql/mysql-8.0.32/storage/innobase/row/row0sel.cc:1142
發現了兩者的區別嗎?區別在於sel_mode這個引數是不同的:對於select for update,sel_mode是SELECT_ORDINARY;對於update,sel_mode是SELECT_SKIP_LOCKED。sel_mode引數的定義如下:
enum select_mode { SELECT_ORDINARY, /* default behaviour */ SELECT_SKIP_LOCKED, /* skip the row if row is locked */ SELECT_NOWAIT /* return immediately if row is locked */ };
在row_search_mvcc函式中,透過以下程式碼來判定這條sql是否為半一致性讀(semi-consistent read)。
/* in case of semi-consistent read, we use SELECT_SKIP_LOCKED, so we don't waste time on creating a WAITING lock, as we won't wait on it anyway */ const bool use_semi_consistent = prebuilt->row_read_type == ROW_READ_TRY_SEMI_CONSISTENT && !unique_search && index == clust_index && !trx_is_high_priority(trx); err = sel_set_rec_lock( pcur, rec, index, offsets, use_semi_consistent ? SELECT_SKIP_LOCKED : prebuilt->select_mode, prebuilt->select_lock_type, lock_type, thr, &mtr);
update語句是半一致性讀,因此use_semi_consistent為true,select_mode為SELECT_SKIP_LOCKED,這表示會話不會浪費時間在建立鎖等待上,可以跳過持有鎖的行。而對於select for update語句,use_semi_consistent為false,select_mode為SELECT_ORDINARY,表示會話會建立一個鎖等待,直到鎖等待超時。
因此,對於實驗四中的現象update不會被堵塞的原因已經比較清楚了,update在mysql內部被定義成了半一致性讀(SELECT_SKIP_LOCKED),因此實驗四的session2中update進行全表掃描讀取主鍵時,讀取到b=1這一列時,會跳過session1所持有的位於b=1行上的行鎖,所以也就不會發生鎖等待的現象。相反,實驗二中select for update在mysql內部定義為普通讀(SELECT_ORDINARY),讀取到b=1這一列時,會被session1所持有的位於b=1行上的行鎖堵塞,發生鎖等待的現象。