标签:条件 测试 cte 记录 undo log ble const inf gap
1、原表必须存在主键 PRIMARY KEY 或者 UNIQUE KEY
The new table `darren`.`_t_user_new` does not have a PRIMARY KEY or a unique index which is required for the DELETE trigger.
Please check you have at least one UNIQUE and NOT NULLABLE index.
2、支持有外键约束的表,需要加上--alter-foreign-keys-method=rebuild_constraints参数
3、默认原表不能存在触发器。但是需要--preserve-triggers变量,不建议这么做,可能存在风险
The table `test`.`foo` has triggers but --preserve-triggers was not specified.
4、在pt-osc的执行过程中,如果有对主键的更新操作则会出现重复的数据,在3.02版本中已经修复
3.02之前:
更新触发器对应的sql语句:REPLACE INTO $new_tbl->{name} ($qcols) VALUES ($new_vals);
3.02之后:
DELETE IGNORE FROM $new_tbl->{name} WHERE !($upd_index_cols) AND $del_index_cols;
REPLACE INTO $new_tbl->{name} ($qcols) VALUES ($new_vals);
5、innodb_autoinc_lock_mode=1测试
当innodb_autoinc_lock_mode =1:
对于 bulk inserts,使用传统表锁的 AUTO-INC Locking 方式。
------------------------
LATEST DETECTED DEADLOCK
------------------------
2018-10-24 16:44:13 0x7f93585e3700
*** (1) TRANSACTION:
TRANSACTION 275263782, ACTIVE 0 sec setting auto-inc lock
mysql tables in use 2, locked 2
LOCK WAIT 4 lock struct(s), heap size 1136, 1 row lock(s), undo log entries 2
MySQL thread id 190, OS thread handle 140269612435200, query id 20289531 10.126.126.164 darren update
REPLACE INTO `sbtest`.`_sbtest1_new` (`id`, `c4`, `k`, `c`, `pad`, `c3`, `c5`, `c6`, `c7`, `c14`, `c9`, `c10`) VALUES (NEW.`id`, NEW.`c4`, NEW.`k`, NEW.`c`, NEW.`pa
d`, NEW.`c3`, NEW.`c5`, NEW.`c6`, NEW.`c7`, NEW.`c14`, NEW.`c9`, NEW.`c10`)
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
TABLE LOCK table `sbtest`.`_sbtest1_new` trx id 275263782 lock mode AUTO-INC waiting
*** (2) TRANSACTION:
TRANSACTION 275263754, ACTIVE 0 sec fetching rows, thread declared inside InnoDB 4212
mysql tables in use 2, locked 2
258 lock struct(s), heap size 41168, 17913 row lock(s), undo log entries 17886
MySQL thread id 204, OS thread handle 140270819489536, query id 20289201 10.126.126.164 darren Sending data
INSERT LOW_PRIORITY IGNORE INTO `sbtest`.`_sbtest1_new` (`id`, `c4`, `k`, `c`, `pad`, `c3`, `c5`, `c6`, `c7`, `c14`, `c9`, `c10`) SELECT `id`, `c4`, `k`, `c`, `pad`
, `c3`, `c5`, `c6`, `c7`, `c14`, `c9`, `c10` FROM `sbtest`.`sbtest1` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ‘1481317‘)) AND ((`id` <= ‘1506548‘)) LOCK IN SHARE MODE
/*pt-online-schema-change 69839 copy nibble*/
*** (2) HOLDS THE LOCK(S):
TABLE LOCK table `sbtest`.`_sbtest1_new` trx id 275263754 lock mode AUTO-INC
*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 15248 page no 25707 n bits 152 index PRIMARY of table `sbtest`.`sbtest1` trx id 275263754 lock mode S locks rec but not gap waiting
Record lock, heap no 40 PHYSICAL RECORD: n_fields 15; compact format; info bits 0
0: len 8; hex 000000000016e050; asc P;;
1: len 6; hex 000010683126; asc h1&;;
2: len 7; hex 01000007722b37; asc r+7;;
3: len 1; hex 30; asc 0;;
4: len 4; hex 00385728; asc 8W(;;
5: len 30; hex 34313331333339383035342d30323832343038383036372d383038353233; asc 41313398054-02824088067-808523; (total 119 bytes);
6: len 30; hex 34393730393732313932382d31333738353836373533322d393836313330; asc 49709721928-13785867532-986130; (total 59 bytes);
7: len 4; hex 80000000; asc ;;
8: len 1; hex 30; asc 0;;
9: len 1; hex 30; asc 0;;
10: len 1; hex 30; asc 0;;
11: len 1; hex 30; asc 0;;
12: len 4; hex 80000000; asc ;;
13: len 4; hex 80000000; asc ;;
14: len 4; hex 80000000; asc ;;
*** WE ROLL BACK TRANSACTION (1)
根据死锁信息可以得到 2个信息:
(1)事务1在等待"_sbtest1_new"表的AUTO-INC表锁;
(2)事务2持有"_sbtest1_new"的AUTO-INC表锁,等待"sbtest1"的记录锁。
事务1的replace into语句,明显是跑pt-osc创建的触发器产生的,当原表产生记录更新时,
触发器并将记录以replace方式同步到新表。
事务1:
(1)根据条件更新,对sbtest1持有排他的RECORD LOCKS;
(2)更新后触发器被触发,再以replace的方式插入_sbtest1_new表,需要对_sbtest1_new持有一个隐式的自增锁。
事务2:
(1)insert into select from,首先对_sbtest1_new加上了表级的自增锁;
(2)对新表加上表锁后,再根据条件中主键id的范围区间去申请原表sbtest1的记录锁。
由上,由于事务1先更新原表sbtest1,对更新的记录加上排它锁,触发器还没触发时,
事务2开始执行,这个时候事务2现对新表加表锁,当它再去申请对原表加记录级别的共享锁时,
发现部分记录被加上了排他锁,所以需要等待。这时事务1触发器触发了,需要对新表获取一个自增锁,造成了回环,产生死锁。
标签:条件 测试 cte 记录 undo log ble const inf gap
原文地址:https://www.cnblogs.com/mysql-dba/p/9901630.html