mysql學習13:第八章:鎖
InnoDB支援行鎖,有時升級為表鎖。
MyISAM只支援表鎖。
表鎖:開小小,加鎖快,不會出現死鎖;鎖粒度大,鎖衝突機率高,併發度低。
行鎖:開銷大,加鎖慢,會出現死鎖,鎖粒度小,鎖衝突機率低,併發高。
1.1. InnoDB鎖型別
主要分為:讀鎖(共享鎖),寫鎖(排他鎖),意向鎖,和MDL鎖。
1.1.1. 讀鎖
讀鎖,S鎖,一個事物在讀取一個資料行時,其他事務也可以讀,但不能對該資料行增刪改的操作。兩種select方式的應用。
l 自動提交模式下的select查詢語句,不需加任何鎖返回結果,是一致性非鎖定讀。
l 透過select....lock in share mode在被讀取的行記錄或行記錄的範圍上加一個讀鎖,讓其他事務可讀不可申請加寫鎖。
1.1.2. 寫鎖
寫鎖簡稱X鎖,一個事務獲取一行的寫鎖,其他事務就不能獲取該行其它鎖,優先順序最高。
select for update,會對讀取的行記錄上加一個寫鎖,其他任何事務就不能加任何鎖。
1.1.3. MDL鎖
mysql5.5引入meta data lock,簡稱MDL鎖,用於保護表中後設資料的資訊。即一個事務查詢表將自動給表加MDL鎖,其他事務不能做任何DDL操作。
1.1.4. 意向鎖
InnoDB引擎中,意向鎖是表級鎖,作用和MDL類似,防止事務進行過程中,執行DDL語句的操作而導致資料不一致。有兩種意向鎖型別:
l 意向共享鎖(IS):資料行加共享鎖前必須先取得該表的IS鎖。
l 意向排他鎖(IX):資料行加排他鎖前必須先取得該表的IX鎖。
1.2. InnoDB行鎖種類
InnoDB預設事務隔離級別為RR,且引數innodb_locks_unsafe_for_binlog=0的模式下,行鎖有三種。
l 單個行記錄的鎖(record lock),主鍵和唯一索引都是。
l 間隙鎖(GAP lock)
l 記錄鎖和間隙鎖的組合叫next-key lock。普通索引預設。
1.2.1. 單個行記錄的鎖
InnoDB上的行鎖就是加在索引上。有索引,更新只鎖指定行,無索引,更新鎖所有行。
1.2.2. 間隙鎖(GAP lock)
RR隔離級別,為了避免幻讀,引入Gap lock,只鎖定行記錄資料的範圍,不包含記錄本身,即不允許在此範圍內插入任何資料。
RC隔離級別允許出現幻讀現象。
1.2.3. Next-Key Locks
Next-key lock是記錄鎖(Record Lock)與間隔鎖(Gap Lock)的組合,當InnoDB掃描索引記錄時,會先對選中的索引記錄加上記錄鎖(Record lock),再對索引記錄兩邊的間隙加上間隙鎖(Gap lock)。
1.3. 鎖等待和死鎖
鎖等待,是一個事務產生鎖,其他事務等待上個事務釋放它的鎖。鎖等待超時閾值innodb_lok_wait_timeout控制,單位秒。
死鎖,多個事務爭奪資源相互等待的現象,即鎖資源請求產生了迴路,就是死迴圈。
避免死鎖的方法:
l 如不同的程式會併發存取多個表,或涉及多汗記錄,儘量約定以相同的順序訪問表。
l 業務中儘量採用小事務,避免大事務,及時提交或回滾。
l 在同一個事務中,儘可能做到一次鎖定所需的所有資源。
l 對容易產生死鎖的業務,可以嘗試使用升級鎖粒度,透過表鎖定減少鎖產生的機率。
透過show engine innodb sttus檢視死鎖展示資訊 。
[(none)]>show engine innodb status;
| Type | Name | Status
| InnoDB | |
=====================================
2018-11-07 22:49:40 0x7f1320202700 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 11 seconds
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: 98 srv_active, 0 srv_shutdown, 49465 srv_idle
srv_master_thread log flush and writes: 49563
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 20
OS WAIT ARRAY INFO: signal count 20
RW-shared spins 0, rounds 34, OS waits 16
RW-excl spins 0, rounds 200, OS waits 2
RW-sx spins 0, rounds 0, OS waits 0
Spin rounds per wait: 34.00 RW-shared, 200.00 RW-excl, 0.00 RW-sx
------------
TRANSACTIONS
------------
Trx id counter 65440
Purge done for trx's n:o < 65438 undo n:o < 0 state: running but idle
History list length 12
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 421197684710112, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 421197684709200, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
--------
FILE I/O
--------
I/O thread 0 state: waiting for completed aio requests (insert buffer thread)
I/O thread 1 state: waiting for completed aio requests (log thread)
I/O thread 2 state: waiting for completed aio requests (read thread)
I/O thread 3 state: waiting for completed aio requests (read thread)
I/O thread 4 state: waiting for completed aio requests (read thread)
I/O thread 5 state: waiting for completed aio requests (read thread)
I/O thread 6 state: waiting for completed aio requests (read thread)
I/O thread 7 state: waiting for completed aio requests (read thread)
I/O thread 8 state: waiting for completed aio requests (read thread)
I/O thread 9 state: waiting for completed aio requests (read thread)
I/O thread 10 state: waiting for completed aio requests (write thread)
I/O thread 11 state: waiting for completed aio requests (write thread)
I/O thread 12 state: waiting for completed aio requests (write thread)
I/O thread 13 state: waiting for completed aio requests (write thread)
I/O thread 14 state: waiting for completed aio requests (write thread)
I/O thread 15 state: waiting for completed aio requests (write thread)
I/O thread 16 state: waiting for completed aio requests (write thread)
I/O thread 17 state: waiting for completed aio requests (write thread)
Pending normal aio reads: [0, 0, 0, 0, 0, 0, 0, 0] , aio writes: [0, 0, 0, 0, 0, 0, 0, 0] ,
ibuf aio reads:, log i/o's:, sync i/o's:
Pending flushes (fsync) log: 0; buffer pool: 0
271 OS file reads, 61118 OS file writes, 60451 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 0.00 writes/s, 0.00 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 0, seg size 2, 0 merges
merged operations:
insert 0, delete mark 0, delete 0
discarded operations:
insert 0, delete mark 0, delete 0
Hash table size 553193, node heap has 0 buffer(s)
Hash table size 553193, node heap has 0 buffer(s)
Hash table size 553193, node heap has 0 buffer(s)
Hash table size 553193, node heap has 0 buffer(s)
Hash table size 553193, node heap has 0 buffer(s)
Hash table size 553193, node heap has 1 buffer(s)
Hash table size 553193, node heap has 0 buffer(s)
Hash table size 553193, node heap has 0 buffer(s)
0.00 hash searches/s, 0.00 non-hash searches/s
---
LOG
---
Log sequence number 20357616
Log flushed up to 20357616
Pages flushed up to 20357616
Last checkpoint at 20357607
0 pending log flushes, 0 pending chkp writes
60048 log i/o's done, 0.00 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total large memory allocated 2198863872
Dictionary memory allocated 156387
Buffer pool size 131056
Free buffers 130465
Database pages 590
Old database pages 0
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 238, created 352, written 805
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 590, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
----------------------
INDIVIDUAL BUFFER POOL INFO
----------------------
---BUFFER POOL 0
Buffer pool size 16382
Free buffers 16279
Database pages 102
Old database pages 0
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 38, created 64, written 95
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 102, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 1
Buffer pool size 16382
Free buffers 16312
Database pages 70
Old database pages 0
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 6, created 64, written 64
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 70, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 2
Buffer pool size 16382
Free buffers 16319
Database pages 63
Old database pages 0
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 8, created 55, written 56
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 63, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 3
Buffer pool size 16382
Free buffers 16303
Database pages 79
Old database pages 0
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 73, created 6, written 59
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 79, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 4
Buffer pool size 16382
Free buffers 16265
Database pages 117
Old database pages 0
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 76, created 41, written 120
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 117, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 5
Buffer pool size 16382
Free buffers 16307
Database pages 75
Old database pages 0
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 11, created 64, written 91
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 75, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 6
Buffer pool size 16382
Free buffers 16363
Database pages 19
Old database pages 0
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 13, created 6, written 12
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 19, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
---BUFFER POOL 7
Buffer pool size 16382
Free buffers 16317
Database pages 65
Old database pages 0
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 13, created 52, written 308
0.00 reads/s, 0.00 creates/s, 0.00 writes/s
No buffer pool page gets since the last printout
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 65, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
0 read views open inside InnoDB
Process ID=21556, Main thread ID=139720374998784, state: sleeping
Number of rows inserted 60824, updated 0, deleted 0, read 121836
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================
1.4. 鎖問題的監控
透過show full processlist和show engine Innodb status來判斷事務中鎖問題情況,另外還有三張表可查:
information_schema.INNODB_TRX
information_schema.INNODB_LOCKS
information_schema.INNODB_LOCK_WAITS
innodb_trx表部分欄位
trx_id唯一的事務id號;
trx_state:事務狀態;
trx_wait_started:事務開始等待時間。
trx_mysql_thread_id:執行緒ID,與show full processlist相互對應。
trx_query:事務執行的SQL;
trx_operation_state:事務執行的狀態。
[(none)]>show full processlist;
+----+------+-----------+------+---------+------+----------+-----------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+----------+-----------------------+
| 57 | root | localhost | NULL | Query | 0 | starting | show full processlist |
+----+------+-----------+------+---------+------+----------+-----------------------+
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29519108/viewspace-2219151/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL學習之行鎖MySql
- MySQL學習筆記:鎖MySql筆記
- MySQL學習之全域性鎖和表鎖MySql
- ThinkPhp學習13PHP
- Mysql學習筆記-臨鍵鎖實驗MySql筆記
- MySQL入門學習之——mysql與oracle死鎖對比MySqlOracle
- 6/13學習程序
- 3/13學習程序
- MySQL 事務、日誌、鎖、索引學習總結,MySql索引
- InnoDB鎖學習
- Arduino學習筆記13UI筆記
- JavaScript學習筆記13JavaScript筆記
- Laravel 第八章學習——郵件傳送Laravel
- MySQL複習筆記(05):MySQL表級鎖和行級鎖MySql筆記
- 13、資料,學習和建模
- JavaScript學習13:事件繫結JavaScript事件
- 重新學習MySQL資料庫6:淺談MySQL的中事務與鎖MySql資料庫
- Laravel 第八章學習——中介軟體以及策略Laravel
- 強化學習-學習筆記13 | 多智慧體強化學習強化學習筆記智慧體
- 第八章習題
- 學習MySQLMySql
- MySQL 學習MySql
- Mysql學習MySql
- 3月13日學習進度
- 學習筆記13:微調模型筆記模型
- MYSQL學習(二) --MYSQL框架MySql框架
- 學習etcd分散式鎖分散式
- mysql鎖之死鎖MySql
- Mudo C++網路庫第八章學習筆記C++筆記
- Flutter學習筆記(13)--表單元件Flutter筆記元件
- substrate學習筆記13:連線parachain筆記AI
- Python學習之路13-記分Python
- CTF學習(13)MISC(另一個世界)
- MySQL學習 - 索引MySql索引
- MySQL深度學習MySql深度學習
- Mysql學習教程MySql
- mysql學習(一)MySql
- mysql學習(二)MySql