MySQL死锁问题分析及解决方法实例详解

数据库 MySQL
MySQL死锁问题是很多程序员在项目开发中常遇到的问题,现就MySQL死锁及解决方法进行详解。

[[194051]]

MySQL死锁问题是很多程序员在项目开发中常遇到的问题,现就MySQL死锁及解决方法详解如下:

1、MySQL常用存储引擎的锁机制

MyISAM和MEMORY采用表级锁(table-level locking)

BDB采用页面锁(page-level locking)或表级锁,默认为页面锁

InnoDB支持行级锁(row-level locking)和表级锁,默认为行级锁

2、各种锁特点

表级锁:开销小,加锁快;不会出现死锁;锁定粒度大,发生锁冲突的概率***,并发度***

行级锁:开销大,加锁慢;会出现死锁;锁定粒度最小,发生锁冲突的概率***,并发度也***

页面锁:开销和加锁时间界于表锁和行锁之间;会出现死锁;锁定粒度界于表锁和行锁之间,并发度一般

3、各种锁的适用场景

表级锁更适合于以查询为主,只有少量按索引条件更新数据的应用,如Web应用

行级锁则更适合于有大量按索引条件并发更新数据,同时又有并发查询的应用,如一些在线事务处理系统

4、死锁

是指两个或两个以上的进程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去。

表级锁不会产生死锁.所以解决死锁主要还是针对于最常用的InnoDB.

5、死锁举例分析

在MySQL中,行级锁并不是直接锁记录,而是锁索引。索引分为主键索引和非主键索引两种,如果一条sql语句操作了主键索引,MySQL就会锁定这条主键索引;如果一条语句操作了非主键索引,MySQL会先锁定该非主键索引,再锁定相关的主键索引。

在UPDATE、DELETE操作时,MySQL不仅锁定WHERE条件扫描过的所有索引记录,而且会锁定相邻的键值,即所谓的next-key locking。

例如,一个表db.tab_test,结构如下:

id:主键;

state:状态;

time:时间;

索引:idx_1 (state, time)

出现死锁日志如下:

  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) (任务1的sql语句)  
  7. ***(1) WAITING FOR THIS LOCK TO BE GRANTED: (任务1等待的索引记录)  
  8. RECORD LOCKS space id 0 page no 849384 n bits 208 index `PRIMARYof 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) (任务2的sql语句)  
  16. *** (2) HOLDS THE LOCK(S): (任务2已获得的锁)  
  17. RECORD LOCKS space id 0 page no 849384 n bits 208 index `PRIMARYof 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: (任务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. (回滚了任务1,以解除死锁) 

原因分析:

当“update tab_test set state=1064,time=now() where state=1061 and time < date_sub(now(), INTERVAL 30 minute)”执行时,MySQL会使用idx_1索引,因此首先锁定相关的索引记录,因为idx_1是非主键索引,为执行该语句,MySQL还会锁定主键索引。

假设“update tab_test set state=1067,time=now () where id in (9921180)”几乎同时执行时,本语句首先锁定主键索引,由于需要更新state的值,所以还需要锁定idx_1的某些索引记录。

这样***条语句锁定了idx_1的记录,等待主键索引,而第二条语句则锁定了主键索引记录,而等待idx_1的记录,这样死锁就产生了。

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(......); 

至此MySQL死锁问题得以解决! 

责任编辑:庞桂玉 来源: segmentfault
相关推荐

2011-08-24 17:41:16

MySQL死锁

2018-05-29 11:44:22

数据库MySQL死锁

2009-07-01 18:14:36

JSP乱码

2023-12-08 18:01:25

Java关键字

2010-08-12 09:30:08

Flex内存泄露

2022-04-02 20:27:30

ETS操作系统鸿蒙

2021-06-17 08:07:35

Linux 内存站岗

2011-05-06 17:25:58

硒鼓

2021-06-08 08:38:36

MySQL数据库死锁问题

2011-07-05 09:44:31

QT Mysql 乱码

2010-01-04 10:18:43

2010-08-03 09:12:52

Flex安全沙箱

2022-04-06 10:09:17

云服务云计算

2010-12-27 10:48:10

VirtualboxFreedos

2009-08-24 10:37:11

Silverlight

2021-01-12 11:40:12

SonarQube平台数据项目授权

2009-02-18 14:28:23

编码乱码JSP

2018-11-01 15:26:38

开源软件安全

2010-08-31 13:49:12

CSS

2016-11-14 10:06:04

大数据max位图
点赞
收藏

51CTO技术栈公众号