Closed2

SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction

kouheikouhei

504 Gateway Timeoutが発生していると報告
下記のようなエラーが出ていることを確認

SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction (SQL: select * from `tasks` where `progress_status` = 0 order by `id` asc limit 1 for update)
kouheikouhei

原因となる箇所のコード

DB::transaction(function () use ($id,$user_id) {

            $t = Task::lockForUpdate()
                ->where('id','=',$id)
                ->where('user_id','=',$user_id)
                ->first();

            DB::update("UPDATE task_outputs set target_url = NULL WHERE task_id = :id",[
                $t->id
            ]);

            ...
            ...
            $t->progress_status = 0;
            $t->task_site_type = 3;
            $t->save();

});

↓ここにインデックスが貼られておらずフルスキャンされていた

UPDATE task_outputs set target_url = NULL WHERE task_id = :id

普通にインデックスを貼ってあげて解決。

MariaDB > create index taskidx on task_outputs(task_id);
Query OK, 0 rows affected (5 min 9.525 sec)
Records: 0  Duplicates: 0  Warnings: 0

データをcountすると150万件ほど。
140万件あたりまではインデックスなしで頑張ってたようです。

行ロック(innodb_lock_wait_timeout)はデフォルト50秒。
https://dev.mysql.com/doc/refman/5.7/en/innodb-parameters.html#sysvar_innodb_lock_wait_timeout

MariaDB > show variables like 'innodb_lock_wait_timeout';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| innodb_lock_wait_timeout | 50    |
+--------------------------+-------+
1 row in set (0.005 sec)

https://stackoverflow.com/questions/5836623/getting-lock-wait-timeout-exceeded-try-restarting-transaction-even-though-im

このスクラップは2022/09/07にクローズされました