一个朋友问我在线对大表进行ddl操作,如何做能尽量避免主从延迟以及不影响在线dml操作呢?我想到一个开源的pt-online-schema-change工具,测试了吧,效果还可以。

pt-online-schema-change原理

1、如果存在外键,根据alter-foreign-keys-method参数的值,检测外键相关的表,做相应设置的处理。

2、创建一个新的表,表结构为修改后的数据表,用于从源数据表向新表中导入数据。

3、创建触发器,用于记录从拷贝数据开始之后,对源数据表继续进行数据修改的操作记录下来,用于数据拷贝结束后,执行这些操作,保证数据不会丢失。

4、拷贝数据,从源数据表中拷贝数据到新表中。

5、修改外键相关的子表,根据修改后的数据,修改外键关联的子表。

6、rename源数据表为old表,把新表rename为源表名,并将old表删除。

7、删除触发器。

1,安装Percona

tar -zxvf DBI-1.625.tar.gz
cd DBI-1.625
perl Makefile.PL
make
make install

# 安装DBD-mysql插件

tar -zxvf DBD-mysql-4.023.tar.gz
cd DBD-mysql-4.023
perl Makefile.PL
make
make install

安装percona-toolkit,pt-online-schema-change 是percona里面的组件之一,通常安装好percona-toolkit之后,基本就能直接用:

##Install percona-toolkit
wget percona.com/get/percona-toolkit.tar.gz
tar -zxvf percona-toolkit-2.2.16.tar.gz
cd percona-toolkit-2.2.16
perl Makefile.PL
make
make install

2,常用操作:

2.1 添加字段

命令:

time pt-online-schema-change –host=192.168.121.91 –port=3307 –user=tim –password=”timisgood” –alter=”add column C_N varchar(64)” –execute D=test,t=UC_USER –set-vars innodb_lock_wait_timeout=50 –no-check-replication-filters

执行过程

[root@hch_test_121_91 ~]# time pt-online-schema-change --host=192.168.121.91 --port=3307 --user=tim --password="timisgood" --alter="add column C_N varchar(64)" --execute D=test,t=UC_USER --set-vars innodb_lock_wait_timeout=50 --no-check-replication-filters
No slaves found.  See --recursion-method if host hch_test_121_91 has slaves.
Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
Operation, tries, wait:
  analyze_table, 10, 1
  copy_rows, 10, 0.25
  create_triggers, 10, 1
  drop_triggers, 10, 1
  swap_tables, 10, 1
  update_foreign_keys, 10, 1
Altering `test`.`UC_USER`...
Creating new table...
Created new table test._UC_USER_new OK.
Altering new table...
Altered `test`.`_UC_USER_new` OK.
2017-07-06T11:25:34 Creating triggers...
2017-07-06T11:25:34 Created triggers OK.
2017-07-06T11:25:34 Copying approximately 1457112 rows...
Copying `test`.`UC_USER`:  21% 01:49 remain
Copying `test`.`UC_USER`:  42% 01:20 remain
Copying `test`.`UC_USER`:  62% 00:54 remain
Copying `test`.`UC_USER`:  91% 00:11 remain
2017-07-06T11:27:45 Copied rows OK.
2017-07-06T11:27:45 Analyzing new table...
2017-07-06T11:27:45 Swapping tables...
2017-07-06T11:27:45 Swapped original and new tables OK.
2017-07-06T11:27:45 Dropping old table...
2017-07-06T11:27:46 Dropped old table `test`.`_UC_USER_old` OK.
2017-07-06T11:27:46 Dropping triggers...
2017-07-06T11:27:46 Dropped triggers OK.
Successfully altered `test`.`UC_USER`.

real    2m12.995s
user    0m0.869s
sys 0m0.160s
[root@hch_test_121_91 ~]#

2.2 修改字段

命令:

time pt-online-schema-change –host=192.168.121.91 –port=3307 –user=tim –password=”timisgood” –alter=”modify column C_N varchar(128) default 0” –execute D=test,t=UC_USER –set-vars innodb_lock_wait_timeout=50 –no-check-replication-filters

执行过程:

