pt-online-schema-change
名字:pt-online-schema-change - ALTER tables without locking them. 在线改表
下载地址:
https://www.percona.com/downloads/percona-toolkit/3.0.12/binary/redhat/6/x86_64/percona-toolkit-3.0.12-1.el6.x86_64.rpm
https://www.percona.com/doc/percona-toolkit/LATEST/pt-online-schema-change.html#downloading
官方文档:https://www.percona.com/doc/percona-toolkit/LATEST/pt-online-schema-change.html
1.首先了解一下pt-online-schema-change工作原理,其实原理很简单
-
1、如果存在外键,根据alter-foreign-keys-method参数的值,检测外键相关的表,做相应设置的处理。没有使用
-
2、创建一个新的表,表结构为修改后的数据表,用于从源数据表向新表中导入数据。
-
3、创建触发器,用于记录从拷贝数据开始之后,对源数据表继续进行数据修改的操作记录下来,用于数据拷贝结束后,执行这些操作,保证数据不会丢失。如果表中已经定义了触发器这个工具就不能工作了。
-
-
5、修改外键相关的子表,根据修改后的数据,修改外键关联的子表。
-
6、rename源数据表为old表,把新表rename为源表名,并将old表删除。
-
2.使用方法:
-
pt-online-schema-change [OPTIONS] DSN
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
结构变更语句,不需要alter table关键字。可以指定多个更改,用逗号分隔。如下场景,需要注意:
-
-
列不能通过先删除,再添加的方式进行重命名,不会将数据拷贝到新列。
-
如果加入的列非空而且没有默认值,则工具会失败。即其不会为你设置一个默认值,必须显示指定。
-
删除外键(drop foreign key constrain_name)时,需要指定名称_constraint_name,而不是原始的constraint_name。
-
如:CONSTRAINT `fk_foo` FOREIGN KEY (`foo_id`) REFERENCES `bar` (`foo_id`),需要指定:
-
-
-
如何把外键引用到新表?需要特殊处理带有外键约束的表,以保证它们可以应用到新表.当重命名表的时候,外键关系会带到重命名后的表上。
-
该工具有两种方法,可以自动找到子表,并修改约束关系。
-
auto: 在rebuild_constraints和drop_swap两种处理方式中选择一个。
-
rebuild_constraints:使用 ALTER TABLE语句先删除外键约束,然后再添加.如果子表很大的话,会导致长时间的阻塞。
-
drop_swap: 执行FOREIGN_KEY_CHECKS=0,禁止外键约束,删除原表,再重命名新表。这种方式很快,也不会产生阻塞,但是有风险:
-
1, 在删除原表和重命名新表的短时间内,表是不存在的,程序会返回错误。
-
2, 如果重命名表出现错误,也不能回滚了.因为原表已经被删除。
-
none: 类似"drop_swap"的处理方式,但是它不删除原表,并且外键关系会随着重命名转到老表上面。
-
-
-
-
-
-
默认1s。每个chunk拷贝完成后,会查看所有复制Slave的延迟情况。要是延迟大于该值,则暂停复制数据,直到所有从的滞后小于这个值,使用Seconds_Behind_Master。如果有任何从滞后超过此选项的值,则该工具将睡眠
-
-
-
-
-
-
默认是show processlist,发现从的方法,也可以是host,但需要在从上指定report_host,通过show slave hosts来找到,可以指定none来不检查Slave。
-
-
-
=========== ==================
-
processlist SHOW PROCESSLIST
-
-
dsn=DSN DSNs from a table
-
-
-
-
-
-
-
-
-
-
-
默认yes。如果工具检测到服务器选项中有任何复制相关的筛选,如指定binlog_ignore_db和replicate_do_db此类。发现有这样的筛选,工具会报错且退出。因为如果更新的表Master上存在,而Slave上不存在,会导致复制的失败。使用–no-check-replication-filters选项来禁用该检查。
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
默认yes。复制数据完成重命名之后,删除原表。如果有错误则会保留原表。
-
-
-
默认为Threads_running=25。每个chunk拷贝完后,会检查SHOW GLOBAL STATUS的内容,检查指标(thread)是否超过了指定的阈值。如果超过,则先暂停。这里可以用逗号分隔,指定多个条件,每个条件格式:status指标=MAX_VALUE或者status指标:MAX_VALUE。如果不指定MAX_VALUE,那么工具会指定其为当前值的120%。
-
-
-
默认为Threads_running=50。用法基本与
-
-
-
默认情况下,新的表与原始表是相同的存储引擎,所以如果原来的表使用InnoDB的,那么新表将使用InnoDB的。在涉及复制某些情况下,很可能主从的存储引擎不一样。使用该选项会默认使用默认的存储引擎。
-
-
-
设置MySQL变量,多个用逗号分割。默认该工具设置的是: wait_timeout=10000 innodb_lock_wait_timeout=1 lock_wait_timeout=60
-
-
-
当需要复制的块远大于设置的chunk-size大小,就不复制.默认值是4.0,一个没有主键或唯一索引的表,块大小就是不确定的。
-
-
-
在chunk-time执行的时间内,动态调整chunk-size的大小,以适应服务器性能的变化,该参数设置为0,或者指定chunk-size,都可以禁止动态调整。
-
-
-
指定块的大小,默认是1000行,可以添加k,M,G后缀.这个块的大小要尽量与
-
-
-
默认yes。为了安全,检查查询的执行计划.默认情况下,这个工具在执行查询之前会先EXPLAIN,以获取一次少量的数据,如果是不好的EXPLAIN,那么会获取一次大量的数据,这个工具会多次执行EXPALIN,如果EXPLAIN不同的结果,那么就会认为这个查询是不安全的。
-
-
-
打印出内部事件的数目,可以看到复制数据插入的数目。
-
-
-
创建和修改新表,但不会创建触发器、复制数据、和替换原表。并不真正执行,可以看到生成的执行语句,了解其执行步骤与细节。
-
-
-
-
-
-
打印SQL语句到标准输出。指定此选项可以让你看到该工具所执行的语句,和
-
-
-
复制数据的时候打印进度报告,二部分组成:第一部分是百分比,第二部分是时间。
-
-
-
3.安装
-
下载最新的pt-online-schema-change
-
-
-
-
warning: percona-toolkit-3.0.12-1.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY
-
error: Failed dependencies:
-
perl(DBI) >= 1.13 is needed by percona-toolkit-3.0.12-1.el6.x86_64
-
perl(DBD::mysql) >= 1.0 is needed by percona-toolkit-3.0.12-1.el6.x86_64
-
perl(Time::HiRes) is needed by percona-toolkit-3.0.12-1.el6.x86_64
-
perl(IO::Socket::SSL) is needed by percona-toolkit-3.0.12-1.el6.x86_64
-
perl(Term::ReadKey) is needed by percona-toolkit-3.0.12-1.el6.x86_64
-
-
-
-
-
-
-
-
-
-
warning: percona-toolkit-3.0.12-1.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID cd2efd2a: NOKEY
-
-
-
至此软件已经安装好,下面就通过测试来了解如何使用该工具
创建测试数据
-
-
-
-
-
-
-
-
-
-
-
-
mysql> create database test;
-
Query OK, 1 row affected (0.00 sec)
-
-
-
-
mysql> CREATE TABLE users (id int(11) NOT NULL AUTO_INCREMENT COMMENT ‘用户主键‘,name varchar(20) DEFAULT ‘‘ COMMENT ‘用户名‘,PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=‘用户表‘;
-
Query OK, 0 rows affected (0.04 sec)
-
mysql> insert into users(name) values(‘test1‘),(‘test2‘),(‘test3‘);
-
Query OK, 3 rows affected (0.01 sec)
-
Records: 3 Duplicates: 0 Warnings: 0
-
-
-
-
-
-
-
-
-
mysql> select count(*) from users;
-
-
-
-
-
-
在正式使用之前,再次了解一下常用的几个参数:
-
-
-
-
-
-
-
-
测试各种不同的场景
1.整理表碎片
-
-
sql语句:alter table table_name engine=innodb
-
-
(1)pt-online-schema-change
-
(2)pt-online-schema-change
-
-
-
-
Not checking slave lag because no slaves were found and
-
-
-
-
-
-
-
update_foreign_keys, 10, 1
-
Altering `test`.`users`...
-
-
Created new table test._users_new OK.
-
-
Altered `test`.`_users_new` OK.
-
2018-12-06T13:28:05 Creating triggers...
-
2018-12-06T13:28:05 Created triggers OK.
-
2018-12-06T13:28:05 Copying approximately 3 rows...
-
2018-12-06T13:28:05 Copied rows OK.
-
2018-12-06T13:28:05 Analyzing new table...
-
2018-12-06T13:28:05 Swapping tables...
-
2018-12-06T13:28:05 Swapped original and new tables OK.
-
2018-12-06T13:28:05 Dropping old table...
-
2018-12-06T13:28:05 Dropped old table `test`.`_users_old` OK.
-
2018-12-06T13:28:05 Dropping triggers...
-
2018-12-06T13:28:05 Dropped triggers OK.
-
Successfully altered `test`.`users`.
2.添加字段:
-
-
alter table users add age varchar(10) NOT NUll DEFAULT ‘‘ COMMENT ‘性别‘;
-
pt语句: pt-online-schema-change
-
-
-
-
Not checking slave lag because no slaves were found and
-
-
-
-
-
-
-
update_foreign_keys, 10, 1
-
Altering `test`.`users`...
-
-
CREATE TABLE `test`.`_users_new` (
-
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT ‘用户主键‘,
-
`name` varchar(20) DEFAULT ‘‘ COMMENT ‘用户名‘,
-
-
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT=‘用户表‘
-
Created new table test._users_new OK.
-
-
ALTER TABLE `test`.`_users_new` add age varchar(10) NOT NUll DEFAULT ‘‘ COMMENT ‘æ?§å?«‘ ;
-
Altered `test`.`_users_new` OK.
-
2018-12-06T13:36:55 Creating triggers...
-
2018-12-06T13:36:55 Created triggers OK.
-
2018-12-06T13:36:55 Copying approximately 3 rows...
-
INSERT LOW_PRIORITY IGNORE INTO `test`.`_users_new` (`id`, `name`) SELECT `id`, `name` FROM `test`.`users` LOCK IN SHARE MODE
-
2018-12-06T13:36:55 Copied rows OK.
-
2018-12-06T13:36:55 Analyzing new table...
-
2018-12-06T13:36:55 Swapping tables...
-
RENAME TABLE `test`.`users` TO `test`.`_users_old`, `test`.`_users_new` TO `test`.`users`
-
2018-12-06T13:36:55 Swapped original and new tables OK.
-
2018-12-06T13:36:55 Dropping old table...
-
DROP TABLE IF EXISTS `test`.`_users_old`
-
2018-12-06T13:36:55 Dropped old table `test`.`_users_old` OK.
-
2018-12-06T13:36:55 Dropping triggers...
-
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_del`
-
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_upd`
-
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_ins`
-
2018-12-06T13:36:55 Dropped triggers OK.
-
Successfully altered `test`.`users`.
-
-
sql语句:alter table users add column address varchar(100) default ‘‘ comment=‘地址‘ after age,add column telephone int default 0 comment=‘手机号码‘;
-
pt语句:pt-online-schema-change
-
-
-
-
Not checking slave lag because no slaves were found and
-
-
-
-
-
-
-
update_foreign_keys, 10, 1
-
Altering `test`.`users`...
-
-
CREATE TABLE `test`.`_users_new` (
-
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT ‘用户主键‘,
-
`name` varchar(20) DEFAULT ‘‘ COMMENT ‘用户名‘,
-
`age` varchar(10) NOT NULL DEFAULT ‘‘ COMMENT ‘性别‘,
-
-
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT=‘用户表‘
-
Created new table test._users_new OK.
-
-
ALTER TABLE `test`.`_users_new` add column address varchar(100) default ‘‘ comment ‘å?°å??‘ after age,add column telephone int default 0 comment ‘æ??æ?ºå?·ç ?‘ after address ;
-
Altered `test`.`_users_new` OK.
-
2018-12-06T14:02:40 Creating triggers...
-
2018-12-06T14:02:40 Created triggers OK.
-
2018-12-06T14:02:40 Copying approximately 3 rows...
-
INSERT LOW_PRIORITY IGNORE INTO `test`.`_users_new` (`id`, `name`, `age`) SELECT `id`, `name`, `age` FROM `test`.`users` LOCK IN SHARE MODE
-
2018-12-06T14:02:40 Copied rows OK.
-
2018-12-06T14:02:40 Analyzing new table...
-
2018-12-06T14:02:40 Swapping tables...
-
RENAME TABLE `test`.`users` TO `test`.`_users_old`, `test`.`_users_new` TO `test`.`users`
-
2018-12-06T14:02:40 Swapped original and new tables OK.
-
2018-12-06T14:02:40 Dropping old table...
-
DROP TABLE IF EXISTS `test`.`_users_old`
-
2018-12-06T14:02:40 Dropped old table `test`.`_users_old` OK.
-
2018-12-06T14:02:40 Dropping triggers...
-
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_del`
-
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_upd`
-
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_ins`
-
2018-12-06T14:02:40 Dropped triggers OK.
-
Successfully altered `test`.`users`.
3.修改字段
-
sql语句:alter table users modify column address varchar(200) default ‘‘ comment ‘家庭住址‘;
-
pt语句: pt-online-schema-change
-
-
-
-
Not checking slave lag because no slaves were found and
-
-
-
-
-
-
-
update_foreign_keys, 10, 1
-
Altering `test`.`users`...
-
-
CREATE TABLE `test`.`_users_new` (
-
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT ‘用户主键‘,
-
`name` varchar(20) DEFAULT ‘‘ COMMENT ‘用户名‘,
-
`age` varchar(10) NOT NULL DEFAULT ‘‘ COMMENT ‘性别‘,
-
`address` varchar(100) DEFAULT ‘‘ COMMENT ‘地址‘,
-
`telephone` int(11) DEFAULT ‘0‘ COMMENT ‘手机号码‘,
-
-
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT=‘用户表‘
-
Created new table test._users_new OK.
-
-
ALTER TABLE `test`.`_users_new` modify column address varchar(200) default ‘‘ comment ‘å®¶åºä½?å??‘;
-
Altered `test`.`_users_new` OK.
-
2018-12-06T14:08:57 Creating triggers...
-
2018-12-06T14:08:57 Created triggers OK.
-
2018-12-06T14:08:57 Copying approximately 3 rows...
-
INSERT LOW_PRIORITY IGNORE INTO `test`.`_users_new` (`id`, `name`, `age`, `address`, `telephone`) SELECT `id`, `name`, `age`, `address`, `telephone` FROM `test`.`users` LOCK IN SHARE MODE
-
2018-12-06T14:08:57 Copied rows OK.
-
2018-12-06T14:08:57 Analyzing new table...
-
2018-12-06T14:08:57 Swapping tables...
-
RENAME TABLE `test`.`users` TO `test`.`_users_old`, `test`.`_users_new` TO `test`.`users`
-
2018-12-06T14:08:57 Swapped original and new tables OK.
-
2018-12-06T14:08:57 Dropping old table...
-
DROP TABLE IF EXISTS `test`.`_users_old`
-
2018-12-06T14:08:57 Dropped old table `test`.`_users_old` OK.
-
2018-12-06T14:08:57 Dropping triggers...
-
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_del`
-
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_upd`
-
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_ins`
-
2018-12-06T14:08:57 Dropped triggers OK.
-
Successfully altered `test`.`users`.
4.添加索引
-
sql语句:alter table users add index index_age(age);
-
pt语句:pt-online-schema-change
-
-
-
-
Not checking slave lag because no slaves were found and
-
-
-
-
-
-
-
update_foreign_keys, 10, 1
-
Altering `test`.`users`...
-
-
CREATE TABLE `test`.`_users_new` (
-
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT ‘用户主键‘,
-
`name` varchar(20) DEFAULT ‘‘ COMMENT ‘用户名‘,
-
`age` varchar(10) NOT NULL DEFAULT ‘‘ COMMENT ‘性别‘,
-
`address` varchar(200) DEFAULT ‘‘ COMMENT ‘家庭住址‘,
-
`telephone` int(11) DEFAULT ‘0‘ COMMENT ‘手机号码‘,
-
-
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT=‘用户表‘
-
Created new table test._users_new OK.
-
-
ALTER TABLE `test`.`_users_new` add index index_age(age);
-
Altered `test`.`_users_new` OK.
-
2018-12-06T14:11:39 Creating triggers...
-
2018-12-06T14:11:39 Created triggers OK.
-
2018-12-06T14:11:39 Copying approximately 3 rows...
-
INSERT LOW_PRIORITY IGNORE INTO `test`.`_users_new` (`id`, `name`, `age`, `address`, `telephone`) SELECT `id`, `name`, `age`, `address`, `telephone` FROM `test`.`users` LOCK IN SHARE MODE
-
2018-12-06T14:11:39 Copied rows OK.
-
2018-12-06T14:11:39 Analyzing new table...
-
2018-12-06T14:11:39 Swapping tables...
-
RENAME TABLE `test`.`users` TO `test`.`_users_old`, `test`.`_users_new` TO `test`.`users`
-
2018-12-06T14:11:39 Swapped original and new tables OK.
-
2018-12-06T14:11:39 Dropping old table...
-
DROP TABLE IF EXISTS `test`.`_users_old`
-
2018-12-06T14:11:39 Dropped old table `test`.`_users_old` OK.
-
2018-12-06T14:11:39 Dropping triggers...
-
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_del`
-
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_upd`
-
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_ins`
-
2018-12-06T14:11:39 Dropped triggers OK.
-
Successfully altered `test`.`users`.
5.删除索引
-
sql语句:alter table users drop index index_age;
-
pt语句: pt-online-schema-change
-
-
-
-
Not checking slave lag because no slaves were found and
-
-
-
-
-
-
-
update_foreign_keys, 10, 1
-
Altering `test`.`users`...
-
-
CREATE TABLE `test`.`_users_new` (
-
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT ‘用户主键‘,
-
`name` varchar(20) DEFAULT ‘‘ COMMENT ‘用户名‘,
-
`age` varchar(10) NOT NULL DEFAULT ‘‘ COMMENT ‘性别‘,
-
`address` varchar(200) DEFAULT ‘‘ COMMENT ‘家庭住址‘,
-
`telephone` int(11) DEFAULT ‘0‘ COMMENT ‘手机号码‘,
-
-
-
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT=‘用户表‘
-
Created new table test._users_new OK.
-
-
ALTER TABLE `test`.`_users_new` drop index index_age;
-
Altered `test`.`_users_new` OK.
-
2018-12-06T14:13:25 Creating triggers...
-
2018-12-06T14:13:25 Created triggers OK.
-
2018-12-06T14:13:25 Copying approximately 3 rows...
-
INSERT LOW_PRIORITY IGNORE INTO `test`.`_users_new` (`id`, `name`, `age`, `address`, `telephone`) SELECT `id`, `name`, `age`, `address`, `telephone` FROM `test`.`users` LOCK IN SHARE MODE
-
2018-12-06T14:13:25 Copied rows OK.
-
2018-12-06T14:13:25 Analyzing new table...
-
2018-12-06T14:13:25 Swapping tables...
-
RENAME TABLE `test`.`users` TO `test`.`_users_old`, `test`.`_users_new` TO `test`.`users`
-
2018-12-06T14:13:25 Swapped original and new tables OK.
-
2018-12-06T14:13:25 Dropping old table...
-
DROP TABLE IF EXISTS `test`.`_users_old`
-
2018-12-06T14:13:25 Dropped old table `test`.`_users_old` OK.
-
2018-12-06T14:13:25 Dropping triggers...
-
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_del`
-
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_upd`
-
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_ins`
-
2018-12-06T14:13:25 Dropped triggers OK.
-
Successfully altered `test`.`users`.
6.修改自增ID
-
sql语句:alter table users modify column id bigint not null auto_increment;
-
pt语句: pt-online-schema-change
-
-
-
-
-
Not checking slave lag because no slaves were found and
-
-
-
-
-
-
-
update_foreign_keys, 10, 1
-
Altering `test`.`users`...
-
-
CREATE TABLE `test`.`_users_new` (
-
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT ‘用户主键‘,
-
`name` varchar(20) DEFAULT ‘‘ COMMENT ‘用户名‘,
-
`age` varchar(10) NOT NULL DEFAULT ‘‘ COMMENT ‘性别‘,
-
`address` varchar(200) DEFAULT ‘‘ COMMENT ‘家庭住址‘,
-
`telephone` int(11) DEFAULT ‘0‘ COMMENT ‘手机号码‘,
-
-
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT=‘用户表‘
-
Created new table test._users_new OK.
-
-
ALTER TABLE `test`.`_users_new` modify column id bigint not null auto_increment;
-
Altered `test`.`_users_new` OK.
-
2018-12-06T14:17:35 Creating triggers...
-
2018-12-06T14:17:35 Created triggers OK.
-
2018-12-06T14:17:35 Copying approximately 3 rows...
-
INSERT LOW_PRIORITY IGNORE INTO `test`.`_users_new` (`id`, `name`, `age`, `address`, `telephone`) SELECT `id`, `name`, `age`, `address`, `telephone` FROM `test`.`users` LOCK IN SHARE MODE
-
2018-12-06T14:17:35 Copied rows OK.
-
2018-12-06T14:17:35 Analyzing new table...
-
2018-12-06T14:17:35 Swapping tables...
-
RENAME TABLE `test`.`users` TO `test`.`_users_old`, `test`.`_users_new` TO `test`.`users`
-
2018-12-06T14:17:35 Swapped original and new tables OK.
-
2018-12-06T14:17:35 Dropping old table...
-
DROP TABLE IF EXISTS `test`.`_users_old`
-
2018-12-06T14:17:35 Dropped old table `test`.`_users_old` OK.
-
2018-12-06T14:17:35 Dropping triggers...
-
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_del`
-
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_upd`
-
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_ins`
-
2018-12-06T14:17:35 Dropped triggers OK.
-
Successfully altered `test`.`users`.
7.修改列名,并添加索引
-
sql语句:alter table test change column age ages varchar(5) default ‘‘ comment ‘性别新‘ after address,add index index_ages(ages);
-
pt语句:pt-online-schema-change
-
-
-
-
-
Not checking slave lag because no slaves were found and
-
-
-
-
-
-
-
update_foreign_keys, 10, 1
-
Altering `test`.`users`...
-
-
-
-
CREATE TABLE `test`.`_users_new` (
-
`id` bigint(20) NOT NULL AUTO_INCREMENT,
-
`name` varchar(20) DEFAULT ‘‘ COMMENT ‘用户名‘,
-
`age` varchar(10) NOT NULL DEFAULT ‘‘ COMMENT ‘性别‘,
-
`address` varchar(200) DEFAULT ‘‘ COMMENT ‘家庭住址‘,
-
`telephone` int(11) DEFAULT ‘0‘ COMMENT ‘手机号码‘,
-
-
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT=‘用户表‘
-
Created new table test._users_new OK.
-
-
ALTER TABLE `test`.`_users_new` change column age ages varchar(5) after address,add index index_ages(ages);
-
Altered `test`.`_users_new` OK.
-
2018-12-06T14:55:27 Creating triggers...
-
2018-12-06T14:55:27 Created triggers OK.
-
2018-12-06T14:55:27 Copying approximately 3 rows...
-
INSERT LOW_PRIORITY IGNORE INTO `test`.`_users_new` (`id`, `name`, `ages`, `address`, `telephone`) SELECT `id`, `name`, `age`, `address`, `telephone` FROM `test`.`users` LOCK IN SHARE MODE
-
2018-12-06T14:55:27 Copied rows OK.
-
2018-12-06T14:55:27 Analyzing new table...
-
2018-12-06T14:55:27 Swapping tables...
-
RENAME TABLE `test`.`users` TO `test`.`_users_old`, `test`.`_users_new` TO `test`.`users`
-
2018-12-06T14:55:27 Swapped original and new tables OK.
-
2018-12-06T14:55:27 Dropping old table...
-
DROP TABLE IF EXISTS `test`.`_users_old`
-
2018-12-06T14:55:27 Dropped old table `test`.`_users_old` OK.
-
2018-12-06T14:55:27 Dropping triggers...
-
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_del`
-
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_upd`
-
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_users_ins`
-
2018-12-06T14:55:27 Dropped triggers OK.
-
Successfully altered `test`.`users`.
上面做了这么多的操作,现在来看看测试表已经变成啥模样了
-
mysql> show create table users;
-
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
-
-
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
-
| users | CREATE TABLE `users` (
-
`id` bigint(20) NOT NULL AUTO_INCREMENT,
-
`name` varchar(20) DEFAULT ‘‘ COMMENT ‘用户名‘,
-
`address` varchar(200) DEFAULT ‘‘ COMMENT ‘家庭住址‘,
-
`ages` varchar(5) DEFAULT NULL,
-
`telephone` int(11) DEFAULT ‘0‘ COMMENT ‘手机号码‘,
-
-
KEY `index_ages` (`ages`)
-
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT=‘用户表‘ |
-
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
-
转自
(3条消息)mysql:pt-online-schema-change 在线修改表_雨花石-CSDN博客_pt-online-schema-change https://blog.csdn.net/shiyu1157758655/article/details/84854513
参考
(3条消息)案例 - optimize table 的一些坑_weixin_34248023的博客-CSDN博客_optimize table https://blog.csdn.net/weixin_34248023/article/details/91617531
(3条消息)[mysql]清除单表大量数据方法(需保留部分数据)_yyongsheng的博客-CSDN博客_mysql truncate清除部分数据 https://blog.csdn.net/yyongsheng/article/details/82938683
MySQL大表删除工具pt-osc? - 云+社区 - 腾讯云 https://cloud.tencent.com/developer/article/1533722
PT-OSC在线DDL变更工具使用攻略 - MySQL数据库技术栈的个人空间 - OSCHINA https://my.oschina.net/u/3678773/blog/4467882
(3条消息)mysql 案例~关于pt-osc工具的用途_weixin_34212189的博客-CSDN博客_mysql ptosc 工具 https://blog.csdn.net/weixin_34212189/article/details/93262658
pt-osc原理 - 茁壮的小草 - 博客园 https://www.cnblogs.com/mysql-dba/p/9901632.html
mysql:pt-online-schema-change 在线修改表、删除表数据【转】
原文:https://www.cnblogs.com/paul8339/p/13489994.html