本节中,将了解Percona的pt-online-schema-change(pt-osc)工具,该工具用于在DML未阻塞的情况下
执行ALTER TABLE操作。
如何运作
pt-online-schema-change会先创建表的空副本,然后根据需要对其进行修改,再将原始表中的行复制
到这个新表中。复制完成后,它将移除原来的表并将其替换为新的表。默认情况下,它会删除(drop)
原始表。
数据的复制是以小块数据为单位执行,这些数据块的大小可以调整,以便能在指定时间内完成复制。在
复制期间对原始表数据的任何修改都将反映在新表中,因为pt-online-schema-change会在原始表上创
建触发器以更新新表中的相应行。触发器的使用意味着,如果表中已经定义了触发器,pt-online-
schema-change将不起作用。
当pt-online-schema-change将数据复制到新表中时,它使用原子操作RENAME TABLE来同时重命名原
始表和新表。完成此操作后,它将删除原始表。
外键会使这个工具的操作复杂化,并引入额外的风险。当外键指向表时,原子重命名原始表和新表的技
术不起作用。在模式更改完成后,pt-online-schema-change必须更新外键来引用新表。
如何操作
修改列数据类型可以像如下这般操作:

[root@www ~]# pt-online-schema-change D=test1,t=tb01,h=localhost -u root \
> --ask-pass --alter="MODIFY COLUMN address VARCHAR(100)" --alter-foreign-keys-
method=auto \
> --execute
Enter MySQL password:
No slaves found. See --recursion-method if host www.lnmp.com 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
No foreign keys reference `test1`.`tb01`; ignoring --alter-foreign-keys-method.
Altering `test1`.`tb01`...
Creating new table...
Created new table test1._tb01_new OK.
Altering new table...
Altered `test1`.`_tb01_new` OK.
2019-09-23T16:49:48 Creating triggers...
2019-09-23T16:49:48 Created triggers OK.
2019-09-23T16:49:48 Copying approximately 19544 rows...
2019-09-23T16:49:48 Copied rows OK.
2019-09-23T16:49:48 Analyzing new table...
云计算-legolas制作
修改一个带外键的表:employees.employees表(提前增加address列)
然后修改address列类型
2019-09-23T16:49:48 Swapping tables...
2019-09-23T16:49:48 Swapped original and new tables OK.
2019-09-23T16:49:48 Dropping old table...
2019-09-23T16:49:48 Dropped old table `test1`.`_tb01_old` OK.
2019-09-23T16:49:48 Dropping triggers...
2019-09-23T16:49:48 Dropped triggers OK.
Successfully altered `test1`.`tb01`.
mysql> alter table employees add column address tinytext;
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0
[root@www ~]# pt-online-schema-change D=employees,t=employees,h=localhost -u
root --ask-pass --alter="MODIFY COLUMN address VARCHAR(100)" --alter-foreign-
keys-method=auto --execute
Enter MySQL password:
No slaves found. See --recursion-method if host www.lnmp.com 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
Child tables:
`employees`.`dept_manager` (approx. 24 rows)
`employees`.`dept_emp` (approx. 331143 rows)
`employees`.`titles` (approx. 442070 rows)
`employees`.`salaries` (approx. 2838426 rows)
Will automatically choose the method to update foreign keys.
Altering `employees`.`employees`...
Creating new table...
Created new table employees._employees_new OK.
Altering new table...
Altered `employees`.`_employees_new` OK.
2019-09-23T17:03:32 Creating triggers...
2019-09-23T17:03:32 Created triggers OK.
2019-09-23T17:03:32 Copying approximately 299568 rows...
2019-09-23T17:03:36 Copied rows OK.
2019-09-23T17:03:36 Max rows for the rebuild_constraints method: 168690
Determining the method to update foreign keys...
2019-09-23T17:03:36  `employees`.`dept_manager`: 24 rows; can use
rebuild_constraints
2019-09-23T17:03:36  `employees`.`dept_emp`: too many rows: 331143; must use
drop_swap
2019-09-23T17:03:36 Drop-swapping tables...
2019-09-23T17:03:36 Analyzing new table...
2019-09-23T17:03:36 Dropped and swapped tables OK.
Not dropping old table because --no-drop-old-table was specified.
2019-09-23T17:03:36 Dropping triggers...
云计算-legolas制作
可以看到,pt-online-schema-change创建了一个列数据类型为改后的类型的新表,在表中创建了触发
器,将行复制到新表,最后,重命名这张新表。
如果你想修改salaries表,而它已经有触发器,那么会出现错误:
根据提示,需要指定--preserve-triggers选项:
2019-09-23T17:03:36 Dropped triggers OK.
Successfully altered `employees`.`employees`.
[root@www ~]# pt-online-schema-change D=employees,t=salaries,h=localhost -u root
--ask-pass --alter="MODIFY COLUMN salary int" --alter-foreign-keys-method=auto -
-execute
Enter MySQL password:
No slaves found. See --recursion-method if host www.lnmp.com has slaves.
Not checking slave lag because no slaves were found and --check-slave-lag was
not specified.
The table `employees`.`salaries` has triggers but --preserve-triggers was not
specified.
Please read the documentation for --preserve-triggers.
[root@www ~]# pt-online-schema-change D=employees,t=salaries,h=localhost -u root
\
> --ask-pass --alter="MODIFY COLUMN salary int" \
> --alter-foreign-keys-method=auto --execute \
> --preserve-triggers
Enter MySQL password:
No slaves found. See --recursion-method if host www.lnmp.com 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
No foreign keys reference `employees`.`salaries`; ignoring --alter-foreign-keys-
method.
Altering `employees`.`salaries`...
Creating new table...
Created new table employees._salaries_new OK.
Altering new table...
Altered `employees`.`_salaries_new` OK.
2019-09-23T17:32:48 Creating triggers...
2019-09-23T17:32:48 Created triggers OK.
2019-09-23T17:32:48 Copying approximately 2838426 rows...
2019-09-23T17:33:12 Copied rows OK.
2019-09-23T17:33:12 Adding original triggers to new table.
2019-09-23T17:33:12 Analyzing new table...
2019-09-23T17:33:12 Swapping tables...
2019-09-23T17:33:12 Swapped original and new tables OK.
2019-09-23T17:33:12 Dropping old table...
2019-09-23T17:33:12 Dropped old table `employees`.`_salaries_old` OK.
2019-09-23T17:33:12 Dropping triggers...
2019-09-23T17:33:12 Dropped triggers OK.
云计算-legolas制作
如果此数据库服务器有从库,那么pt-online-schema-change在将现有表复制到新表时可能会使从库产
生延时。为了避免这种情况,可以指定--check-slave-lag(默认是启用的);它会暂停数据的复制,直到此
复制的延时小于--max-lag(默认为1秒)。可以通过传递--max-lag选项来指定--max-lag的值。
如果想确保从库的延时不会超过10秒,可以设置--max-lag=10。
pt-online-schema-change仅在有主键或唯一键时才起作用,否则其执行将失败,并显示以下错误:
因此,如果表没有唯一键,则不能使用pt-online-schema-change工具。
Successfully altered `employees`.`salaries`.
The new table `employees`.`_employee_names_new` does not have a PRIMARY KEY or a
unique index which is required for the DELETE trigger.
云计算-legolas