[root@hch_test_121_91 ~]# time pt-online-schema-change --host=192.168.121.91 --port=3307 --user=tim --password="timisgood" --alter="modify column C_N varchar(128) default 0" --execute D=test,t=UC_USER --set-vars innodb_lock_wait_timeout=50 --no-check-replication-filters
No slaves found.  See --recursion-method if host hch_test_121_91 has slaves.
Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
Operation, tries, wait:
  analyze_table, 10, 1
  copy_rows, 10, 0.25
  create_triggers, 10, 1
  drop_triggers, 10, 1
  swap_tables, 10, 1
  update_foreign_keys, 10, 1
Altering `test`.`UC_USER`...
Creating new table...
Created new table test._UC_USER_new OK.
Altering new table...
Altered `test`.`_UC_USER_new` OK.
2017-07-05T19:08:46 Creating triggers...
2017-07-05T19:08:46 Created triggers OK.
2017-07-05T19:08:46 Copying approximately 1457112 rows...
Copying `test`.`UC_USER`:  23% 01:36 remain
Copying `test`.`UC_USER`:  51% 00:56 remain
Copying `test`.`UC_USER`:  71% 00:36 remain
Copying `test`.`UC_USER`:  91% 00:11 remain
2017-07-05T19:11:02 Copied rows OK.
2017-07-05T19:11:02 Analyzing new table...
2017-07-05T19:11:02 Swapping tables...
2017-07-05T19:11:02 Swapped original and new tables OK.
2017-07-05T19:11:02 Dropping old table...
2017-07-05T19:11:03 Dropped old table `test`.`_UC_USER_old` OK.
2017-07-05T19:11:03 Dropping triggers...
2017-07-05T19:11:03 Dropped triggers OK.
Successfully altered `test`.`UC_USER`.

real    2m17.788s
user    0m0.839s
sys 0m0.122s
[root@hch_test_121_91 ~]#

2.3 改字段名

执行命令:

time pt-online-schema-change –host=192.168.121.91 –port=3307 –user=tim –password=”timisgood” –alter=”change C_N C_N_01 varchar(128) default 11” –execute D=test,t=UC_USER –set-vars innodb_lock_wait_timeout=50 –no-check-replication-filters –no-check-alter

执行过程:

 [root@hch_test_121_91 ~]# time pt-online-schema-change --host=192.168.121.91 --port=3307 --user=tim --password="timisgood" --alter="change C_N C_N_01 varchar(128) default 11" --execute D=test,t=UC_USER --set-vars innodb_lock_wait_timeout=50 --no-check-replication-filters --no-check-alter
No slaves found.  See --recursion-method if host hch_test_121_91 has slaves.
Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
Operation, tries, wait:
  analyze_table, 10, 1
  copy_rows, 10, 0.25
  create_triggers, 10, 1
  drop_triggers, 10, 1
  swap_tables, 10, 1
  update_foreign_keys, 10, 1
Altering `test`.`UC_USER`...
Renaming columns:
  C_N to C_N_01
Creating new table...
Created new table test._UC_USER_new OK.
Altering new table...
Altered `test`.`_UC_USER_new` OK.
2017-07-05T19:15:03 Creating triggers...
2017-07-05T19:15:03 Created triggers OK.
2017-07-05T19:15:03 Copying approximately 1457112 rows...
Copying `test`.`UC_USER`:  20% 01:57 remain
Copying `test`.`UC_USER`:  40% 01:27 remain
Copying `test`.`UC_USER`:  60% 00:59 remain
Copying `test`.`UC_USER`:  79% 00:30 remain
Copying `test`.`UC_USER`:  98% 00:02 remain
2017-07-05T19:17:38 Copied rows OK.
2017-07-05T19:17:38 Analyzing new table...
2017-07-05T19:17:38 Swapping tables...
2017-07-05T19:17:38 Swapped original and new tables OK.
2017-07-05T19:17:38 Dropping old table...
2017-07-05T19:17:39 Dropped old table `test`.`_UC_USER_old` OK.
2017-07-05T19:17:39 Dropping triggers...
2017-07-05T19:17:39 Dropped triggers OK.
Successfully altered `test`.`UC_USER`.

real    2m36.045s
user    0m1.000s
sys 0m0.158s
[root@hch_test_121_91 ~]#

2.4 删除字段

执行命令:

time pt-online-schema-change –host=192.168.121.91 –port=3307 –user=tim –password=”timisgood” –alter=”drop column C_N_01” –execute D=test,t=UC_USER –set-vars innodb_lock_wait_timeout=50 –no-check-replication-filters –no-check-alter

