自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MySQL死鎖問題實例分析及解決方法

數(shù)據(jù)庫 MySQL
本文我們主要介紹了MySQL死鎖的相關(guān)知識,包括:MySQL常用存儲引擎的鎖機制、各種鎖特點、各種鎖的適用場景以及死鎖問題的實例分析及解決方法,希望能夠?qū)δ兴斋@!

MySQL死鎖問題的相關(guān)知識是本文我們主要要介紹的內(nèi)容,接下來我們就來一一介紹這部分內(nèi)容,希望能夠?qū)δ兴鶐椭?/p>

1、MySQL常用存儲引擎的鎖機制

MyISAM和MEMORY采用表級鎖(table-level locking)

BDB采用頁面鎖(page-level locking)或表級鎖,默認為頁面鎖

InnoDB支持行級鎖(row-level locking)和表級鎖,默認為行級鎖

2、各種鎖特點

表級鎖:開銷小,加鎖快;不會出現(xiàn)死鎖;鎖定粒度大,發(fā)生鎖沖突的概率***,并發(fā)度***

行級鎖:開銷大,加鎖慢;會出現(xiàn)死鎖;鎖定粒度最小,發(fā)生鎖沖突的概率***,并發(fā)度也***

頁面鎖:開銷和加鎖時間界于表鎖和行鎖之間;會出現(xiàn)死鎖;鎖定粒度界于表鎖和行鎖之間,并發(fā)度一般

3、各種鎖的適用場景

表級鎖更適合于以查詢?yōu)橹?只有少量按索引條件更新數(shù)據(jù)的應用,如Web應用

行級鎖則更適合于有大量按索引條件并發(fā)更新數(shù)據(jù),同時又有并發(fā)查詢的應用,如一些在線事務(wù)處理系統(tǒng)

4、死鎖

是指兩個或兩個以上的進程在執(zhí)行過程中,因爭奪資源而造成的一種互相等待的現(xiàn)象,若無外力作用,它們都將無法推進下去。

表級鎖不會產(chǎn)生死鎖.所以解決死鎖主要還是針對于最常用的InnoDB.

5、死鎖舉例分析

在MySQL中,行級鎖并不是直接鎖記錄,而是鎖索引。索引分為主鍵索引和非主鍵索引兩種,如果一條sql語句操作了主鍵索引,MySQL就會鎖定這條主鍵索引;如果一條語句操作了非主鍵索引,MySQL會先鎖定該非主鍵索引,再鎖定相關(guān)的主鍵索引。

在UPDATE、DELETE操作時,MySQL不僅鎖定WHERE條件掃描過的所有索引記錄,而且會鎖定相鄰的鍵值,即所謂的next-key locking。

例如,一個表db.tab_test,結(jié)構(gòu)如下:

id:主鍵;

state:狀態(tài);

time:時間;

索引:idx_1 (state, time)