使用在线模式更改工具修改表相关推荐

  1. CockroachDB学习笔记——[译]在CockroachDB中如何让在线模式更改成为可能

    原文链接:https://www.cockroachlabs.com/blog/how-online-schema-changes-are-possible-in-cockroachdb/ 原作者: ...

  2. oracle 11g 通过在线重定义方式修改表结构

    今天因为要对一套数据库的数据抽取进行io优化,希望通过修改表结构将抽取io降下来,因为抽取只针对标签HAVE_FLAG为"0"的值进行抽取,抽取之后更新HAVE_FLAG为其他值, ...

  3. mysql在线修改表结构大数据表的风险与解决办法归纳

    整理这篇文章的缘由: 互联网应用会频繁加功能,修改需求.那么表结构也会经常修改,加字段,加索引.在线直接在生产环境的表中修改表结构,对用户使用网站是有影响. 以前我一直为这个问题头痛.当然那个时候不需 ...

  4. mysql 主从 索引_Mysql繁忙主从库在线修改表结构与添加索引问题

    本帖最后由 jan_1985 于 2014-1-15 13:28 编辑 Mysql繁忙主从库在线修改表结构与添加索引问题 一直以来,生产情况下都有修改索引和修改字段的需求,但是对锁表引起的访问不便是会 ...

  5. osc mysql_MySQL在线修改表结构pt-osc

    MySQL在线修改表结构pt-osc 重所周知 MySQL的DDL操作操作是相比比较昂贵的.因为MySQL在修改表期间会阻塞任何读写操作. 基本上业务处于瘫痪.如果数据量较大可能需要好几个小时才能完成 ...

  6. SQL 2008中修改表结构提示“阻止保存要求重新创建表的更改”

    [问题] SQL Server 2008 Management Studio 中修改表结构的时候,提示"阻止保存要求重新创建表的更改" [解决方法] 1.  打开SQL Serve ...

  7. pt-online-schema-change 在线修改表结构

    1. 参数 参数 默认值 说明 --host=xxx --user=xxx --password=xxx 连接实例信息,缩写-h xxx -u xxx -p xxx,密码可以使用参数--ask-pas ...

  8. mysql大表修改表名原理_MySQL修改大表工具pt-online-schema-change原理

    MySQL修改大表工具pt-online-schema-change的使用限制: 1).如果修改表有外键,除非使用 –alter-foreign-keys-method 指定特定的值,否则工具不予执行 ...

  9. mysql修改表结构大表_在线修改MySQL大表的表结构

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