执行过程:

[root@rac1 bin]# ./pt-online-schema-change  -uroot  -pxxx  --alter='drop  column  address ' --execute D=test,t=t_xxx_compensate
[root@hch_test_121_91 ~]# time pt-online-schema-change --host=192.168.121.91 --port=3307 --user=tim --password="timisgood" --alter="drop column C_N_01" --execute D=test,t=UC_USER --set-vars innodb_lock_wait_timeout=50 --no-check-replication-filters --no-check-alter
No slaves found.  See --recursion-method if host hch_test_121_91 has slaves.
Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
Operation, tries, wait:
  analyze_table, 10, 1
  copy_rows, 10, 0.25
  create_triggers, 10, 1
  drop_triggers, 10, 1
  swap_tables, 10, 1
  update_foreign_keys, 10, 1
Altering `test`.`UC_USER`...
Creating new table...
Created new table test._UC_USER_new OK.
Altering new table...
Altered `test`.`_UC_USER_new` OK.
2017-07-06T10:47:33 Creating triggers...
2017-07-06T10:47:33 Created triggers OK.
2017-07-06T10:47:33 Copying approximately 1403100 rows...
Copying `test`.`UC_USER`:  21% 01:47 remain
Copying `test`.`UC_USER`:  39% 01:31 remain
Copying `test`.`UC_USER`:  61% 00:56 remain
Copying `test`.`UC_USER`:  82% 00:26 remain
2017-07-06T10:50:13 Copied rows OK.
2017-07-06T10:50:13 Analyzing new table...
2017-07-06T10:50:13 Swapping tables...
2017-07-06T10:50:13 Swapped original and new tables OK.
2017-07-06T10:50:13 Dropping old table...
2017-07-06T10:50:14 Dropped old table `test`.`_UC_USER_old` OK.
2017-07-06T10:50:14 Dropping triggers...
2017-07-06T10:50:14 Dropped triggers OK.
Successfully altered `test`.`UC_USER`.

real    2m41.533s
user    0m1.013s
sys 0m0.196s
[root@hch_test_121_91 ~]#

2.5 添加索引

执行命令:

time pt-online-schema-change –host=192.168.121.91 –port=3307 –user=tim –password=”timisgood” –alter=”add index IDX_MOBILE(MOBILE)” –execute D=test,t=UC_USER –set-vars innodb_lock_wait_timeout=50 –no-check-replication-filters –no-check-alter

执行过程:

[root@hch_test_121_91 ~]# time pt-online-schema-change --host=192.168.121.91 --port=3307 --user=tim --password="timisgood" --alter="add index IDX_MOBILE(MOBILE)" --execute D=test,t=UC_USER --set-vars innodb_lock_wait_timeout=50 --no-check-replication-filters --no-check-alter
No slaves found.  See --recursion-method if host hch_test_121_91 has slaves.
Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
Operation, tries, wait:
  analyze_table, 10, 1
  copy_rows, 10, 0.25
  create_triggers, 10, 1
  drop_triggers, 10, 1
  swap_tables, 10, 1
  update_foreign_keys, 10, 1
Altering `test`.`UC_USER`...
Creating new table...
Created new table test._UC_USER_new OK.
Altering new table...
Altered `test`.`_UC_USER_new` OK.
2017-07-06T10:52:51 Creating triggers...
2017-07-06T10:52:51 Created triggers OK.
2017-07-06T10:52:51 Copying approximately 1457112 rows...
Copying `test`.`UC_USER`:  20% 01:58 remain
Copying `test`.`UC_USER`:  37% 01:39 remain
Copying `test`.`UC_USER`:  54% 01:16 remain
Copying `test`.`UC_USER`:  71% 00:47 remain
Copying `test`.`UC_USER`:  87% 00:21 remain
2017-07-06T10:55:48 Copied rows OK.
2017-07-06T10:55:48 Analyzing new table...
2017-07-06T10:55:48 Swapping tables...
2017-07-06T10:55:48 Swapped original and new tables OK.
2017-07-06T10:55:48 Dropping old table...
2017-07-06T10:55:49 Dropped old table `test`.`_UC_USER_old` OK.
2017-07-06T10:55:49 Dropping triggers...
2017-07-06T10:55:49 Dropped triggers OK.
Successfully altered `test`.`UC_USER`.

