MySQL:单个表中的死锁UPDATE ... WHERE

时间:2016-11-07 10:03:22

标签: mysql transactions database-deadlocks database-concurrency

下一个问题提出了这个配置:

  • MySQL 5.7.10
  • Spring 4.0.5
  • Spring Batch 3.0.1
  • Spring ThreadPoolTask​​Executor介于10到20个线程之间

当某些线程尝试在单个表中执行UPDATE ... WHERE时,问题就是死锁。

表格是:

CREATE TABLE IF NOT EXISTS `invoice_events` (
  `INTERNAL_ID` bigint(20) NOT NULL,
  `FECHA_FAC` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `PERIOD_TYPE` varchar(50) COLLATE utf8_spanish_ci DEFAULT NULL,
  `PRODUCT_ID` varchar(50) COLLATE utf8_spanish_ci DEFAULT NULL,
  `RATE_ID` varchar(50) COLLATE utf8_spanish_ci DEFAULT NULL,
  `INVOICE_INTERNAL_ID` bigint(20) unsigned DEFAULT NULL,
  `COUNTRY_CODE` varchar(4) COLLATE utf8_spanish_ci DEFAULT NULL,
  `SOURCE_MSISDN` varchar(50) COLLATE utf8_spanish_ci DEFAULT NULL,
  `TARGET_MSISDN` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
  `CATEGORY` varchar(50) COLLATE utf8_spanish_ci DEFAULT NULL,
  `SERVICE` varchar(50) COLLATE utf8_spanish_ci DEFAULT NULL,
  `USAGE_TYPE` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
  `BT_COST` double(22,6) DEFAULT NULL,
  PRIMARY KEY (`INTERNAL_ID`,`FECHA_FAC`),
  KEY `IDX_INV_INT_ID` (`INVOICE_INTERNAL_ID`),
  KEY `IDX_MSISDN` (`SOURCE_MSISDN`),
  KEY `IDX_FECHA_FAC` (`FECHA_FAC`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci
PARTITION BY RANGE( TO_DAYS(FECHA_FAC) ) (
    PARTITION p201511 VALUES LESS THAN (TO_DAYS('2015-12-01')),
    PARTITION p201512 VALUES LESS THAN (TO_DAYS('2016-01-01')),
    PARTITION p201601 VALUES LESS THAN (TO_DAYS('2016-02-01')),
    PARTITION p201602 VALUES LESS THAN (TO_DAYS('2016-03-01')),
    PARTITION p201603 VALUES LESS THAN (TO_DAYS('2016-04-01')),
    PARTITION p201604 VALUES LESS THAN (TO_DAYS('2016-05-01')),
    PARTITION p201605 VALUES LESS THAN (TO_DAYS('2016-06-01')),
    PARTITION p201606 VALUES LESS THAN (TO_DAYS('2016-07-01')),
    PARTITION p201607 VALUES LESS THAN (TO_DAYS('2016-08-01')),
    PARTITION p201608 VALUES LESS THAN (TO_DAYS('2016-09-01')),
    PARTITION p201609 VALUES LESS THAN (TO_DAYS('2016-10-01')),
    PARTITION p201610 VALUES LESS THAN (TO_DAYS('2016-11-01')),
    PARTITION p201611 VALUES LESS THAN (TO_DAYS('2016-12-01')),
    PARTITION future VALUES LESS THAN MAXVALUE
  );

UPDATE语句是:

update invoice_events  
set invoice_internal_id = 978202
where fecha_fac between '2016-02-01 00:00:00' and '2016-05-31 23:59:59.999' 
and source_msisdn = '239642983472' 
and invoice_internal_id is null  
and country_code = 'ES';

这个语句的MySQL的EXPLAIN是: enter image description here

问题的SHOW STATUS ENGINE是:

2016-06-03 11:08:23 0x7f8bcc1aa700
*** (1) TRANSACTION:
TRANSACTION 7031093, ACTIVE 0 sec fetching rows
mysql tables in use 1, locked 1
LOCK WAIT 406 lock struct(s), heap size 41168, 2884 row lock(s), undo log entries 375
MySQL thread id 4726, OS thread handle 140238271616768, query id 10226209 172.30.6.9 BDUSER updating
update invoice_events  set invoice_internal_id = 978173 where fecha_fac between '2016-02-01 00:00:00' and '2016-05-31 23:59:59.999' and source_msisdn in ( '239642983345' )  and invoice_internal_id is null  and country_code = 'ES'
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 12252 page no 54015 n bits 512 index IDX_MSISDN of table `my_schema`.`invoice_events` /* Partition `p201603` */ trx id 7031093 lock_mode X waiting
Record lock, heap no 101 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
 0: len 16; hex 33303835303732313033323532303132; asc 3085072103252012;;
 1: len 8; hex 80000000005d2fa3; asc      ]/ ;;
 2: len 5; hex 9998fe0000; asc      ;;

*** (2) TRANSACTION:
TRANSACTION 7031094, ACTIVE 0 sec starting index read, thread declared inside InnoDB 4999
mysql tables in use 1, locked 1
424 lock struct(s), heap size 41168, 3945 row lock(s), undo log entries 628
MySQL thread id 4731, OS thread handle 140238401480448, query id 10226408 172.30.6.9 BDUSER updating
update invoice_events  set invoice_internal_id = 978202 where fecha_fac between '2016-02-01 00:00:00' and '2016-05-31 23:59:59.999' and source_msisdn in ( '239642983472' )  and invoice_internal_id is null  and country_code = 'ES'
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 12252 page no 54015 n bits 512 index IDX_MSISDN of table `my_schema`.`invoice_events` /* Partition `p201603` */ trx id 7031094 lock_mode X
Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
 0: len 8; hex 73757072656d756d; asc supremum;;

从innodb数据库变量中,我注意到 innodb_thread_concurrency 的值是32。

所选分区包含21853907行。

我尝试制作一个简单的UPDATE语句。我只使用fecha_fac进行过滤以访问所需的分区,然后使用索引字段进行过滤。

所以,我的第一个主要问题是:如何修复此事务锁定?任何建议,小费,......?

任何其他重要问题是:

  • 从EXPLAIN输出:即使在单个表更新中,类型(范围)也可以更好吗?或者它是单表更新的最佳类型?
  • 从EXPLAIN输出:最后一个键是IDX_INV_INT_ID而不是IDX_MSISDN是对的吗? IDX_INV_INT_ID索引空列。
  • 是否帮助innodb_thread_concurrency参数设置为0(无限并发)?
  • SHOW STATUS输出中是否有任何数据可以帮助我和我没有注意到?

除了我的问题,我们非常感谢任何帮助或建议。

提前致谢。

1 个答案:

答案 0 :(得分:0)

正如@Shadow建议的那样,我查看了索引,只留下了IDX_MSISDN。

数据库设计的其他良好实践和避免死锁的原因是:

  • 重新排序列。可能的NULL和/或未索引的列已移至末尾。
  • 使用PROCEDURE ANALYZE()调整列数据类型和大小。例如,SOURCE_MSISDN已经减少,因此key_len也减少了。
  • 同步交易。每个Spring Batch项只执行一个事务。在我的设计中,吞吐量降低了约5-10%,并且可以管理。

希望它有所帮助。