溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Mysql一分鐘定位 Next-Key Lock,你需要幾分

發(fā)布時間:2020-07-27 02:30:42 來源:網(wǎng)絡(luò) 閱讀:442 作者:王清培 欄目:MySQL數(shù)據(jù)庫

連接與線程

查看連接信息 show processlist

+----+------+------------------+------+---------+------+----------+------------------+
| Id | User | Host             | db   | Command | Time | State    | Info             |
+----+------+------------------+------+---------+------+----------+------------------+
| 3  | root | 172.17.0.1:60542 | test | Query   | 0    | starting | show processlist |
| 5  | root | 172.17.0.1:60546 | test | Sleep   | 4168 |          | <null>           |
| 8  | root | 172.17.0.1:60552 | test | Sleep   | 4170 |          | <null>           |
+----+------+------------------+------+---------+------+----------+------------------+

mysql 非企業(yè)版本只支持一個線程一個鏈接

查看線程模型 show variables like 'thread_handling'

+-----------------------------------------+---------------------------+
| Variable_name                           | Value                     |
+-----------------------------------------+---------------------------+
| thread_handling                         | one-thread-per-connection |
+-----------------------------------------+---------------------------+

【 事務(wù)提交策略】
有兩個隱藏事務(wù)提交時間點需要注意,第一個是 autocommit=1 Mysql session 級別的自動提交變量,所有 ORM 框架中的事務(wù)提交控制都會受到這個字段影響,默認(rèn)情況下當(dāng)前語句會自動提交,但是如果是顯示 begin transaction 開啟事務(wù)需要自行手動提交。有些時候 ORM 框架會根據(jù)一些設(shè)置或者策略,將 autocommit 設(shè)置為0。

第二個就是,DDL操作前都會隱式提交當(dāng)前事務(wù),有些腳本將DML和DDL混合在一起使用,這樣會有一致性問題。DDL會自動提交當(dāng)前事務(wù)。因為DDL在5.7之前都是不支持事務(wù)原則操作的。(Mysql8.0已經(jīng)支持DDL事務(wù)性)

Next-Key Lock 排查

Next-Key Lock 只發(fā)生在 RR(REPEATABLE-READ) 隔離級別下。

Mysql 有很多類型對種鎖,表鎖record lock、gap lock意向共享/排他鎖、插入意向鎖元數(shù)據(jù)鎖、Auto_Incr自增鎖,排除掉 元數(shù)據(jù)鎖、Auto_Incr自增鎖 之后,剩下的鎖組合使用最多的就是在RR隔離級別下。

RR隔離級別是默認(rèn)事務(wù)隔離級別,也是Mysql的強項之一,在RR隔離級別下事務(wù)有最大的吞吐量,而且不會出現(xiàn)幻讀問題。Next-Key Lock 就是為了解決這個問題,簡單講 record lock+gap lock 就是 Next-Key Lock。

_幻讀_的根本問題就是出現(xiàn)在記錄的邊界值上,比如我們統(tǒng)計年齡大于30歲的人數(shù):select count(1) peoples where age&gt;30 這個語句有可能每次查詢得到的結(jié)果集都是不一樣的,因為只要符合 age>30 的記錄進(jìn)到我們的 peoples 表中就會被查詢條件命中。

所以要想解決幻讀不僅不允許記錄的空隙被插入記錄外,還要防止兩遍記錄被修改,因為如果前后兩條記錄被修改了那區(qū)間就會變大,就會有幻讀出現(xiàn)。