real    2m58.237s
user    0m1.092s
sys 0m0.184s
[root@hch_test_121_91 ~]#

2.6 删除索引

执行命令:

time pt-online-schema-change –host=192.168.121.91 –port=3307 –user=tim –password=”timisgood” –alter=”drop index IDX_MOBILE” –execute D=test,t=UC_USER –set-vars innodb_lock_wait_timeout=50 –no-check-replication-filters –no-check-alter

执行过程:

[root@rac1 bin]# ./pt-online-schema-change -uroot  -pxxx --alter='DROP INDEX indx_test' --execute D=test,t=t_xxx_compensate
[root@hch_test_121_91 ~]# time pt-online-schema-change --host=192.168.121.91 --port=3307 --user=tim --password="timisgood" --alter="drop index IDX_MOBILE" --execute D=test,t=UC_USER --set-vars innodb_lock_wait_timeout=50 --no-check-replication-filters --no-check-alter
No slaves found.  See --recursion-method if host hch_test_121_91 has slaves.
Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
Operation, tries, wait:
  analyze_table, 10, 1
  copy_rows, 10, 0.25
  create_triggers, 10, 1
  drop_triggers, 10, 1
  swap_tables, 10, 1
  update_foreign_keys, 10, 1
Altering `test`.`UC_USER`...
Creating new table...
Created new table test._UC_USER_new OK.
Altering new table...
Altered `test`.`_UC_USER_new` OK.
2017-07-06T10:59:15 Creating triggers...
2017-07-06T10:59:15 Created triggers OK.
2017-07-06T10:59:15 Copying approximately 1457112 rows...
Copying `test`.`UC_USER`:  24% 01:30 remain
Copying `test`.`UC_USER`:  42% 01:20 remain
Copying `test`.`UC_USER`:  60% 00:58 remain
Copying `test`.`UC_USER`:  79% 00:31 remain
2017-07-06T11:01:46 Copied rows OK.
2017-07-06T11:01:46 Analyzing new table...
2017-07-06T11:01:46 Swapping tables...
2017-07-06T11:01:46 Swapped original and new tables OK.
2017-07-06T11:01:46 Dropping old table...
2017-07-06T11:01:47 Dropped old table `test`.`_UC_USER_old` OK.
2017-07-06T11:01:47 Dropping triggers...
2017-07-06T11:01:47 Dropped triggers OK.
Successfully altered `test`.`UC_USER`.

real    2m32.376s
user    0m0.944s
sys 0m0.169s
[root@hch_test_121_91 ~]#

3,总结:

在没有执行sql语句时,mysql在线修改表结构的时间与pt-online-schma-change的基本上相等,无大的差别;pt-online-schma-change方式会占用较多内存,负载也会略高

在mysql预热以后,在线修改表结构的时间,直接修改会比pt-online-schema-change方式略快

在线修改表结构的同时执行mysql语句,mysql直接修改的方式会先修改完表结构再执行sql语句,pt-online-schema-change方式会优先执行sql语句,再复制数据表,复制完毕后再把执行sql语句的结果更新到新表,因此,在时间上,直接修改表结构会比pt-online-schema-change方式,在实际用时上,直接修改会略快

参考文章:https://www.percona.com/doc/percona-toolkit/2.1/pt-online-schema-change.html

4,问题一:

[root@hch_test_121_91 percona-toolkit-3.0.3]# time pt-online-schema-change -uroot -p –alter=’add column C_N varchar(64)’ –execute D=test,t=UC_USER

Cannot connect to MySQL: install_driver(mysql) failed: Attempt to reload DBD/mysql.pm aborted.

Compilation failed in require at (eval 13) line 3.

at /usr/local/bin/pt-online-schema-change line 2296

real 0m0.336s

user 0m0.314s

sys 0m0.021s

[root@hch_test_121_91 percona-toolkit-3.0.3]#

解决办法是移走/usr/local/lib64/perl5的目录:

[root@hch_test_121_91 /]# find / -name perl5

/data/backup/perl5

/usr/local/lib64/perl5

/usr/lib64/perl5

/usr/share/swig/1.3.40/perl5

/usr/share/perl5

[root@hch_test_121_91 /]# mv /usr/local/lib64/perl5 /usr/local/lib64/perl5_bak

[root@hch_test_121_91 /]#

5,问题二

[root@hch_test_121_91 /]# time pt-online-schema-change --user=root --alter="add column C_N varchar(64)" --execute D=test,t=UC_USER
Error setting innodb_lock_wait_timeout: DBD::mysql::db do failed: Variable 'innodb_lock_wait_timeout' is a read only variable [for Statement "SET SESSION innodb_lock_wait_timeout=1"].  The current value for innodb_lock_wait_timeout is 50.  If the variable is read only (not dynamic), specify --set-vars innodb_lock_wait_timeout=50 to avoid this warning, else manually set the variable and restart MySQL.

Error setting innodb_lock_wait_timeout: DBD::mysql::db do failed: Variable 'innodb_lock_wait_timeout' is a read only variable [for Statement "SET SESSION innodb_lock_wait_timeout=1"].  The current value for innodb_lock_wait_timeout is 50.  If the variable is read only (not dynamic), specify --set-vars innodb_lock_wait_timeout=50 to avoid this warning, else manually set the variable and restart MySQL.

No slaves found.  See --recursion-method if host hch_test_121_91 has slaves.
Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
The original table `test`.`UC_USER` does not exist.

real    0m0.362s
user    0m0.321s
sys 0m0.033s
[root@hch_test_121_91 /]# 

解决办法:添加--set-vars innodb_lock_wait_timeout=50 参数

6,问题三

问题:

[root@hch_test_121_91 /]# time pt-online-schema-change –host=192.168.121.91 –port=3307 –user=root –alter=”add column C_N varchar(64)” –execute D=test,t=UC_USER –set-vars innodb_lock_wait_timeout=50 –no-check-replication-filters

Cannot connect to MySQL: DBI connect(‘test;host=192.168.121.91;port=3307;mysql_read_default_group=client’,’root’,…) failed: Access denied for user ‘root’@’192.168.121.91’ (using password: NO) at /usr/local/bin/pt-online-schema-change line 2296

real    0m0.358s
user    0m0.312s
sys 0m0.039s

解决办法,用户密码不能为null值

[root@hch_test_121_91 /]# time pt-online-schema-change --host=192.168.121.91 --port=3307 --user=tim --password="timisgood" --alter="add column C_N varchar(64)" --execute D=test,t=UC_USER --set-vars innodb_lock_wait_timeout=50 --no-check-replication-filters
No slaves found.  See --recursion-method if host hch_test_121_91 has slaves.
Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
Operation, tries, wait:
  analyze_table, 10, 1
  copy_rows, 10, 0.25
  create_triggers, 10, 1
  drop_triggers, 10, 1
  swap_tables, 10, 1
  update_foreign_keys, 10, 1
Altering `test`.`UC_USER`...
Creating new table...
Created new table test._UC_USER_new OK.
Altering new table...
Altered `test`.`_UC_USER_new` OK.
2017-07-05T15:18:46 Creating triggers...
2017-07-05T15:18:46 Created triggers OK.
2017-07-05T15:18:46 Copying approximately 1381835 rows...
Copying `test`.`UC_USER`:  20% 01:56 remain
Copying `test`.`UC_USER`:  41% 01:23 remain
Copying `test`.`UC_USER`:  61% 00:56 remain
Copying `test`.`UC_USER`:  82% 00:24 remain
2017-07-05T15:21:22 Copied rows OK.
2017-07-05T15:21:22 Analyzing new table...
2017-07-05T15:21:22 Swapping tables...
2017-07-05T15:21:22 Swapped original and new tables OK.
2017-07-05T15:21:22 Dropping old table...
2017-07-05T15:21:23 Dropped old table `test`.`_UC_USER_old` OK.
2017-07-05T15:21:23 Dropping triggers...
2017-07-05T15:21:23 Dropped triggers OK.
Successfully altered `test`.`UC_USER`.

real    2m42.191s
user    0m0.978s
sys 0m0.156s
[root@hch_test_121_91 /]#

7,验证不影响在线DML操作

自己单独执行alter操作

