mysql插入忽略重复密钥更新导致死锁

iibxawm4  于 2021-06-18  发布在  Mysql
关注(0)|答案(0)|浏览(263)

我使用的是MySQL5.7版本,当时我使用insert ignore将大量数据插入表中。。在重复密钥更新时。如果找到相同的记录,则时间将被更新。下面是我的表模式。

CREATE TABLE `unauthuserpostview` (
    `postid` bigint(20) unsigned NOT NULL,
    `serial` varchar(255) DEFAULT NULL,
    `createdat` timestamp(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    UNIQUE KEY `idx_unauthuserpostview_postid_serial` (`postid`,`serial`)
    ) ENGINE=InnoDB

下面是我的批量插入查询。

INSERT IGNORE INTO unauthuserpostview(postid,serial) VALUES ( 44654, 'qpmpf' ),( 44653, 'qpmpf' ),( 44652, 'qpmpf' ),( 44651, 'qpmpf' ),( 44650, 'qpmpf' ),( 44649, 'qpmpf' ),( 44648, 'qpmpf' ),( 44647,             'qpmpf' ),( 44646, 'qpmpf' ),( 44645, 'qpmpf' )on duplicate key update createdat = now()

查询工作正常,但有时会导致死锁。为什么有时会导致死锁?如何解决?为了更好的性能,我使用批量插入。死锁结果如下:

LATEST DETECTED DEADLOCK
    ------------------------
    2018-12-08 08:36:10 0x7f3c5794c700
  ***(1) TRANSACTION:
    TRANSACTION 2488051, ACTIVE 0 sec inserting
    mysql tables in use 1, locked 1
    LOCK WAIT 3 lock struct(s), heap size 1136, 2 row lock(s), undo log entries 1
    MySQL thread id 65, OS thread handle 139897890834176, query id 12946 192.168.1.127 root update
     INSERT IGNORE INTO unauthuserpostview(postid,serial) VALUES ( 44654, 'qpmpf' ),( 44653, 'qpmpf' ),( 44652, 'qpmpf' ),( 44651, 'qpmpf' ),( 44650, 'qpmpf' ),( 44649, 'qpmpf' ),( 44648, 'qpmpf' ),( 44647,            'qpmpf' ),( 44646, 'qpmpf' ),( 44645, 'qpmpf' )on duplicate key update createdat = now()
  ***(1) WAITING FOR THIS LOCK TO BE GRANTED:
    RECORD LOCKS space id 422 page no 79 n bits 592 index idx_unauthuserpostview_postid_serial of table `development`.`unauthuserpostview` trx id 2488051 lock_mode X locks gap before rec insert intention   waiting
    Record lock, heap no 491 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
     0: len 8; hex 000000000000ae6e; asc        n;;
     1: len 5; hex 7172656d73; asc qrems;;
     2: len 6; hex 00000089bb9e; asc       ;;

  ***(2) TRANSACTION:
    TRANSACTION 2488045, ACTIVE 0 sec inserting
    mysql tables in use 1, locked 1
    4 lock struct(s), heap size 1136, 3 row lock(s), undo log entries 1
    MySQL thread id 113, OS thread handle 139897144133376, query id 12936 192.168.1.127 root update
    INSERT IGNORE INTO unauthuserpostview(postid,serial) VALUES ( 44654, 'qfdvj' ),( 44653, 'qfdvj' ),( 44652, 'qfdvj' ),( 44651, 'qfdvj' ),( 44650, 'qfdvj' ),( 44649, 'qfdvj' ),( 44648, 'qfdvj' ),( 44647,             'qfdvj' ),( 44646, 'qfdvj' ),( 44645, 'qfdvj' )on duplicate key update createdat = now()
  ***(2) HOLDS THE LOCK(S):
    RECORD LOCKS space id 422 page no 79 n bits 576 index idx_unauthuserpostview_postid_serial of table `development`.`unauthuserpostview` trx id 2488045 lock_mode X locks gap before rec
    Record lock, heap no 491 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
     0: len 8; hex 000000000000ae6e; asc        n;;
     1: len 5; hex 7172656d73; asc qrems;;
     2: len 6; hex 00000089bb9e; asc       ;;

  ***(2) WAITING FOR THIS LOCK TO BE GRANTED:
    RECORD LOCKS space id 422 page no 79 n bits 592 index idx_unauthuserpostview_postid_serial of table `development`.`unauthuserpostview` trx id 2488045 lock_mode X locks gap before rec insert intention waiting
    Record lock, heap no 491 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
     0: len 8; hex 000000000000ae6e; asc        n;;
     1: len 5; hex 7172656d73; asc qrems;;
     2: len 6; hex 00000089bb9e; asc       ;;

  ***WE ROLL BACK TRANSACTION (1)

下面是我经常使用的select查询。

SELECT p.postid from postmst p  
left JOIN unauthuserpostview uv on p.postid = uv.postid and uv.serial 
= 'qpmpf' 
where uv.postid is NULL ORDER by p.postid desc LIMIT 50;

那么我的索引是否适合上面的查询呢?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题