这个存储过程怎么会导致死锁?

时间:2017-08-25 12:00:16

标签: mysql stored-procedures

我有一个存储过程,可以同时被多个mysql连接调用。

经过一个月没有错误,昨晚我收到了几条错误消息:

“尝试获取锁定时发现死锁尝试重新启动事务”

存储过程如下:

CREATE PROCEDURE pull_updates(IN table_name VARCHAR(255))

BEGIN  

CREATE TEMPORARY TABLE IF NOT EXISTS dates_to_process AS 
select * from export_progress where tablename = table_name and
progress_message = "imdb_to_mysql_success" and cs_sd_date not  
in (select cs_sd_date from export_progress where tablename = 
table_name and progress_message = "mysql_to_domo_success"); 

insert into export_progress(tablename, cs_sd_date, ts, progress_message) 
select table_name, cs_sd_date, now(), "mysql_to_domo_success" from dates_to_process;

SET @t1 = CONCAT('select t1.* from ', table_name,' t1 join dates_to_process t2 
on t1.cs_sd >= t2.cs_sd_date and t1.cs_sd < (t2.cs_sd_date + INTERVAL 1 DAY)' );
PREPARE stmt3 FROM @t1;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;

DROP TEMPORARY TABLE IF EXISTS dates_to_process;

END

我在AWS RDS mysql实例上运行它 服务器版本:5.6.29-log MySQL社区服务器(GPL)

我不明白为什么这会陷入僵局。

  • 1)创建一个临时表(如果我,则为一个连接范围) 正确理解)
  • 2)从中选择几行 table export_progress
  • 3)写入一些行INTO export_progress
  • 4) 从表中返回一些行,加入临时表
  • 5) 删除临时表

我在这里看不到死锁的可能性,但我必须错过一些东西,因为我得到了一个!

谢谢你的帮助!

编辑1:感谢死锁消息,感谢来自@fancyPants的评论:

------------------------
LATEST DETECTED DEADLOCK
------------------------
2017-08-25 04:58:12 2ba1acaf7700
*** (1) TRANSACTION:
TRANSACTION 1643978, ACTIVE 1 sec inserting
mysql tables in use 2, locked 2
LOCK WAIT 38 lock struct(s), heap size 6544, 5242 row lock(s), undo log entries 1
MySQL thread id 125547, OS thread handle 0x2ba26ea85700, query id 3210748  domo Sending data
insert into export_progress(tablename, cs_sd_date, ts, progress_message) select  NAME_CONST('table_name',_utf8'AUTO_coaching' COLLATE 'utf8_general_ci'), cs_sd_date, now(), "mysql_to_domo_success" from dates_to_process
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 5167 page no 34 n bits 240 index `GEN_CLUST_INDEX` of table `autouk`.`export_progress` trx id 1643978 lock_mode X insert intention waiting
Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
 0: len 8; hex 73757072656d756d; asc supremum;;

*** (2) TRANSACTION:
TRANSACTION 1643980, ACTIVE 1 sec inserting
mysql tables in use 2, locked 2
38 lock struct(s), heap size 6544, 5242 row lock(s), undo log entries 1
MySQL thread id 125548, OS thread handle 0x2ba1acaf7700, query id 3210749  domo Sending data
insert into export_progress(tablename, cs_sd_date, ts, progress_message) select  NAME_CONST('table_name',_utf8'AUTO_backend' COLLATE 'utf8_general_ci'), cs_sd_date, now(), "mysql_to_domo_success" from dates_to_process
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 5167 page no 34 n bits 240 index `GEN_CLUST_INDEX` of table `autouk`.`export_progress` trx id 1643980 lock mode S
Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
 0: len 8; hex 73757072656d756d; asc supremum;

....

*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 5167 page no 34 n bits 240 index `GEN_CLUST_INDEX` of table `autouk`.`export_progress` trx id 1643980 lock_mode X insert intention waiting
Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
 0: len 8; hex 73757072656d756d; asc supremum;;

*** WE ROLL BACK TRANSACTION (2)

0 个答案:

没有答案