InnoDB支持行鎖,有時升級為表鎖。
創(chuàng)新互聯(lián)自2013年起,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元珙縣做網(wǎng)站,已為上家服務(wù),為珙縣各地企業(yè)和個人服務(wù),聯(lián)系電話:18982081108
MyISAM只支持表鎖。
表鎖:開小小,加鎖快,不會出現(xiàn)死鎖;鎖粒度大,鎖沖突概率高,并發(fā)度低。
行鎖:開銷大,加鎖慢,會出現(xiàn)死鎖,鎖粒度小,鎖沖突概率低,并發(fā)高。
主要分為:讀鎖(共享鎖),寫鎖(排他鎖),意向鎖,和MDL鎖。
讀鎖,S鎖,一個事物在讀取一個數(shù)據(jù)行時,其他事務(wù)也可以讀,但不能對該數(shù)據(jù)行增刪改的操作。兩種select方式的應(yīng)用。
l 自動提交模式下的select查詢語句,不需加任何鎖返回結(jié)果,是一致性非鎖定讀。
l 通過select....lock in share mode在被讀取的行記錄或行記錄的范圍上加一個讀鎖,讓其他事務(wù)可讀不可申請加寫鎖。
寫鎖簡稱X鎖,一個事務(wù)獲取一行的寫鎖,其他事務(wù)就不能獲取該行其它鎖,優(yōu)先級最高。
select for update,會對讀取的行記錄上加一個寫鎖,其他任何事務(wù)就不能加任何鎖。
MySQL5.5引入meta data lock,簡稱MDL鎖,用于保護(hù)表中元數(shù)據(jù)的信息。即一個事務(wù)查詢表將自動給表加MDL鎖,其他事務(wù)不能做任何DDL操作。
InnoDB引擎中,意向鎖是表級鎖,作用和MDL類似,防止事務(wù)進(jìn)行過程中,執(zhí)行DDL語句的操作而導(dǎo)致數(shù)據(jù)不一致。有兩種意向鎖類型:
l 意向共享鎖(IS):數(shù)據(jù)行加共享鎖前必須先取得該表的IS鎖。
l 意向排他鎖(IX):數(shù)據(jù)行加排他鎖前必須先取得該表的IX鎖。
InnoDB默認(rèn)事務(wù)隔離級別為RR,且參數(shù)innodb_locks_unsafe_for_binlog=0的模式下,行鎖有三種。
l 單個行記錄的鎖(record lock),主鍵和唯一索引都是。
l 間隙鎖(GAP lock)
l 記錄鎖和間隙鎖的組合叫next-key lock。普通索引默認(rèn)。
InnoDB上的行鎖就是加在索引上。有索引,更新只鎖指定行,無索引,更新鎖所有行。
RR隔離級別,為了避免幻讀,引入Gap lock,只鎖定行記錄數(shù)據(jù)的范圍,不包含記錄本身,即不允許在此范圍內(nèi)插入任何數(shù)據(jù)。
RC隔離級別允許出現(xiàn)幻讀現(xiàn)象。
Next-key lock是記錄鎖(Record Lock)與間隔鎖(Gap Lock)的組合,當(dāng)InnoDB掃描索引記錄時,會先對選中的索引記錄加上記錄鎖(Record lock),再對索引記錄兩邊的間隙加上間隙鎖(Gap lock)。
鎖等待,是一個事務(wù)產(chǎn)生鎖,其他事務(wù)等待上個事務(wù)釋放它的鎖。鎖等待超時閾值innodb_lok_wait_timeout控制,單位秒。
死鎖,多個事務(wù)爭奪資源相互等待的現(xiàn)象,即鎖資源請求產(chǎn)生了回路,就是死循環(huán)。
避免死鎖的方法:
l 如不同的程序會并發(fā)存取多個表,或涉及多汗記錄,盡量約定以相同的順序訪問表。
l 業(yè)務(wù)中盡量采用小事務(wù),避免大事務(wù),及時提交或回滾。
l 在同一個事務(wù)中,盡可能做到一次鎖定所需的所有資源。
l 對容易產(chǎn)生死鎖的業(yè)務(wù),可以嘗試使用升級鎖粒度,通過表鎖定減少鎖產(chǎn)生的概率。
通過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
============================
通過show full processlist和show engine Innodb status來判斷事務(wù)中鎖問題情況,另外還有三張表可查:
information_schema.INNODB_TRX
information_schema.INNODB_LOCKS
information_schema.INNODB_LOCK_WAITS
innodb_trx表部分字段
trx_id唯一的事務(wù)id號;
trx_state:事務(wù)狀態(tài);
trx_wait_started:事務(wù)開始等待時間。
trx_mysql_thread_id:線程ID,與show full processlist相互對應(yīng)。
trx_query:事務(wù)運(yùn)行的SQL;
trx_operation_state:事務(wù)運(yùn)行的狀態(tài)。
[(none)]>show full processlist;
+----+------+-----------+------+---------+------+----------+-----------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+----------+-----------------------+
| 57 | root | localhost | NULL | Query | 0 | starting | show full processlist |
+----+------+-----------+------+---------+------+----------+-----------------------+