我們看個例子。

 CREATE TABLE `peoples` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_peoples_age` (`age`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4
+----+-----+
| id | age |
+----+-----+
| 1  | 20  |
| 2  | 30  |
| 3  | 35  |
| 4  | 40  |
+----+-----+

為了方便調(diào)試,將 innodb 獲取鎖的超時時間調(diào)大點

show variables like '%innodb_lock_wait%'
set innodb_lock_wait_timeout=600

開啟兩個會話。

session A id=8:
begin
select count(1) from peoples where age>30 for update;
session B id=5:
begin
insert into peoples(age) values(31)

show processlist 找到連接的id。

***************************[ 1. row ]***************************
Id      | 3
User    | root
Host    | 172.17.0.1:60542
db      | test
Command | Query
Time    | 0
State   | starting
Info    | show processlist
***************************[ 2. row ]***************************
Id      | 5
User    | root
Host    | 172.17.0.1:60546
db      | test
Command | Query
Time    | 394
State   | update
Info    | insert into peoples(age) values(31)
***************************[ 3. row ]***************************
Id      | 8
User    | root
Host    | 172.17.0.1:60552
db      | test
Command | Sleep
Time    | 396
State   |
Info    | <null>
  • 事務(wù)

select * from information_schema.innodb_trx \G 查看事務(wù)執(zhí)行情況。

***************************[ 1. row ]***************************
trx_id                     | 457240
trx_state                  | LOCK WAIT
trx_started                | 2020-01-27 06:08:12
trx_requested_lock_id      | 457240:131:4:4
trx_wait_started           | 2020-01-27 06:09:25
trx_weight                 | 6
trx_mysql_thread_id        | 5
trx_query                  | insert into peoples(age) values(31)
trx_operation_state        | inserting
trx_tables_in_use          | 1
trx_tables_locked          | 1
trx_lock_structs           | 5
trx_lock_memory_bytes      | 1136
trx_rows_locked            | 4
trx_rows_modified          | 1
trx_concurrency_tickets    | 0
trx_isolation_level        | REPEATABLE READ
trx_unique_checks          | 1
trx_foreign_key_checks     | 1
trx_last_foreign_key_error | <null>
trx_adaptive_hash_latched  | 0
trx_adaptive_hash_timeout  | 0
trx_is_read_only           | 0
trx_autocommit_non_locking | 0
***************************[ 2. row ]***************************
trx_id                     | 457239
trx_state                  | RUNNING
trx_started                | 2020-01-27 06:07:59
trx_requested_lock_id      | <null>
trx_wait_started           | <null>
trx_weight                 | 3
trx_mysql_thread_id        | 8
trx_query                  | <null>
trx_operation_state        | <null>
trx_tables_in_use          | 0
trx_tables_locked          | 1
trx_lock_structs           | 3
trx_lock_memory_bytes      | 1136
trx_rows_locked            | 5
trx_rows_modified          | 0
trx_concurrency_tickets    | 0
trx_isolation_level        | REPEATABLE READ
trx_unique_checks          | 1
trx_foreign_key_checks     | 1
trx_last_foreign_key_error | <null>
trx_adaptive_hash_latched  | 0
trx_adaptive_hash_timeout  | 0
trx_is_read_only           | 0
trx_autocommit_non_locking | 0

457240 事務(wù)狀態(tài)是 LOCK WAIT 在等待鎖,457239事務(wù)狀態(tài)是 RUNNING執(zhí)行中,正在等待事務(wù)提交。

select * from information_schema.innodb_locks \G 查看鎖的占用情況。

***************************[ 1. row ]***************************
lock_id     | 457240:131:4:4
lock_trx_id | 457240
lock_mode   | X,GAP
lock_type   | RECORD
lock_table  | `test`.`peoples`
lock_index  | idx_peoples_age
lock_space  | 131
lock_page   | 4
lock_rec    | 4
lock_data   | 35, 7
***************************[ 2. row ]***************************
lock_id     | 457239:131:4:4
lock_trx_id | 457239
lock_mode   | X
lock_type   | RECORD
lock_table  | `test`.`peoples`
lock_index  | idx_peoples_age
lock_space  | 131
lock_page   | 4
lock_rec    | 4
lock_data   | 35, 7

innodb_locks 表包含了已經(jīng)獲取到的鎖信息和請求鎖的信息。lock_index字段表示鎖走的索引,record鎖都是基于索引完成。

根據(jù)上面事務(wù)457240狀態(tài)是獲取鎖,lock_data | 35, 7,表示請求的數(shù)據(jù)。而事務(wù)457239占用了當(dāng)前X鎖。

  • 鎖等待

select * from information_schema.innodb_lock_waits 查看鎖等待信息。

***************************[ 1. row ]***************************
requesting_trx_id | 457240
requested_lock_id | 457240:131:4:4
blocking_trx_id   | 457239
blocking_lock_id  | 457239:131:4:4

457240 事務(wù)需要獲取131:4:4鎖,457239 事務(wù)占用了131:4:4鎖。

  • innodb 監(jiān)視器
    show engine innodb status
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 422032240994144, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 457240, ACTIVE 394 sec inserting
mysql tables in use 1, locked 1
LOCK WAIT 5 lock struct(s), heap size 1136, 7 row lock(s), undo log entries 1
MySQL thread id 5, OS thread handle 140556966967040, query id 105 172.17.0.1 root update
insert into peoples(age) values(31)
------- TRX HAS BEEN WAITING 165 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 131 page no 4 n bits 72 index idx_peoples_age of table `test`.`peoples` trx id 457240 lock_mode X locks gap before rec insert intention waiting
Record lock, heap no 4 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
 0: len 4; hex 80000023; asc    #;;
 1: len 4; hex 00000007; asc     ;;

------------------
---TRANSACTION 457239, ACTIVE 407 sec
3 lock struct(s), heap size 1136, 5 row lock(s)
MySQL thread id 8, OS thread handle 140556966696704, query id 104 172.17.0.1 root

MySQL thread id 5 正在準(zhǔn)備上插入意向鎖,插入意向鎖本質(zhì)上是加間隙鎖,是為了保證最大并發(fā)插入,不相關(guān)的行插入不受到互斥。thread id 5 需要保證在插入前加上間隙鎖,主要是防止并發(fā)插入帶來的一致性問題。

session 5 和 session 8 都沒有操作到 id=3,age=35的記錄,但是卻被X+Gap Lock 鎖住,只有這樣才能解決幻讀問題。

作者:王清培(趣頭條 Tech Leader)

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI