zoukankan      html  css  js  c++  java
  • pt-online-schema-change

    [root@mysql5 ~]# pt-online-schema-change --alter="drop column address" h=127.0.0.1,P=3306,u=root,p=1qaz2wsx,D=test,t=ddl_test --print --dry-run
    Operation, tries, wait:
      copy_rows, 10, 0.25
      create_triggers, 10, 1
      drop_triggers, 10, 1
      swap_tables, 10, 1
      update_foreign_keys, 10, 1
    Starting a dry run.  `test`.`ddl_test` will not be altered.  Specify --execute instead of --dry-run to alter the table.
    步骤1,创建空表,其命名规则是_+原表名+_new
    Creating new table...
    CREATE TABLE `test`.`_ddl_test_new` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(20) NOT NULL DEFAULT '',
      `age` int(3) DEFAULT '0',
      `address` varchar(200) NOT NULL DEFAULT '',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=196805678 DEFAULT CHARSET=utf8
    Created new table test._ddl_test_new OK.
    #步骤2,根据语句更新新表结构
    Altering new table...
    ALTER TABLE `test`.`_ddl_test_new` drop column address
    Altered `test`.`_ddl_test_new` OK.
    #步骤3,在原表上创建触发器,以便后续原表上的操作同步到新表
    Not creating triggers because this is a dry run.
    CREATE TRIGGER `pt_osc_test_ddl_test_del` AFTER DELETE ON `test`.`ddl_test` FOR EACH ROW DELETE IGNORE FROM `test`.`_ddl_test_new` WHERE `test`.`_ddl_test_new`.`id` <=> OLD.`id`
    CREATE TRIGGER `pt_osc_test_ddl_test_upd` AFTER UPDATE ON `test`.`ddl_test` FOR EACH ROW REPLACE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`) VALUES (NEW.`id`, NEW.`name`, NEW.`age`)
    CREATE TRIGGER `pt_osc_test_ddl_test_ins` AFTER INSERT ON `test`.`ddl_test` FOR EACH ROW REPLACE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`) VALUES (NEW.`id`, NEW.`name`, NEW.`age`)
    Not copying rows because this is a dry run.
    步骤4,拷贝原表数据到新表,数据量大时会根据主键进行分段chunk插入
    INSERT LOW_PRIORITY IGNORE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`) SELECT `id`, `name`, `age` FROM `test`.`ddl_test` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ?)) AND ((`id` <= ?)) LOCK IN SHARE MODE /*pt-online-schema-change 26819 copy nibble*/
    SELECT /*!40001 SQL_NO_CACHE */ `id` FROM `test`.`ddl_test` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ?)) ORDER BY `id` LIMIT ?, 2 /*next chunk boundary*/
    
    #最后清除临时生成的表、触发器。默认情况下会删除原表
    Not swapping tables because this is a dry run.
    Not dropping old table because this is a dry run.
    Not dropping triggers because this is a dry run.
    DROP TRIGGER IF EXISTS `test`.`pt_osc_test_ddl_test_del`;
    DROP TRIGGER IF EXISTS `test`.`pt_osc_test_ddl_test_upd`;
    DROP TRIGGER IF EXISTS `test`.`pt_osc_test_ddl_test_ins`;
    2015-10-29T15:51:41 Dropping new table...
    DROP TABLE IF EXISTS `test`.`_ddl_test_new`;
    2015-10-29T15:51:41 Dropped new table OK.
    Dry run complete.  `test`.`ddl_test` was not altered.
    [root@mysql5 ~]# 
    
    因为这不是真正执行,真正执行有个过程
    拷贝完成后,移走原表,用新表代替(RENAME TABLE)。其通过一个RENAME TABLE同时处理两个表,实现原子操作。
    2015-10-29T16:08:44 Copied rows OK.
    2015-10-29T16:08:44 Swapping tables...
    RENAME TABLE `test`.`ddl_test` TO `test`.`_ddl_test_old`, `test`.`_ddl_test_new` TO `test`.`ddl_test`
    2015-10-29T16:08:44 Swapped original and new tables OK.
    ##如果原表有触发器
    Oracle可以在一个触发器触发insert,delete,update事件,
    MySQL 触发器 只支持一个事件,然后一个表允许三个事件触发器(insert,update,deleted),在5.7之前,一个表只能一类型的触发器,不能存在两个(update )事件触发器,假设已经有了一个update的事件触发器,那么使用pt-online-schema-change加三个事件触发期的时候会加不上,导致工具无法使用。
    那么下面来看看,整个实验过程, ##首先,执行这个工具,前面不能有长事务(如:大查询,大范围更新插入语句),下面就来看看又大事务会咋样 (mysql5.7-101)root@localhost [(none)]> show processlist; +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ | 62 | root | localhost | test | Query | 5 | optimizing | select count(*) from ddl_test | | 72 | root | localhost | NULL | Query | 0 | starting | show processlist | | 73 | root | 127.0.0.1:43059 | test | Query | 2 | Waiting for table metadata lock | CREATE TRIGGER `pt_osc_test_ddl_test_del` AFTER DELETE ON `test`.`ddl_test` FOR EACH ROW DELETE IGNO | | 74 | root | 127.0.0.1:43060 | test | Sleep | 3 | | NULL | +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ 4 rows in set (0.00 sec) (mysql5.7-101)root@localhost [(none)]> (mysql5.7-101)root@localhost [(none)]> show processlist; +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ | 62 | root | localhost | test | Query | 5 | optimizing | select count(*) from ddl_test | | 72 | root | localhost | NULL | Query | 0 | starting | show processlist | | 73 | root | 127.0.0.1:43059 | test | Query | 2 | Waiting for table metadata lock | CREATE TRIGGER `pt_osc_test_ddl_test_del` AFTER DELETE ON `test`.`ddl_test` FOR EACH ROW DELETE IGNO | | 74 | root | 127.0.0.1:43060 | test | Sleep | 3 | | NULL | +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ 4 rows in set (0.00 sec) (mysql5.7-101)root@localhost [(none)]> show processlist; +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ | 62 | root | localhost | test | Query | 17 | optimizing | select count(*) from ddl_test | | 72 | root | localhost | NULL | Query | 0 | starting | show processlist | | 73 | root | 127.0.0.1:43059 | test | Query | 14 | Waiting for table metadata lock | CREATE TRIGGER `pt_osc_test_ddl_test_del` AFTER DELETE ON `test`.`ddl_test` FOR EACH ROW DELETE IGNO | | 74 | root | 127.0.0.1:43060 | test | Sleep | 15 | | NULL | +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ 4 rows in set (0.00 sec) (mysql5.7-101)root@localhost [(none)]> show processlist; +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ | 62 | root | localhost | test | Query | 19 | optimizing | select count(*) from ddl_test | | 72 | root | localhost | NULL | Query | 0 | starting | show processlist | | 73 | root | 127.0.0.1:43059 | test | Query | 16 | Waiting for table metadata lock | CREATE TRIGGER `pt_osc_test_ddl_test_del` AFTER DELETE ON `test`.`ddl_test` FOR EACH ROW DELETE IGNO | | 74 | root | 127.0.0.1:43060 | test | Sleep | 17 | | NULL | +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ 4 rows in set (0.00 sec) (mysql5.7-101)root@localhost [(none)]> show processlist; +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ | 62 | root | localhost | test | Query | 24 | optimizing | select count(*) from ddl_test | | 72 | root | localhost | NULL | Query | 0 | starting | show processlist | | 73 | root | 127.0.0.1:43059 | test | Query | 21 | Waiting for table metadata lock | CREATE TRIGGER `pt_osc_test_ddl_test_del` AFTER DELETE ON `test`.`ddl_test` FOR EACH ROW DELETE IGNO | | 74 | root | 127.0.0.1:43060 | test | Sleep | 22 | | NULL | +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ 4 rows in set (0.00 sec) (mysql5.7-101)root@localhost [(none)]> show processlist; +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | 62 | root | localhost | test | Sleep | 24 | | NULL | | 72 | root | localhost | NULL | Query | 0 | starting | show processlist | | 73 | root | 127.0.0.1:43059 | test | Query | 0 | Sending data | INSERT LOW_PRIORITY IGNORE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`) SELECT `id`, `name`, `a | | 74 | root | 127.0.0.1:43060 | test | Sleep | 22 | | NULL | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ 4 rows in set (0.00 sec) (mysql5.7-101)root@localhost [(none)]> show processlist; +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | 62 | root | localhost | test | Sleep | 26 | | NULL | | 72 | root | localhost | NULL | Query | 0 | starting | show processlist | | 73 | root | 127.0.0.1:43059 | test | Query | 0 | Sending data | INSERT LOW_PRIORITY IGNORE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`) SELECT `id`, `name`, `a | | 74 | root | 127.0.0.1:43060 | test | Sleep | 24 | | NULL | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ 4 rows in set (0.00 sec) (mysql5.7-101)root@localhost [(none)]> show processlist; +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | 62 | root | localhost | test | Sleep | 30 | | NULL | | 72 | root | localhost | NULL | Query | 0 | starting | show processlist | | 73 | root | 127.0.0.1:43059 | test | Query | 0 | Sending data | INSERT LOW_PRIORITY IGNORE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`) SELECT `id`, `name`, `a | | 74 | root | 127.0.0.1:43060 | test | Sleep | 28 | | NULL | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ 4 rows in set (0.00 sec) 哈哈,有前面大查询一样要等待MDL锁,MDL锁这个坑很大,所以有在线DDL也不能随心所欲的执行

    测试一下删除字段 (mysql5.
    7-101)root@localhost [(none)]> [root@mysql5 ~]# pt-online-schema-change --alter="drop column address" h=127.0.0.1,P=3306,u=root,p=1qaz2wsx,D=test,t=ddl_test --charset=utf8 --print --execute No slaves found. See --recursion-method if host mysql5.7 has slaves. Not checking slave lag because no slaves were found and --check-slave-lag was not specified. Operation, tries, wait: copy_rows, 10, 0.25 create_triggers, 10, 1 drop_triggers, 10, 1 swap_tables, 10, 1 update_foreign_keys, 10, 1 Altering `test`.`ddl_test`... Creating new table... CREATE TABLE `test`.`_ddl_test_new` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL DEFAULT '', `age` int(3) DEFAULT '0', `address` varchar(200) NOT NULL DEFAULT '', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=196805678 DEFAULT CHARSET=utf8 Created new table test._ddl_test_new OK. Altering new table... ALTER TABLE `test`.`_ddl_test_new` drop column address Altered `test`.`_ddl_test_new` OK. 2015-10-29T15:58:50 Creating triggers... CREATE TRIGGER `pt_osc_test_ddl_test_del` AFTER DELETE ON `test`.`ddl_test` FOR EACH ROW DELETE IGNORE FROM `test`.`_ddl_test_new` WHERE `test`.`_ddl_test_new`.`id` <=> OLD.`id` CREATE TRIGGER `pt_osc_test_ddl_test_upd` AFTER UPDATE ON `test`.`ddl_test` FOR EACH ROW REPLACE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`) VALUES (NEW.`id`, NEW.`name`, NEW.`age`) CREATE TRIGGER `pt_osc_test_ddl_test_ins` AFTER INSERT ON `test`.`ddl_test` FOR EACH ROW REPLACE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`) VALUES (NEW.`id`, NEW.`name`, NEW.`age`) 2015-10-29T15:59:11 Created triggers OK. 2015-10-29T15:59:11 Copying approximately 111022344 rows... INSERT LOW_PRIORITY IGNORE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`) SELECT `id`, `name`, `age` FROM `test`.`ddl_test` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ?)) AND ((`id` <= ?)) LOCK IN SHARE MODE /*pt-online-schema-change 26826 copy nibble*/ SELECT /*!40001 SQL_NO_CACHE */ `id` FROM `test`.`ddl_test` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ?)) ORDER BY `id` LIMIT ?, 2 /*next chunk boundary*/ Copying `test`.`ddl_test`: 6% 07:13 remain Copying `test`.`ddl_test`: 12% 07:02 remain Copying `test`.`ddl_test`: 18% 06:44 remain Copying `test`.`ddl_test`: 23% 06:20 remain Copying `test`.`ddl_test`: 29% 05:54 remain Copying `test`.`ddl_test`: 35% 05:27 remain Copying `test`.`ddl_test`: 41% 04:57 remain Copying `test`.`ddl_test`: 47% 04:28 remain Copying `test`.`ddl_test`: 52% 03:59 remain Copying `test`.`ddl_test`: 58% 03:30 remain Copying `test`.`ddl_test`: 64% 03:00 remain Copying `test`.`ddl_test`: 70% 02:29 remain Copying `test`.`ddl_test`: 76% 01:59 remain Copying `test`.`ddl_test`: 82% 01:31 remain Copying `test`.`ddl_test`: 87% 01:02 remain Copying `test`.`ddl_test`: 93% 00:34 remain Copying `test`.`ddl_test`: 98% 00:05 remain 2015-10-29T16:08:44 Copied rows OK. 2015-10-29T16:08:44 Swapping tables... RENAME TABLE `test`.`ddl_test` TO `test`.`_ddl_test_old`, `test`.`_ddl_test_new` TO `test`.`ddl_test` 2015-10-29T16:08:44 Swapped original and new tables OK. 2015-10-29T16:08:44 Dropping old table... DROP TABLE IF EXISTS `test`.`_ddl_test_old` 2015-10-29T16:08:45 Dropped old table `test`.`_ddl_test_old` OK. 2015-10-29T16:08:45 Dropping triggers... DROP TRIGGER IF EXISTS `test`.`pt_osc_test_ddl_test_del`; DROP TRIGGER IF EXISTS `test`.`pt_osc_test_ddl_test_upd`; DROP TRIGGER IF EXISTS `test`.`pt_osc_test_ddl_test_ins`; 2015-10-29T16:08:45 Dropped triggers OK. Successfully altered `test`.`ddl_test`. [root@mysql5 ~]# (mysql5.7-101)root@localhost [test]> show processlist; +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ | 62 | root | localhost | test | Query | 8 | optimizing | select count(*) from ddl_test | | 72 | root | localhost | test | Query | 0 | starting | show processlist | | 75 | root | 127.0.0.1:43064 | test | Query | 6 | Waiting for table metadata lock | CREATE TRIGGER `pt_osc_test_ddl_test_del` AFTER DELETE ON `test`.`ddl_test` FOR EACH ROW DELETE IGNO | | 76 | root | 127.0.0.1:43065 | test | Sleep | 6 | | NULL | +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ 4 rows in set (0.00 sec) (mysql5.7-101)root@localhost [test]> show processlist; +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ | 62 | root | localhost | test | Query | 9 | optimizing | select count(*) from ddl_test | | 72 | root | localhost | test | Query | 0 | starting | show processlist | | 75 | root | 127.0.0.1:43064 | test | Query | 7 | Waiting for table metadata lock | CREATE TRIGGER `pt_osc_test_ddl_test_del` AFTER DELETE ON `test`.`ddl_test` FOR EACH ROW DELETE IGNO | | 76 | root | 127.0.0.1:43065 | test | Sleep | 7 | | NULL | +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ 4 rows in set (0.00 sec) (mysql5.7-101)root@localhost [test]> show processlist; +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | 62 | root | localhost | test | Sleep | 29 | | NULL | | 72 | root | localhost | test | Query | 0 | starting | show processlist | | 75 | root | 127.0.0.1:43064 | test | Query | 1 | Sending data | INSERT LOW_PRIORITY IGNORE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`) SELECT `id`, `name`, `a | | 76 | root | 127.0.0.1:43065 | test | Sleep | 27 | | NULL | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ 4 rows in set (0.00 sec) (mysql5.7-101)root@localhost [test]> show processlist; +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | 62 | root | localhost | test | Sleep | 30 | | NULL | | 72 | root | localhost | test | Query | 0 | starting | show processlist | | 75 | root | 127.0.0.1:43064 | test | Query | 0 | Sending data | INSERT LOW_PRIORITY IGNORE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`) SELECT `id`, `name`, `a | | 76 | root | 127.0.0.1:43065 | test | Sleep | 28 | | NULL | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ 4 rows in set (0.00 sec) (mysql5.7-101)root@localhost [test]> show processlist; +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | 62 | root | localhost | test | Sleep | 31 | | NULL | | 72 | root | localhost | test | Query | 0 | starting | show processlist | | 75 | root | 127.0.0.1:43064 | test | Query | 0 | Sending data | INSERT LOW_PRIORITY IGNORE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`) SELECT `id`, `name`, `a | | 76 | root | 127.0.0.1:43065 | test | Sleep | 29 | | NULL | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ 4 rows in set (0.00 sec) (mysql5.7-101)root@localhost [test]> delete from ddl_test where id=10000; Query OK, 1 row affected (0.05 sec) (mysql5.7-101)root@localhost [test]> show processlist; +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | 62 | root | localhost | test | Sleep | 57 | | NULL | | 72 | root | localhost | test | Query | 0 | starting | show processlist | | 75 | root | 127.0.0.1:43064 | test | Query | 0 | Sending data | INSERT LOW_PRIORITY IGNORE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`) SELECT `id`, `name`, `a | | 76 | root | 127.0.0.1:43065 | test | Sleep | 55 | | NULL | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ 4 rows in set (0.00 sec)
    ###执行一下DML语句 (mysql5.
    7-101)root@localhost [test]> update ddl_test set name='12eswq' where id=10000; Query OK, 0 rows affected (0.00 sec) Rows matched: 0 Changed: 0 Warnings: 0 (mysql5.7-101)root@localhost [test]> update ddl_test set name='12eswq' where id=10001; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 (mysql5.7-101)root@localhost [test]> show processlist; +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | 62 | root | localhost | test | Sleep | 87 | | NULL | | 72 | root | localhost | test | Query | 0 | starting | show processlist | | 75 | root | 127.0.0.1:43064 | test | Query | 0 | Sending data | SELECT /*!40001 SQL_NO_CACHE */ `id` FROM `test`.`ddl_test` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ' | | 76 | root | 127.0.0.1:43065 | test | Sleep | 85 | | NULL | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ 4 rows in set (0.00 sec) (mysql5.7-101)root@localhost [test]> show processlist; +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | 62 | root | localhost | test | Sleep | 92 | | NULL | | 72 | root | localhost | test | Query | 0 | starting | show processlist | | 75 | root | 127.0.0.1:43064 | test | Query | 0 | Sending data | INSERT LOW_PRIORITY IGNORE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`) SELECT `id`, `name`, `a | | 76 | root | 127.0.0.1:43065 | test | Sleep | 90 | | NULL | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ 4 rows in set (0.00 sec) (mysql5.7-101)root@localhost [test]> ##增加字段测试 [root@mysql5 ~]# pt-online-schema-change --alter="add column address varchar(100) not null default ''" h=127.0.0.1,P=3306,u=root,p=1qaz2wsx,D=test,t=ddl_test --print --execute No slaves found. See --recursion-method if host mysql5.7 has slaves. Not checking slave lag because no slaves were found and --check-slave-lag was not specified. Operation, tries, wait: copy_rows, 10, 0.25 create_triggers, 10, 1 drop_triggers, 10, 1 swap_tables, 10, 1 update_foreign_keys, 10, 1 Altering `test`.`ddl_test`... Creating new table... CREATE TABLE `test`.`_ddl_test_new` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL DEFAULT '', `age` int(3) DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=196805678 DEFAULT CHARSET=utf8 Created new table test._ddl_test_new OK. Altering new table... ALTER TABLE `test`.`_ddl_test_new` add column address varchar(100) not null default '' Altered `test`.`_ddl_test_new` OK. 2015-10-29T16:26:45 Creating triggers... CREATE TRIGGER `pt_osc_test_ddl_test_del` AFTER DELETE ON `test`.`ddl_test` FOR EACH ROW DELETE IGNORE FROM `test`.`_ddl_test_new` WHERE `test`.`_ddl_test_new`.`id` <=> OLD.`id` CREATE TRIGGER `pt_osc_test_ddl_test_upd` AFTER UPDATE ON `test`.`ddl_test` FOR EACH ROW REPLACE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`) VALUES (NEW.`id`, NEW.`name`, NEW.`age`) CREATE TRIGGER `pt_osc_test_ddl_test_ins` AFTER INSERT ON `test`.`ddl_test` FOR EACH ROW REPLACE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`) VALUES (NEW.`id`, NEW.`name`, NEW.`age`) 2015-10-29T16:27:02 Created triggers OK. 2015-10-29T16:27:02 Copying approximately 116880295 rows... INSERT LOW_PRIORITY IGNORE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`) SELECT `id`, `name`, `age` FROM `test`.`ddl_test` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ?)) AND ((`id` <= ?)) LOCK IN SHARE MODE /*pt-online-schema-change 26858 copy nibble*/ SELECT /*!40001 SQL_NO_CACHE */ `id` FROM `test`.`ddl_test` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ?)) ORDER BY `id` LIMIT ?, 2 /*next chunk boundary*/ Copying `test`.`ddl_test`: 5% 08:48 remain Copying `test`.`ddl_test`: 10% 08:17 remain Copying `test`.`ddl_test`: 16% 07:47 remain Copying `test`.`ddl_test`: 21% 07:19 remain Copying `test`.`ddl_test`: 26% 06:51 remain Copying `test`.`ddl_test`: 32% 06:21 remain Copying `test`.`ddl_test`: 37% 05:51 remain Copying `test`.`ddl_test`: 42% 05:22 remain Copying `test`.`ddl_test`: 47% 04:52 remain Copying `test`.`ddl_test`: 53% 04:22 remain Copying `test`.`ddl_test`: 58% 03:52 remain Copying `test`.`ddl_test`: 63% 03:23 remain Copying `test`.`ddl_test`: 69% 02:53 remain Copying `test`.`ddl_test`: 74% 02:22 remain Copying `test`.`ddl_test`: 80% 01:52 remain Copying `test`.`ddl_test`: 85% 01:21 remain Copying `test`.`ddl_test`: 90% 00:51 remain Copying `test`.`ddl_test`: 96% 00:21 remain 2015-10-29T16:36:53 Copied rows OK. 2015-10-29T16:36:53 Swapping tables... RENAME TABLE `test`.`ddl_test` TO `test`.`_ddl_test_old`, `test`.`_ddl_test_new` TO `test`.`ddl_test` 2015-10-29T16:36:53 Swapped original and new tables OK. 2015-10-29T16:36:53 Dropping old table... DROP TABLE IF EXISTS `test`.`_ddl_test_old` 2015-10-29T16:36:53 Dropped old table `test`.`_ddl_test_old` OK. 2015-10-29T16:36:53 Dropping triggers... DROP TRIGGER IF EXISTS `test`.`pt_osc_test_ddl_test_del`; DROP TRIGGER IF EXISTS `test`.`pt_osc_test_ddl_test_upd`; DROP TRIGGER IF EXISTS `test`.`pt_osc_test_ddl_test_ins`; 2015-10-29T16:36:53 Dropped triggers OK. Successfully altered `test`.`ddl_test`. [root@mysql5 ~]# (mysql5.7-101)root@localhost [test]> show processlist; +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | 62 | root | localhost | test | Sleep | 87 | | NULL | | 72 | root | localhost | test | Query | 0 | starting | show processlist | | 75 | root | 127.0.0.1:43064 | test | Query | 0 | Sending data | SELECT /*!40001 SQL_NO_CACHE */ `id` FROM `test`.`ddl_test` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ' | | 76 | root | 127.0.0.1:43065 | test | Sleep | 85 | | NULL | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ 4 rows in set (0.00 sec) ##来个DML看看 (mysql5.7-101)root@localhost [test]> delete from ddl_test where id=2000; Query OK, 0 rows affected (0.03 sec) (mysql5.7-101)root@localhost [test]> delete from ddl_test where id=7000; Query OK, 0 rows affected (0.00 sec) (mysql5.7-101)root@localhost [test]> update ddl_test set name='adfadf' where id=8090; Query OK, 0 rows affected (0.00 sec) Rows matched: 0 Changed: 0 Warnings: 0 (mysql5.7-101)root@localhost [test]> show processlist; +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | 62 | root | localhost | test | Sleep | 92 | | NULL | | 72 | root | localhost | test | Query | 0 | starting | show processlist | | 75 | root | 127.0.0.1:43064 | test | Query | 0 | Sending data | INSERT LOW_PRIORITY IGNORE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`) SELECT `id`, `name`, `a | | 76 | root | 127.0.0.1:43065 | test | Sleep | 90 | | NULL | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ 4 rows in set (0.00 sec) (mysql5.7-101)root@localhost [test]> ### [root@mysql5 ~]# pt-online-schema-change --alter="add column address varchar(100) not null default ''" h=127.0.0.1,P=3306,u=root,p=1qaz2wsx,D=test,t=ddl_test --print --execute No slaves found. See --recursion-method if host mysql5.7 has slaves. Not checking slave lag because no slaves were found and --check-slave-lag was not specified. Operation, tries, wait: copy_rows, 10, 0.25 create_triggers, 10, 1 drop_triggers, 10, 1 swap_tables, 10, 1 update_foreign_keys, 10, 1 Altering `test`.`ddl_test`... Creating new table... CREATE TABLE `test`.`_ddl_test_new` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL DEFAULT '', `age` int(3) DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=196805678 DEFAULT CHARSET=utf8 Created new table test._ddl_test_new OK. Altering new table... ALTER TABLE `test`.`_ddl_test_new` add column address varchar(100) not null default '' Altered `test`.`_ddl_test_new` OK. 2015-10-29T16:26:45 Creating triggers... CREATE TRIGGER `pt_osc_test_ddl_test_del` AFTER DELETE ON `test`.`ddl_test` FOR EACH ROW DELETE IGNORE FROM `test`.`_ddl_test_new` WHERE `test`.`_ddl_test_new`.`id` <=> OLD.`id` CREATE TRIGGER `pt_osc_test_ddl_test_upd` AFTER UPDATE ON `test`.`ddl_test` FOR EACH ROW REPLACE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`) VALUES (NEW.`id`, NEW.`name`, NEW.`age`) CREATE TRIGGER `pt_osc_test_ddl_test_ins` AFTER INSERT ON `test`.`ddl_test` FOR EACH ROW REPLACE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`) VALUES (NEW.`id`, NEW.`name`, NEW.`age`) 2015-10-29T16:27:02 Created triggers OK. 2015-10-29T16:27:02 Copying approximately 116880295 rows... INSERT LOW_PRIORITY IGNORE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`) SELECT `id`, `name`, `age` FROM `test`.`ddl_test` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ?)) AND ((`id` <= ?)) LOCK IN SHARE MODE /*pt-online-schema-change 26858 copy nibble*/ SELECT /*!40001 SQL_NO_CACHE */ `id` FROM `test`.`ddl_test` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ?)) ORDER BY `id` LIMIT ?, 2 /*next chunk boundary*/ Copying `test`.`ddl_test`: 5% 08:48 remain Copying `test`.`ddl_test`: 10% 08:17 remain Copying `test`.`ddl_test`: 16% 07:47 remain Copying `test`.`ddl_test`: 21% 07:19 remain Copying `test`.`ddl_test`: 26% 06:51 remain Copying `test`.`ddl_test`: 32% 06:21 remain Copying `test`.`ddl_test`: 37% 05:51 remain Copying `test`.`ddl_test`: 42% 05:22 remain Copying `test`.`ddl_test`: 47% 04:52 remain Copying `test`.`ddl_test`: 53% 04:22 remain Copying `test`.`ddl_test`: 58% 03:52 remain Copying `test`.`ddl_test`: 63% 03:23 remain Copying `test`.`ddl_test`: 69% 02:53 remain Copying `test`.`ddl_test`: 74% 02:22 remain Copying `test`.`ddl_test`: 80% 01:52 remain Copying `test`.`ddl_test`: 85% 01:21 remain Copying `test`.`ddl_test`: 90% 00:51 remain Copying `test`.`ddl_test`: 96% 00:21 remain 2015-10-29T16:36:53 Copied rows OK. 2015-10-29T16:36:53 Swapping tables... RENAME TABLE `test`.`ddl_test` TO `test`.`_ddl_test_old`, `test`.`_ddl_test_new` TO `test`.`ddl_test` 2015-10-29T16:36:53 Swapped original and new tables OK. 2015-10-29T16:36:53 Dropping old table... DROP TABLE IF EXISTS `test`.`_ddl_test_old` 2015-10-29T16:36:53 Dropped old table `test`.`_ddl_test_old` OK. 2015-10-29T16:36:53 Dropping triggers... DROP TRIGGER IF EXISTS `test`.`pt_osc_test_ddl_test_del`; DROP TRIGGER IF EXISTS `test`.`pt_osc_test_ddl_test_upd`; DROP TRIGGER IF EXISTS `test`.`pt_osc_test_ddl_test_ins`; 2015-10-29T16:36:53 Dropped triggers OK. Successfully altered `test`.`ddl_test`. [root@mysql5 ~]# pt-online-schema-change --alter="modify column address varchar(255) not null default ''" h=127.0.0.1,P=3306,u=root,p=1qaz2wsx,D=test,t=ddl_test --print --execute No slaves found. See --recursion-method if host mysql5.7 has slaves. Not checking slave lag because no slaves were found and --check-slave-lag was not specified. Operation, tries, wait: copy_rows, 10, 0.25 create_triggers, 10, 1 drop_triggers, 10, 1 swap_tables, 10, 1 update_foreign_keys, 10, 1 Altering `test`.`ddl_test`... Creating new table... CREATE TABLE `test`.`_ddl_test_new` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL DEFAULT '', `age` int(3) DEFAULT '0', `address` varchar(100) NOT NULL DEFAULT '', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=196805678 DEFAULT CHARSET=utf8 Created new table test._ddl_test_new OK. Altering new table... ALTER TABLE `test`.`_ddl_test_new` modify column address varchar(255) not null default '' Altered `test`.`_ddl_test_new` OK. 2015-10-29T16:42:10 Creating triggers... CREATE TRIGGER `pt_osc_test_ddl_test_del` AFTER DELETE ON `test`.`ddl_test` FOR EACH ROW DELETE IGNORE FROM `test`.`_ddl_test_new` WHERE `test`.`_ddl_test_new`.`id` <=> OLD.`id` CREATE TRIGGER `pt_osc_test_ddl_test_upd` AFTER UPDATE ON `test`.`ddl_test` FOR EACH ROW REPLACE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`, `address`) VALUES (NEW.`id`, NEW.`name`, NEW.`age`, NEW.`address`) CREATE TRIGGER `pt_osc_test_ddl_test_ins` AFTER INSERT ON `test`.`ddl_test` FOR EACH ROW REPLACE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`, `address`) VALUES (NEW.`id`, NEW.`name`, NEW.`age`, NEW.`address`) 2015-10-29T16:42:38 Created triggers OK. 2015-10-29T16:42:38 Copying approximately 115679967 rows... INSERT LOW_PRIORITY IGNORE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`, `address`) SELECT `id`, `name`, `age`, `address` FROM `test`.`ddl_test` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ?)) AND ((`id` <= ?)) LOCK IN SHARE MODE /*pt-online-schema-change 26897 copy nibble*/ SELECT /*!40001 SQL_NO_CACHE */ `id` FROM `test`.`ddl_test` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ?)) ORDER BY `id` LIMIT ?, 2 /*next chunk boundary*/ Copying `test`.`ddl_test`: 4% 09:43 remain Copying `test`.`ddl_test`: 9% 09:11 remain Copying `test`.`ddl_test`: 14% 08:40 remain Copying `test`.`ddl_test`: 19% 08:12 remain Copying `test`.`ddl_test`: 24% 07:39 remain Copying `test`.`ddl_test`: 29% 07:09 remain Copying `test`.`ddl_test`: 34% 06:38 remain Copying `test`.`ddl_test`: 39% 06:08 remain Copying `test`.`ddl_test`: 44% 05:38 remain Copying `test`.`ddl_test`: 49% 05:08 remain Copying `test`.`ddl_test`: 54% 04:36 remain Copying `test`.`ddl_test`: 59% 04:06 remain Copying `test`.`ddl_test`: 64% 03:34 remain Copying `test`.`ddl_test`: 69% 03:03 remain Copying `test`.`ddl_test`: 74% 02:32 remain Copying `test`.`ddl_test`: 79% 02:03 remain Copying `test`.`ddl_test`: 84% 01:32 remain Copying `test`.`ddl_test`: 89% 01:03 remain Copying `test`.`ddl_test`: 94% 00:32 remain Copying `test`.`ddl_test`: 99% 00:02 remain 2015-10-29T16:53:19 Copied rows OK. 2015-10-29T16:53:19 Swapping tables... RENAME TABLE `test`.`ddl_test` TO `test`.`_ddl_test_old`, `test`.`_ddl_test_new` TO `test`.`ddl_test` 2015-10-29T16:53:19 Swapped original and new tables OK. 2015-10-29T16:53:19 Dropping old table... DROP TABLE IF EXISTS `test`.`_ddl_test_old` 2015-10-29T16:53:20 Dropped old table `test`.`_ddl_test_old` OK. 2015-10-29T16:53:20 Dropping triggers... DROP TRIGGER IF EXISTS `test`.`pt_osc_test_ddl_test_del`; DROP TRIGGER IF EXISTS `test`.`pt_osc_test_ddl_test_upd`; DROP TRIGGER IF EXISTS `test`.`pt_osc_test_ddl_test_ins`; 2015-10-29T16:53:20 Dropped triggers OK. Successfully altered `test`.`ddl_test`. [root@mysql5 ~]# (mysql5.7-101)root@localhost [test]> show processlist; +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ | 62 | root | localhost | test | Query | 3 | Waiting for table metadata lock | select count(*) from ddl_test | | 72 | root | localhost | test | Query | 0 | starting | show processlist | | 77 | root | localhost | test | Query | 17 | optimizing | select count(*) from ddl_test | | 78 | root | 127.0.0.1:43069 | test | Query | 5 | Waiting for table metadata lock | CREATE TRIGGER `pt_osc_test_ddl_test_del` AFTER DELETE ON `test`.`ddl_test` FOR EACH ROW DELETE IGNO | | 79 | root | 127.0.0.1:43070 | test | Sleep | 5 | | NULL | +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ 5 rows in set (0.00 sec) (mysql5.7-101)root@localhost [test]> show processlist; +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ | 62 | root | localhost | test | Query | 9 | optimizing | select count(*) from ddl_test | | 72 | root | localhost | test | Query | 0 | starting | show processlist | | 77 | root | localhost | test | Sleep | 23 | | NULL | | 78 | root | 127.0.0.1:43069 | test | Query | 3 | Waiting for table metadata lock | CREATE TRIGGER `pt_osc_test_ddl_test_upd` AFTER UPDATE ON `test`.`ddl_test` FOR EACH ROW REPLACE INT | | 79 | root | 127.0.0.1:43070 | test | Sleep | 11 | | NULL | +----+------+-----------------+------+---------+------+---------------------------------+------------------------------------------------------------------------------------------------------+ 5 rows in set (0.00 sec) (mysql5.7-101)root@localhost [test]> (mysql5.7-101)root@localhost [test]> show processlist; +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | 62 | root | localhost | test | Sleep | 46 | | NULL | | 72 | root | localhost | test | Query | 0 | starting | show processlist | | 77 | root | localhost | test | Sleep | 60 | | NULL | | 78 | root | 127.0.0.1:43069 | test | Query | 1 | Sending data | INSERT LOW_PRIORITY IGNORE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`, `address`) SELECT `id`, | | 79 | root | 127.0.0.1:43070 | test | Sleep | 48 | | NULL | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ 5 rows in set (0.00 sec) (mysql5.7-101)root@localhost [test]> update ddl_test set name='12eswq' where id=30000; Query OK, 0 rows affected (0.00 sec) Rows matched: 0 Changed: 0 Warnings: 0 (mysql5.7-101)root@localhost [test]> update ddl_test set name='12eswq' where id=30001; Query OK, 0 rows affected (0.00 sec) Rows matched: 0 Changed: 0 Warnings: 0 (mysql5.7-101)root@localhost [test]> update ddl_test set name='12eswq' where id=40001; Query OK, 1 row affected (0.24 sec) Rows matched: 1 Changed: 1 Warnings: 0 (mysql5.7-101)root@localhost [test]> show processlist; +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | 62 | root | localhost | test | Sleep | 68 | | NULL | | 72 | root | localhost | test | Query | 0 | starting | show processlist | | 77 | root | localhost | test | Sleep | 82 | | NULL | | 78 | root | 127.0.0.1:43069 | test | Query | 0 | Sending data | SELECT /*!40001 SQL_NO_CACHE */ `id` FROM `test`.`ddl_test` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ' | | 79 | root | 127.0.0.1:43070 | test | Sleep | 70 | | NULL | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ 5 rows in set (0.00 sec) (mysql5.7-101)root@localhost [test]> update ddl_test set name='12eswq' where id=40008; Query OK, 1 row affected (0.08 sec) Rows matched: 1 Changed: 1 Warnings: 0 (mysql5.7-101)root@localhost [test]> delete from ddl_test where id=40008; Query OK, 1 row affected (0.01 sec) (mysql5.7-101)root@localhost [test]> show processlist; +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | 62 | root | localhost | test | Sleep | 97 | | NULL | | 72 | root | localhost | test | Query | 0 | starting | show processlist | | 77 | root | localhost | test | Sleep | 111 | | NULL | | 78 | root | 127.0.0.1:43069 | test | Query | 0 | Sending data | SELECT /*!40001 SQL_NO_CACHE */ `id` FROM `test`.`ddl_test` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ' | | 79 | root | 127.0.0.1:43070 | test | Sleep | 99 | | NULL | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ 5 rows in set (0.00 sec) (mysql5.7-101)root@localhost [test]> show processlist; +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ | 62 | root | localhost | test | Sleep | 298 | | NULL | | 72 | root | localhost | test | Query | 0 | starting | show processlist | | 77 | root | localhost | test | Sleep | 312 | | NULL | | 78 | root | 127.0.0.1:43069 | test | Query | 1 | Sending data | INSERT LOW_PRIORITY IGNORE INTO `test`.`_ddl_test_new` (`id`, `name`, `age`, `address`) SELECT `id`, | | 79 | root | 127.0.0.1:43070 | test | Sleep | 300 | | NULL | +----+------+-----------------+------+---------+------+--------------+------------------------------------------------------------------------------------------------------+ 5 rows in set (0.00 sec) (mysql5.7-101)root@localhost [test]>

    注意事项 --critical-load type: Array; default: Threads_running=50 Examine SHOW GLOBAL STATUS after every chunk, and abort if the load is too high. The option accepts a comma-separated list of MySQL status variables and thresholds. An optional =MAX_VALUE (or :MAX_VALUE) can follow each variable. If not given, the tool determines a threshold by examining the current value at startup and doubling it. See --max-load for further details. These options work similarly, except that this option will abort the tool’s operation instead of pausing it,  and the default value is computed differently if you specify no threshold.  The reason for this option is as a safety check in case the triggers on the  original table add so much load to the server that it causes downtime.  There is probably no single value of Threads_running that is wrong for  every server, but a default of 50 seems likely to be unacceptably high  for most servers, indicating that the operation should be canceled immediately. 大致的意思如下: 每次chunk操作前后,会根据show global status统计指定的状态量的变化,默认是统计Thread_running。 目的是为了安全,防止原始表上的触发器引起负载过高。这也是为了防止在线DDL对线上的影响。 超过设置的阀值,就会终止操作,在线DDL就会中断。提示的异常如上报错信息。

    还有一个--max-load 参数也要考虑 --max-load type: Array; default: Threads_running=25 Examine SHOW GLOBAL STATUS after every chunk, and pause if any status variables are higher than their thresholds. The option accepts a comma-separated list of MySQL status variables. An optional =MAX_VALUE (or :MAX_VALUE) can follow each variable. If not given, the tool determines a threshold by examining the current value and increasing it by 20%. For example, if you want the tool to pause when Threads_connected gets too high, you can specify “Threads_connected”,  and the tool will check the current value when it starts working and add 20% to that value. If the current value is 100,  then the tool will pause when Threads_connected exceeds 120, and resume working when it is below 120 again. If you want to  specify an explicit threshold, such as 110, you can use either “Threads_connected:110” or “Threads_connected=110”. The purpose of this option is to prevent the tool from adding too much load to the server. If the data-copy queries are intrusive, or if they cause lock waits, then other queries on the server will tend to block and queue. This will typically cause Threads_running to increase, and the tool can detect that by running SHOW GLOBAL STATUS immediately after each query finishes. If you specify a threshold for this variable, then you can instruct the tool to wait until queries are running normally again. This will not prevent queueing, however; it will only give the server a chance to recover from the queueing. If you notice queueing, it is best to decrease the chunk time. --max-load 选项定义一个阀值,在每次chunk操作后,查看show global status状态值是否高于指定的阀值。

    
    

    注意这个参数不会像--critical-load终止操作,而只是暂停操作。当status值低于阀值时,则继续往下操作。

    
    

    最好加上--max-load="Threads_running=100" --critical-load="Threads_running=200"。

    --chunk-size       type: size; default: 1000

    
    

               Number of rows to select for each chunk copied.  Allowable suffixes            are k, M, G.

             This option can override the default behavior, which is to adjust 
    chunk size dynamically to try to make chunks run in exactly           
    "--chunk-time" seconds.  When this option isn’t set explicitly, its           
    default value is used as a starting point, but after that, the tool           
    ignores this option’s value.  If you set this option explicitly,           
    however, then it disables the dynamic adjustment behavior and tries           
    to make all chunks exactly the specified number of rows.
             There is a subtlety: if the chunk index is not unique, then it’s            
    possible that chunks will be larger than desired. For example, if a           
    table is chunked by an index that contains 10,000 of a given value,           
    there is no way to write a WHERE clause that matches only 1,000 of           
    the values, and that chunk will be at least 10,000 rows large.           
    Such a chunk will probably be skipped because of
      "--chunk-size-limit".
    
    
    结论:
    
    有大查询一样要等待MDL锁,跟官方DDL比,修改列这个情况不锁表,可以DML,因为是按chunk来拷贝数据可以比较平稳的运行,IO比较均衡,就是有个表复制的情况,也就是全表select,会冲刷innodb_buffer_poo,热数据给冲刷掉,IO有压力,也不建议在繁忙的线上使用。
  • 相关阅读:
    Oracle创建表空间、创建用户以及授权、查看权限
    zf2 数据库连接
    ZF2.0用户向导 —— 6. 数据库及模型(Models)
    zf2配置
    zend framework2使用教程【一】安装
    config/application.config.php
    zf2\config\application.config.php
    zf2 数据库album demo
    albumController.php
    登录
  • 原文地址:https://www.cnblogs.com/LMySQL/p/5025868.html
Copyright © 2011-2022 走看看