LMLPHP后院

MySQL update where in 子查询导致锁表技术

maybe yes 发表于 2021-01-25 03:57

说实话,我第一次碰都这样的报错,因为我在 update 语句的条件里面使用了子查询,我以为执行会很快,因为子查询我试过,就一两秒的事情数据也没多少个,90 来条数据修改的条件是主键 ID,整个 SQL 执行下来我以为也就两秒,谁知道,执行了第一次,发现卡住了,于是 Ctrl+c 终止了,以为是网络问题,于是又重试了一次,还是不对了,于是远程删除了进程。

到此,我以为进程也清理了,应该没事了,结果到了凌晨,报错了,查询数据库,原来是锁住了。

大家看看我的报错堆栈,不算深,给大家看看而已,这个不是最深的。去掉了一些路径:

[ 2021-01-25T01:44:58+08:00 ] /index/script/job, LmlException: HY000
1205                                                                                                                                                                                   
Lock wait timeout exceeded; try restarting transaction in MysqlPdoEnhance.php:109
Stack trace:
#0 MysqlPdoEnhance.php(153): MysqlPdoEnhance->query('UPDATE _s...', Array)
#1 Model.php(39): MysqlPdoEnhance->update('_s...', Array, 'id=123584', Array)
#2 spider.php(908): Model->update(Array, 'id=123584')
#3 job.php(26): Job->{closure}(20)
#4 job.php(28): {closure}() 
#5 [internal function]: {closure}()
#6 job.php(293): Generator->next()
#7 job.php(302): Job->start()
#8 LmlBase.php(53): include('lmlphp...')
#9 LmlBase.php(39): LmlBase->fetch('lmlphp...')
#10 Index.php(22): LmlBase->display('', '/job.php')
#11 [internal function]: Index->script()
#12 lml.min.php(1): ReflectionMethod->invoke(Object(Index))
#13 index.php(134): LmlApp->run(true)
#14 {main}

解决方法

select * from information_schema.innodb_trx;
SELECT * from information_schema.`PROCESSLIST`;

找到死锁的线程,kill 掉。

mysql 死锁场景 死锁案例 子查询

2024-04-25 14:16:25 1714025785 0.007984