mysql 一次死锁的处理

昨天用apache bench压操作的时候发生严重的死锁。

查了一下操作:

操作1,insert into tableA 然后 用tableA 的数据 update tableB 

操作2,delete from tableA 然后 用tableA 的数据 update tableB 

用了很小的并发,去执行 操作1后立即执行操作2。发现只有两条成功,后面的都在等待锁 。

异常:mysql 'Deadlock found when trying to get lock; try restarting transaction'

从操作上看,两个操作的顺序是对的,不应该发生死锁。

然后看了sql

操作2:

[sql]  view plain copy
  1. delete from tableB where SId = 987  

[sql]  view plain copy
  1. update tableA a  
  2.         JOIN  
  3.     (select   
  4.         '123456' AS CId,  
  5.             count(c.SId) as Count1,  
  6.             IFNULL(SUM(CASE  
  7.                 WHEN s.dType = 'a' THEN 1  
  8.                 ELSE 0  
  9.             END), 0) as Count2,  
  10.             IFNULL(SUM(CASE  
  11.                 WHEN s.dType = 'b' THEN 1  
  12.                 ELSE 0  
  13.             END), 0) as Count3  
  14.     from  
  15.         tableB c  
  16.     left JOIN tableC s ON c.CId = s.Id  
  17.     where  
  18.         c.CId = '123456'as co ON a.Id = co.CId   
  19. set   
  20.     a.Count1 = co.Count1,  
  21.     a.Count2 = co.Count2,  
  22.     a.Count3 = co.Count3  

然后分析认为后面的sql 在更新tableA时,可能导致了tableB被锁,因此导致死锁。

然后把,后面那个sql的查询的部分拆出来,然后用查询的结果去更新tableA。

拆出来后发现没有了死锁

你可能感兴趣的:(mysql 一次死锁的处理)