最新文章

  1. mysql binlog2sql闪回数据
  2. BM16 删除有序链表中重复的元素-II
  3. An RFC destination could not be specified for the logical system QI3CLNT504
  4. 产品设计 产品经理 喜欢的网站
  5. squid代理服务器在企业网中的应用
  6. python判断进程是否存在
  7. mysql5.7的存储过程_MySql5.7命令笔记(三)mysql存储过程命令
  8. apache反向代理实战
  9. 温故知新----标签的语义化
  10. 软件工程本科毕业设计题目推荐?软件工程毕设题目大全
  11. 关于ip报文校验和一些思考
  12. 临近秋招实习,科普一下“内推”的利优势
  13. 微信小程序 -对应的服务器证书无效
  14. 【css】用css的方法来画一个三角形
  15. 启动到APP的设置页,小米手机自启动管理页,小米手机APP权限管理页
  16. 人工智能的主要应用领域
  17. boost::asio::tcp
  18. 武汉微软认证考点及考试流程 与 微软认证考试流程
  19. C/C++编程学习 - 第9周 ③ 整理药名
  20. APISpace 未来7天生活指数API接口 免费好用

热门文章

  1. C语言:for循环用法 完全攻略
  2. 【计算机网络】期末课程设计 ENSP组网综合实验(附工程文件)
  3. Linux 终端生存指南
  4. Go :运行linkx测试(附完整源码)
  5. linux文件操作和目录操作,Linux基础操作1——文件和目录
  6. Anaconda3 偏好设置
  7. Python3.x爬虫教程:爬网页、爬图片、自动登录
  8. AI遮天传 ML-SVM
  9. RNA 27 SCI文章中转录因子结合motif富集到调控网络 (RcisTarget)
  10. ab压力测试并发测试基于HTTP