mysql> alter table test.UC_USER add column C_N_2 varchar(64);
Query OK, 0 rows affected (2 min 8.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql>

再次执行操作,看是否能在线ddl

(1)添加字段操作

[root@hch_test_121_91 /]# time pt-online-schema-change --host=192.168.121.91 --port=3307 --user=tim --password="timisgood" --alter="add column C_N_3 varchar(64) not null default 'sky'" --execute D=test,t=UC_USER --set-vars innodb_lock_wait_timeout=50 --no-check-replication-filters
No slaves found.  See --recursion-method if host hch_test_121_91 has slaves.
Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
Operation, tries, wait:
  analyze_table, 10, 1
  copy_rows, 10, 0.25
  create_triggers, 10, 1
  drop_triggers, 10, 1
  swap_tables, 10, 1
  update_foreign_keys, 10, 1
Altering `test`.`UC_USER`...
Creating new table...
Created new table test._UC_USER_new OK.
Altering new table...
Altered `test`.`_UC_USER_new` OK.
2017-07-05T15:30:32 Creating triggers...
2017-07-05T15:30:32 Created triggers OK.
2017-07-05T15:30:32 Copying approximately 1425328 rows...
Copying `test`.`UC_USER`:  20% 01:59 remain
Copying `test`.`UC_USER`:  39% 01:31 remain
Copying `test`.`UC_USER`:  61% 00:57 remain
Copying `test`.`UC_USER`:  87% 00:16 remain
2017-07-05T15:32:49 Copied rows OK.
2017-07-05T15:32:49 Analyzing new table...
2017-07-05T15:32:49 Swapping tables...
2017-07-05T15:32:49 Swapped original and new tables OK.
2017-07-05T15:32:49 Dropping old table...
2017-07-05T15:32:49 Dropped old table `test`.`_UC_USER_old` OK.
2017-07-05T15:32:49 Dropping triggers...
2017-07-05T15:32:49 Dropped triggers OK.
Successfully altered `test`.`UC_USER`.

real    2m18.175s
user    0m0.758s
sys 0m0.136s
[root@hch_test_121_91 /]#

(2)同时进行update操作

mysql> update test.UC_USER set UPDATE_DATE=now(),NAME=concat(NAME,1) where ID=300;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select ID,UPDATE_DATE,NAME from test.UC_USER  where ID=300;
+-----+---------------------+------------+
| ID  | UPDATE_DATE | NAME   |
+-----+---------------------+------------+
| 300 | 2017-07-05 15:33:05 | 刘崇武1|
+-----+---------------------+------------+
1 row in set (0.00 sec)

mysql>
mysql> select ID,UPDATE_DATE,NAME,C_N_3 from test.UC_USER  where ID=300;
+-----+---------------------+------------+-------+
| ID  | UPDATE_DATE | NAME   | C_N_3 |
+-----+---------------------+------------+-------+
| 300 | 2017-07-05 15:33:05 | 刘崇武1| sky   |
+-----+---------------------+------------+-------+
1 row in set (0.00 sec)

mysql>

总结:

(1)不适合跨网络操作,适合本地操作

(2)上千万记录表或者上百万记录含大字段的表,使用pt比直接alter慢很多。

(3)pt在业务高峰期不适合使用,在高并发情景下也不太适合使用。

8,补充pt组件涉及到的三个触发器:

        DELIMITER $$

        USE `test`$$

        DROP TRIGGER /*!50032 IF EXISTS */ `pt_osc_test_UC_USER_del`$$

        CREATE
            /*!50017 DEFINER = 'tim'@'192.168.%' */
            TRIGGER `pt_osc_test_UC_USER_del` AFTER DELETE ON `UC_USER`
            FOR EACH ROW DELETE IGNORE FROM `test`.`_UC_USER_new` WHERE `test`.`_UC_USER_new`.`id` <=> OLD.`id`;
        $$

        DELIMITER ;

        -----------------------------------------------------------------------------------------------------------
        DELIMITER $$

        USE `test`$$

        DROP TRIGGER /*!50032 IF EXISTS */ `pt_osc_test_UC_USER_upd`$$

        CREATE
            /*!50017 DEFINER = 'tim'@'192.168.%' */
            TRIGGER `pt_osc_test_UC_USER_upd` AFTER UPDATE ON `UC_USER`
            FOR EACH ROW BEGIN DELETE IGNORE FROM `test`.`_UC_USER_new` WHERE !(OLD.`id` <=> NEW.`id`) AND `test`.`_UC_USER_new`.`id` <=> OLD.`id`;REPLACE INTO `test`.`_UC_USER_new` (`id`, `user_name`, `user_pwd`, `birthday`, `name`, `user_icon`, `sex`, `nickname`, `stat`, `user_mall`, `last_login_date`, `last_login_ip`, `src_open_user_id`, `email`, `mobile`, `is_del`, `is_email_confirmed`, `is_phone_confirmed`, `creater`, `create_date`, `update_date`, `pwd_intensity`, `mobile_tgc`, `mac`, `source`, `activate`, `activate_type`, `is_life`, `reserve_create_date`, `c_n_2`, `c_n_3`) VALUES (NEW.`id`, NEW.`user_name`, NEW.`user_pwd`, NEW.`birthday`, NEW.`name`, NEW.`user_icon`, NEW.`sex`, NEW.`nickname`, NEW.`stat`, NEW.`user_mall`, NEW.`last_login_date`, NEW.`last_login_ip`, NEW.`src_open_user_id`, NEW.`email`, NEW.`mobile`, NEW.`is_del`, NEW.`is_email_confirmed`, NEW.`is_phone_confirmed`, NEW.`creater`, NEW.`create_date`, NEW.`update_date`, NEW.`pwd_intensity`, NEW.`mobile_tgc`, NEW.`mac`, NEW.`source`, NEW.`activate`, NEW.`activate_type`, NEW.`is_life`, NEW.`reserve_create_date`, NEW.`c_n_2`, NEW.`c_n_3`);END;
        $$

        DELIMITER ;

        -----------------------------------------------------------------------------------------------------------
        DELIMITER $$

        USE `test`$$

        DROP TRIGGER /*!50032 IF EXISTS */ `pt_osc_test_UC_USER_ins`$$

        CREATE
            /*!50017 DEFINER = 'tim'@'192.168.%' */
            TRIGGER `pt_osc_test_UC_USER_ins` AFTER INSERT ON `UC_USER`
            FOR EACH ROW REPLACE INTO `test`.`_UC_USER_new` (`id`, `user_name`, `user_pwd`, `birthday`, `name`, `user_icon`, `sex`, `nickname`, `stat`, `user_mall`, `last_login_date`, `last_login_ip`, `src_open_user_id`, `email`, `mobile`, `is_del`, `is_email_confirmed`, `is_phone_confirmed`, `creater`, `create_date`, `update_date`, `pwd_intensity`, `mobile_tgc`, `mac`, `source`, `activate`, `activate_type`, `is_life`, `reserve_create_date`, `c_n_2`, `c_n_3`) VALUES (NEW.`id`, NEW.`user_name`, NEW.`user_pwd`, NEW.`birthday`, NEW.`name`, NEW.`user_icon`, NEW.`sex`, NEW.`nickname`, NEW.`stat`, NEW.`user_mall`, NEW.`last_login_date`, NEW.`last_login_ip`, NEW.`src_open_user_id`, NEW.`email`, NEW.`mobile`, NEW.`is_del`, NEW.`is_email_confirmed`, NEW.`is_phone_confirmed`, NEW.`creater`, NEW.`create_date`, NEW.`update_date`, NEW.`pwd_intensity`, NEW.`mobile_tgc`, NEW.`mac`, NEW.`source`, NEW.`activate`, NEW.`activate_type`, NEW.`is_life`, NEW.`reserve_create_date`, NEW.`c_n_2`, NEW.`c_n_3`);
        $$

        DELIMITER ;

MySQL 大表在线DML神器--pt-online-schema-change的更多相关文章

  1. [记录]一则清理MySQL大表以释放磁盘空间的案例

    一则清理MySQL大表以释放磁盘空间的案例 一.基本情况: 1.dbtest库554G,先清理st_online_time_away_ds(37G)表的数据,保留半年的数据: 1)删除的数据:sele ...

  2. 优秀后端架构师必会知识:史上最全MySQL大表优化方案总结

    本文原作者“ manong”,原创发表于segmentfault,原文链接:segmentfault.com/a/1190000006158186 1.引言   MySQL作为开源技术的代表作之一,是 ...

  3. 从云数据迁移服务看MySQL大表抽取模式

    摘要:MySQL JDBC抽取到底应该采用什么样的方式,且听小编给你娓娓道来. 小编最近在云上的一个迁移项目中被MySQL抽取模式折磨的很惨.一开始爆内存被客户怼,再后来迁移效率低下再被怼.MySQL ...

  4. MySQL 大表优化方案(长文)

    当MySQL单表记录数过大时,增删改查性能都会急剧下降,可以参考以下步骤来优化: 单表优化 除非单表数据未来会一直不断上涨,否则不要一开始就考虑拆分,拆分会带来逻辑.部署.运维的各种复杂度,一般以整型 ...

  5. 在线修改MySQL大表的表结构

    由于某个临时需求,需要给在线MySQL的某个超过千万的表增加一个字段.此表在设计之时完全按照需求实现,并没有多余的保留字段. 我们知道在MySQL中如果要执行ALTER TABLE操作,MySQL会通 ...

  6. mysql大表在不停机的情况下增加字段该怎么处理

    MySQL中给一张千万甚至更大量级的表添加字段一直是比较头疼的问题,遇到此情况通常该如果处理?本文通过常见的三种场景进行案例说明. 1. 环境准备 数据库版本: 5.7.25-28(Percona 分 ...

  7. mysql 大表拆分成csv导出

    最近公司有一个几千万行的大表需要按照城市的id字段拆分成不同的csv文件. 写了一个自动化的shell脚本 在/home/hdh 下面 linux-xud0:/home/hdh # lltotal 1 ...

  8. 详解MySQL大表优化方案( 转)

    当MySQL单表记录数过大时,增删改查性能都会急剧下降,可以参考以下步骤来优化: 单表优化 除非单表数据未来会一直不断上涨,否则不要一开始就考虑拆分,拆分会带来逻辑.部署.运维的各种复杂度,一般以整型 ...

  9. MySQL 大表优化方案探讨

    当MySQL单表记录数过大时,增删改查性能都会急剧下降,可以参考以下步骤来优化: 单表优化 除非单表数据未来会一直不断上涨,否则不要一开始就考虑拆分,拆分会带来逻辑.部署.运维的各种复杂度,一般以整型 ...

随机推荐

  1. Jquery2 基础核心

    学习要点: 1.代码风格 2.加载模式 3.对象互换 4.多个库之间的冲突 本节简单的介绍一下jQuery 一些核心的问题. 一.代码风格 在jQuery程序中,不管是页面元素的选择.内置的功能函数, ...

  2. centos7更改主机名

    操作环境 [root@centos701 ~]# uname Linux [root@centos701 ~]# uname -a Linux centos701 3.10.0-693.el7.x86 ...

  3. LeetCode——Unique Binary Search Trees II

    Question Given an integer n, generate all structurally unique BST's (binary search trees) that store ...

  4. No module named _sqlite3 django python manage.py runserver

    linux 执行django(python manage.py runserver),报错No module named _sqlite3,需要安装sqlite-devel,再重新编译安装python ...

  5. java -jar 启动jar包 带参数

    运行jar包时指定端口:java -jar xxx.jar --server.port=8088 server.port=8081 若命令行传入的server.port没有作用,服务仍然使用8081端 ...

  6. angularjs1 自定义图片查看器(可旋转、放大、缩小、拖拽)

    笔记: angularjs1 制作自定义图片查看器(可旋转.放大.缩小.拖拽) 2018-01-12 更新  可以在我的博客  查看我 已经封装好的 纯 js写的图片查看器插件    博客链接 懒得把 ...

  7. tensorflow入门(二)

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt #使用numpy生成200个随机点 x_data ...

  8. Jdev 本地RUN页面时候,将异常直接显示出来,而不是乱码

    本地运行页面时,经常会遇到以下错误 oracle.jbo.JboException: JBO-29000: JBO-29000: JBO-26028: ???? MemberAttributesDis ...

  9. wordcount程序出现map 100% reduce 0%问题的解决方法

    运行wordcount程序一直停在map 100% reduce 0%, input文件夹的内容: 其中: f1.txt中的内容为:hello hadoop f2.txt中的内容为:hello had ...

  10. 011PHP基础知识——运算符(四)

    <?php /** * 连接运算符: . 连接2个参数生成新的字符串: */ /*$str="中国"; $bbs="bbs.blog.com"; $new ...