如何解决MySQL死锁问题?

时间:2017-06-18 08:53:17

标签: mysql database-deadlocks

我解决MySQL死锁问题有点迷失。这就是我如何陷入僵局。

线程#1运行:

    BEGIN;
    SELECT
        sequence_number,
        status
    FROM
        sns_preorders t
    WHERE
        t.program_user_id=1
    ORDER BY
        sequence_number ASC
    FOR UPDATE;

线程#2运行:

    BEGIN;
    SELECT
        sequence_number,
        status
    FROM
        sns_preorders t
    WHERE
        t.program_user_id=2
    ORDER BY
        sequence_number ASC
    FOR UPDATE;

同时线程#1运行(并且它会挂起,直到第二个线程执行相同的操作):

        INSERT INTO
            sns_preorders
        SET
            program_user_id=1,
            gift_id=1,
            handling_date=now(),
            delivery_date=now(),
            total_price=1000,
            sequence_number=1,
            status=1;    

几乎同时线程#2运行:

        INSERT INTO
            sns_preorders
        SET
            program_user_id=2,
            gift_id=1,
            handling_date=now(),
            delivery_date=now(),
            total_price=1000,
            sequence_number=1,
            status=1;    

线程#2成功执行请求,但线程1返回错误Error Code: 1213. Deadlock found when trying to get lock; try restarting transaction

也许我对死锁的理解是错误的,但我认为如果我按特定的program_user_id锁定行,那么应该没有死锁。这可以解决什么问题?

UPDATE :这是一个索引列表

mysql> show index from sns_preorders;
+---------------+------------+---------------------------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table         | Non_unique | Key_name                        | Seq_in_index | Column_name     | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------------+------------+---------------------------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| sns_preorders |          0 | PRIMARY                         |            1 | id              | A         |           1 |     NULL | NULL   |      | BTREE      |         |               |
| sns_preorders |          0 | program_user_id_sequence_number |            1 | program_user_id | A         |           1 |     NULL | NULL   | YES  | BTREE      |         |               |
| sns_preorders |          0 | program_user_id_sequence_number |            2 | sequence_number | A         |           1 |     NULL | NULL   |      | BTREE      |         |               |
| sns_preorders |          1 | IDX_26A332972C842B41            |            1 | program_user_id | A         |           1 |     NULL | NULL   | YES  | BTREE      |         |               |
| sns_preorders |          1 | IDX_26A3329797A95A83            |            1 | gift_id         | A         |           1 |     NULL | NULL   | YES  | BTREE      |         |               |
| sns_preorders |          1 | sequence_number                 |            1 | sequence_number | A         |           1 |     NULL | NULL   |      | BTREE      |         |               |
+---------------+------------+---------------------------------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+

0 个答案:

没有答案