出現(xiàn)死鎖日志如下:

 

  1. ***(1) TRANSACTION:  
  2. TRANSACTION 0 677833455, ACTIVE 0 sec, process no 11393, OS thread id 278546 starting index read  
  3. mysql tables in use 1, locked 1  
  4. LOCK WAIT 3 lock struct(s), heap size 320  
  5. MySQL thread id 83, query id 162348740 dcnet03 dcnet Searching rows for update  
  6. update tab_test set state=1064,time=now() where state=1061 and time < date_sub(now(), INTERVAL 30 minute) (任務(wù)1的sql語句)  
  7. ***(1) WAITING FOR THIS LOCK TO BE GRANTED: (任務(wù)1等待的索引記錄)  
  8. RECORD LOCKS space id 0 page no 849384 n bits 208 index `PRIMARY` of table `db/tab_test` trx id 0 677833455 _mode X locks rec but not gap waiting  
  9. Record lock, heap no 92 PHYSICAL RECORD: n_fields 11; compact format; info bits 0  
  10. 0: len 8; hex 800000000097629c; asc b ;; 1: len 6; hex 00002866eaee; asc (f ;; 2: len 7; hex 00000d40040110; asc @ ;; 3: len 8; hex 80000000000050b2; asc P ;; 4: len 8; hex 800000000000502a; asc P*;; 5: len 8; hex 8000000000005426; asc T&;; 6: len 8; hex 800012412c66d29c; asc A,f ;; 7: len 23; hex 75706c6f6164666972652e636f6d2f6 8616e642e706870; asc xxx.com/;; 8: len 8; hex 800000000000042b; asc +;; 9: len 4; hex 474bfa2b; asc GK +;; 10: len 8; hex 8000000000004e24; asc N$;;  
  11. *** (2) TRANSACTION:  
  12. TRANSACTION 0 677833454, ACTIVE 0 sec, process no 11397, OS thread id 344086 updating or deleting, thread declared inside InnoDB 499  
  13. mysql tables in use 1, locked 1  
  14. 3 lock struct(s), heap size 320, undo log entries 1  
  15. MySQL thread id 84, query id 162348739 dcnet03 dcnet Updating update tab_test set state=1067,time=now () where id in (9921180) (任務(wù)2的sql語句)  
  16. *** (2) HOLDS THE LOCK(S): (任務(wù)2已獲得的鎖)  
  17. RECORD LOCKS space id 0 page no 849384 n bits 208 index `PRIMARY` of table `db/tab_test` trx id 0 677833454 lock_mode X locks rec but not gap  
  18. Record lock, heap no 92 PHYSICAL RECORD: n_fields 11; compact format; info bits 0  
  19. 0: len 8; hex 800000000097629c; asc b ;; 1: len 6; hex 00002866eaee; asc (f ;; 2: len 7; hex 00000d40040110; asc @ ;; 3: len 8; hex 80000000000050b2; asc P ;; 4: len 8; hex 800000000000502a; asc P*;; 5: len 8; hex 8000000000005426; asc T&;; 6: len 8; hex 800012412c66d29c; asc A,f ;; 7: len 23; hex 75706c6f6164666972652e636f6d2f6 8616e642e706870; asc uploadfire.com/hand.php;; 8: len 8; hex 800000000000042b; asc +;; 9: len 4; hex 474bfa2b; asc GK +;; 10: len 8; hex 8000000000004e24; asc N$;;  
  20. *** (2) WAITING FOR THIS LOCK TO BE GRANTED: (任務(wù)2等待的鎖)  
  21. RECORD LOCKS space id 0 page no 843102 n bits 600 index `idx_1` of table `db/tab_test` trx id 0 677833454 lock_mode X locks rec but not gap waiting   
  22. Record lock, heap no 395 PHYSICAL RECORD: n_fields 3; compact format; info bits 0  
  23. 0: len 8; hex 8000000000000425; asc %;; 1: len 8; hex 800012412c66d29c; asc A,f ;; 2: len 8; hex 800000000097629c; asc b ;;  
  24. *** WE ROLL BACK TRANSACTION (1)  
  25. (回滾了任務(wù)1,以解除死鎖) 

 

原因分析:

當“update tab_test set state=1064,time=now() where state=1061 and time < date_sub(now(), INTERVAL 30 minute)”執(zhí)行時,MySQL會使用idx_1索引,因此首先鎖定相關(guān)的索引記錄,因為idx_1是非主鍵索引,為執(zhí)行該語句,MySQL還會鎖定主鍵索引。

假設(shè)“update tab_test set state=1067,time=now () where id in (9921180)”幾乎同時執(zhí)行時,本語句首先鎖定主鍵索引,由于需要更新state的值,所以還需要鎖定idx_1的某些索引記錄。

這樣***條語句鎖定了idx_1的記錄,等待主鍵索引,而第二條語句則鎖定了主鍵索引記錄,而等待idx_1的記錄,這樣死鎖就產(chǎn)生了。

6、解決辦法

拆分***條sql,先查出符合條件的主鍵值,再按照主鍵更新記錄:

 

  1. select id from tab_test where state=1061 and time < date_sub(now(), INTERVAL 30 minute);  
  2. update tab_test state=1064,time=now() where id in(......); 

 

關(guān)于MySQL死鎖問題的實例分析及解決方法就介紹到這里了,希望本次的介紹能夠?qū)δ兴斋@!

【編輯推薦】

  1. MySQL數(shù)據(jù)庫丟失root密碼后該如何補救?
  2. MySQL參數(shù)DELAY_KEY_WRITE的詳細說明
  3. MySQL數(shù)據(jù)庫TIMESTAMP設(shè)置默認值的靈活運用
  4. MySQL如何創(chuàng)建用戶表并將其ID設(shè)置為自動增長列?
  5. MySQL數(shù)據(jù)庫TIMESTAMP設(shè)置默認值的幾個應用實例
責任編輯:趙鵬 來源: CSDN博客
相關(guān)推薦

2017-06-14 22:11:57

數(shù)據(jù)庫MySQL死鎖

2018-05-29 11:44:22

數(shù)據(jù)庫MySQL死鎖

2009-07-01 18:14:36

JSP亂碼

2022-04-02 20:27:30

ETS操作系統(tǒng)鴻蒙

2011-05-06 17:25:58

硒鼓

2021-06-17 08:07:35

Linux 內(nèi)存站崗

2010-01-04 10:18:43

2025-04-07 07:20:35

SQL慢查詢性能

2010-08-31 13:49:12

CSS

2016-11-14 10:06:04

大數(shù)據(jù)max位圖

2009-03-04 10:38:36

Troubleshoo桌面虛擬化Xendesktop

2018-11-01 15:26:38

開源軟件安全

2019-10-11 19:45:28

SparkSQLHiveHadoop

2010-12-27 10:48:10

VirtualboxFreedos

2022-04-06 10:09:17

云服務(wù)云計算

2009-08-24 10:37:11

Silverlight

2021-01-12 11:40:12

SonarQube平臺數(shù)據(jù)項目授權(quán)

2021-06-08 08:38:36

MySQL數(shù)據(jù)庫死鎖問題

2010-06-17 11:17:34

裝系統(tǒng)出現(xiàn)Grub

2010-10-15 11:22:24

MySQL復制表結(jié)構(gòu)
點贊
收藏

51CTO技術(shù)棧公眾號