国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

mysql教學 鎖

2024-07-24 12:31:46
字體:
來源:轉載
供稿:網友
       mysql學習 鎖:

1.   鎖
       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   對容易產生死鎖的業務,可以嘗試使用升級鎖粒度,通過表鎖定減少鎖產生的概率。
 
| InnoDB |      |
 
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)
  
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
  
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
  
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]
 
1.4.   鎖問題的監控
通過show full processlist和show engine Innodb status來判斷事務中鎖問題情況,另外還有三張表可查:
 
information_schema.INNODB_TRX
 
information_schema.INNODB_LOCKS
 
information_schema.INNODB_LOCK_WAITS
 
[(none)]>show full processlist;
 
+----+------+-----------+------+---------+------+----------+-----------------------+
 
| Id | User | Host      | db   | Command | Time | State    | Info                  |
 
+----+------+-----------+------+---------+------+----------+-----------------------+
 
| 57 | root | localhost | NULL | Query   |    0 | starting | show full processlist |
 
+----+------+-----------+------+---------+------+----------+-----------------------+

(編輯:武林網)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 琼结县| 阿拉善盟| 汕尾市| 临沭县| 龙川县| 八宿县| 葫芦岛市| 宣化县| 七台河市| 苍南县| 阳高县| 塘沽区| 贺州市| 瑞昌市| 德保县| 宜兰市| 忻州市| 东海县| 七台河市| 武乡县| 游戏| 黄陵县| 中牟县| 文安县| 从化市| 巴里| 桦南县| 遵义县| 荃湾区| 齐河县| 东阿县| 大丰市| 灌南县| 清原| 宁蒗| 山丹县| 临猗县| 扬州市| 云霄县| 望谟县| 博湖县|