pt-online-schema-change的实现原理
pt-online-schema-change用于MySQL的在线DDL。
下面结合官方文档和general log来分析其实现原理。
测试表
mysql> show create table t2\G
*************************** 1. row ***************************
Table: t2
Create Table: CREATE TABLE `t2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1005764 DEFAULT CHARSET=utf8
1 row in set (0.19 sec)
该表中只有1列,id,自增主键。
其中,表中已经存在一部分数据
mysql> select count(*) from t2;
+----------+
| count(*) |
+----------+
| 1005763 |
+----------+
1 row in set (0.31 sec)
利用pt-online-schema-change对该表新增一列
# pt-online-schema-change --execute --alter "ADD COLUMN c1 DATETIME" D=test,t=t2
Found 2 slaves:
test
hbase
Will check slave lag on:
test
hbase
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`.`t2`...
Creating new table...
CREATE TABLE `test`.`_t2_new` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1005764 DEFAULT CHARSET=utf8
Created new table test._t2_new OK.
Waiting forever for new table `test`.`_t2_new` to replicate to test...
Altering new table...
ALTER TABLE `test`.`_t2_new` ADD COLUMN c1 DATETIME
Altered `test`.`_t2_new` OK.
2016-11-21T12:49:18 Creating triggers...
CREATE TRIGGER `pt_osc_test_t2_del` AFTER DELETE ON `test`.`t2` FOR EACH ROW DELETE IGNORE FROM `test`.`_t2_new` WHERE `test`.`_t2_ne
w`.`id` <=> OLD.`id`CREATE TRIGGER `pt_osc_test_t2_upd` AFTER UPDATE ON `test`.`t2` FOR EACH ROW REPLACE INTO `test`.`_t2_new` (`id`) VALUES (NEW.`id`)
CREATE TRIGGER `pt_osc_test_t2_ins` AFTER INSERT ON `test`.`t2` FOR EACH ROW REPLACE INTO `test`.`_t2_new` (`id`) VALUES (NEW.`id`)
2016-11-21T12:49:18 Created triggers OK.
2016-11-21T12:49:18 Copying approximately 1005075 rows...
INSERT LOW_PRIORITY IGNORE INTO `test`.`_t2_new` (`id`) SELECT `id` FROM `test`.`t2` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ?)) AND (
(`id` <= ?)) LOCK IN SHARE MODE /*pt-online-schema-change 2352 copy nibble*/SELECT /*!40001 SQL_NO_CACHE */ `id` FROM `test`.`t2` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= ?)) ORDER BY `id` LIMIT ?, 2 /*next chun
k boundary*/Copying `test`.`t2`: 40% 00:44 remain
Copying `test`.`t2`: 82% 00:12 remain
2016-11-21T12:50:31 Copied rows OK.
2016-11-21T12:50:31 Analyzing new table...
2016-11-21T12:50:32 Swapping tables...
RENAME TABLE `test`.`t2` TO `test`.`_t2_old`, `test`.`_t2_new` TO `test`.`t2`
2016-11-21T12:50:35 Swapped original and new tables OK.
2016-11-21T12:50:35 Dropping old table...
DROP TABLE IF EXISTS `test`.`_t2_old`
2016-11-21T12:50:36 Dropped old table `test`.`_t2_old` OK.
2016-11-21T12:50:36 Dropping triggers...
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_t2_del`;
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_t2_upd`;
DROP TRIGGER IF EXISTS `test`.`pt_osc_test_t2_ins`;
2016-11-21T12:50:36 Dropped triggers OK.
Successfully altered `test`.`t2`.
查看general log中的输出
161017 11:22:56 1052 Connect root@localhost on test
1052 Query set autocommit=1
1052 Query SHOW VARIABLES LIKE 'innodb\_lock_wait_timeout'
1052 Query SET SESSION innodb_lock_wait_timeout=1
1052 Query SHOW VARIABLES LIKE 'lock\_wait_timeout'
1052 Query SET SESSION lock_wait_timeout=60
1052 Query SHOW VARIABLES LIKE 'wait\_timeout'
1052 Query SET SESSION wait_timeout=10000
1052 Query SELECT @@SQL_MODE
1052 Query SET @@SQL_QUOTE_SHOW_CREATE = 1/*!40101, @@SQL_MODE='NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION'*/
1052 Query SELECT @@server_id /*!50038 , @@hostname*/
1053 Connect root@localhost on test
1053 Query set autocommit=1
1053 Query SHOW VARIABLES LIKE 'innodb\_lock_wait_timeout'
1053 Query SET SESSION innodb_lock_wait_timeout=1
1053 Query SHOW VARIABLES LIKE 'lock\_wait_timeout'
1053 Query SET SESSION lock_wait_timeout=60
1053 Query SHOW VARIABLES LIKE 'wait\_timeout'
1053 Query SET SESSION wait_timeout=10000
1053 Query SELECT @@SQL_MODE
1053 Query SET @@SQL_QUOTE_SHOW_CREATE = 1/*!40101, @@SQL_MODE='NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION'*/
1053 Query SELECT @@server_id /*!50038 , @@hostname*/
上述主要是设置会话的变量信息,包括innodb_lock_wait_timeout,wait_timeout和SQL_QUOTE_SHOW_CREATE。
1052 Query SHOW VARIABLES LIKE 'wsrep_on'
1052 Query SHOW VARIABLES LIKE 'version%'
1052 Query SHOW ENGINES
1052 Query SHOW VARIABLES LIKE 'innodb_version'
1052 Query SHOW VARIABLES LIKE 'innodb_stats_persistent'
1052 Query SELECT @@SERVER_ID
1052 Query SHOW GRANTS FOR CURRENT_USER()
1052 Query SHOW FULL PROCESSLIST
1052 Query SHOW SLAVE HOSTS
1052 Query SHOW GLOBAL STATUS LIKE 'Threads_running'
1052 Query SHOW GLOBAL STATUS LIKE 'Threads_running'
1052 Query SELECT CONCAT(@@hostname, @@port)
1052 Query SHOW TABLES FROM `test` LIKE 't2'
1052 Query SHOW TRIGGERS FROM `test` LIKE 't2'
1052 Query /*!40101 SET @OLD_SQL_MODE := @@SQL_MODE, @@SQL_MODE := '', @OLD_QUOTE := @@SQL_QUOTE_SHOW_CREATE, @@SQL_QUOTE_SHOW_CREATE := 1 */
1052 Query USE `test`
1052 Query SHOW CREATE TABLE `test`.`t2`
1052 Query /*!40101 SET @@SQL_MODE := @OLD_SQL_MODE, @@SQL_QUOTE_SHOW_CREATE := @OLD_QUOTE */
1052 Query EXPLAIN SELECT * FROM `test`.`t2` WHERE 1=1
1052 Query SELECT table_schema, table_name FROM information_schema.key_column_usage WHERE referenced_table_schema='test' AND referenced_table_name='t2'
1052 Query SHOW VARIABLES LIKE 'wsrep_on'
1052 Query /*!40101 SET @OLD_SQL_MODE := @@SQL_MODE, @@SQL_MODE := '', @OLD_QUOTE := @@SQL_QUOTE_SHOW_CREATE, @@SQL_QUOTE_SHOW_CREATE := 1 */
解释:
1. 查看参数变量,当前用户的权限,slave的信息,会话变量
2. 确认t2是否存在,t2上是否有触发器
3. 查看执行计划
4. 查看是否t2表是否被其它表外键关联。
39 Query USE `test`
39 Query SHOW CREATE TABLE `test`.`t2`
39 Query /*!40101 SET @@SQL_MODE := @OLD_SQL_MODE, @@SQL_QUOTE_SHOW_CREATE := @OLD_QUOTE */
39 Query CREATE TABLE `test`.`_t2_new` (
`id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1005764 DEFAULT CHARSET=utf8
161121 12:49:18 39 Query ALTER TABLE `test`.`_t2_new` ADD COLUMN c1 DATETIME
39 Query /*!40101 SET @OLD_SQL_MODE := @@SQL_MODE, @@SQL_MODE := '', @OLD_QUOTE := @@SQL_QUOTE_SHOW_CREATE, @@SQL_QUOTE_SHOW_CREATE := 1 */
39 Query USE `test`
39 Query SHOW CREATE TABLE `test`.`_t2_new`
39 Query /*!40101 SET @@SQL_MODE := @OLD_SQL_MODE, @@SQL_QUOTE_SHOW_CREATE := @OLD_QUOTE */
39 Query CREATE TRIGGER `pt_osc_test_t2_del` AFTER DELETE ON `test`.`t2` FOR EACH ROW DELETE IGNORE FROM `test`.`_t2_new` WHERE `test`.`_t2_new`.`id` <=> OLD.`id`
39 Query CREATE TRIGGER `pt_osc_test_t2_upd` AFTER UPDATE ON `test`.`t2` FOR EACH ROW REPLACE INTO `test`.`_t2_new` (`id`) VALUES (NEW.`id`)
39 Query CREATE TRIGGER `pt_osc_test_t2_ins` AFTER INSERT ON `test`.`t2` FOR EACH ROW REPLACE INTO `test`.`_t2_new` (`id`) VALUES (NEW.`id`)
解释:
1. 根据目标表结构创建一张新表。
2. 对新表添加字段,可以看出pt-online-shema-change对表结构进行变更依赖的还是MySQL自身的Online DDL。
3. 针对目标表创建三个触发器,DELETE,UPDATE和INSERT,因为REPLACE操作只有在主键或唯一索引存在的情况下才有意义,这也就解释了为什么目标表上要有主键或唯一索引。
39 Query EXPLAIN SELECT * FROM `test`.`t2` WHERE 1=1
39 Query SELECT /*!40001 SQL_NO_CACHE */ `id` FROM `test`.`t2` FORCE INDEX(`PRIMARY`) ORDER BY `id` LIMIT 1 /*first lower boundary*/
39 Query SELECT /*!40001 SQL_NO_CACHE */ `id` FROM `test`.`t2` FORCE INDEX (`PRIMARY`) WHERE `id` IS NOT NULL ORDER BY `id` LIMIT 1 /*key_len*/
39 Query EXPLAIN SELECT /*!40001 SQL_NO_CACHE */ * FROM `test`.`t2` FORCE INDEX (`PRIMARY`) WHERE `id` >= '' /*key_len*/
39 Query EXPLAIN SELECT /*!40001 SQL_NO_CACHE */ `id` FROM `test`.`t2` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= '')) ORDER BY `id` LIMIT 999, 2 /*next chunk boundary*/
39 Query SELECT /*!40001 SQL_NO_CACHE */ `id` FROM `test`.`t2` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= '')) ORDER BY `id` LIMIT 999, 2 /*next chunk boundary*/
39 Query EXPLAIN SELECT `id` FROM `test`.`t2` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= '')) AND ((`id` <= '')) LOCK IN SHARE MODE /*explain pt-online-schema-change 2352 copy nibble*/
39 Query INSERT LOW_PRIORITY IGNORE INTO `test`.`_t2_new` (`id`) SELECT `id` FROM `test`.`t2` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= '')) AND ((`id` <= '')) LOCK IN SHARE MODE /*pt-online-schema-change 2352 copy nibble*/
39 Query SHOW WARNINGS
39 Query SELECT @@SERVER_ID
39 Query SHOW GRANTS FOR CURRENT_USER()
39 Query SHOW FULL PROCESSLIST
39 Query SELECT @@SERVER_ID
39 Query SHOW GRANTS FOR CURRENT_USER()
39 Query SHOW FULL PROCESSLIST
161121 12:49:20 39 Query SELECT 'pt-online-schema-change keepalive'
161121 12:49:21 39 Query SELECT @@SERVER_ID
39 Query SHOW GRANTS FOR CURRENT_USER()
39 Query SHOW FULL PROCESSLIST
39 Query SHOW GLOBAL STATUS LIKE 'Threads_running'
39 Query EXPLAIN SELECT /*!40001 SQL_NO_CACHE */ `id` FROM `test`.`t2` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= '')) ORDER BY `id` LIMIT 28516, 2 /*next chunk boundary*/
39 Query SELECT /*!40001 SQL_NO_CACHE */ `id` FROM `test`.`t2` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= '')) ORDER BY `id` LIMIT 28516, 2 /*next chunk boundary*/
39 Query EXPLAIN SELECT `id` FROM `test`.`t2` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= '')) AND ((`id` <= '')) LOCK IN SHARE MODE /*explain pt-online-schema-change 2352 copy nibble*/
39 Query INSERT LOW_PRIORITY IGNORE INTO `test`.`_t2_new` (`id`) SELECT `id` FROM `test`.`t2` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= '')) AND ((`id` <= '')) LOCK IN SHARE MODE /*pt-online-schema-change 2352 copy nibble*/
解释:
上述输出只包含两个chunk的选择。其它chunk的选择基本相同。
1. SHOW GLOBAL STATUS LIKE 'Threads_running'用于监控当前的系统负载。
2. 可以看出pt-online-schema-change是以chunk为单位进行目标表数据的拷贝。
3. 在拷贝的过程中,对目标表的相关记录加了共享锁,此时,会堵塞客户端对这些记录的DML操作。
39 Query ANALYZE TABLE `test`.`_t2_new` /* pt-online-schema-change */
161121 12:50:32 39 Query RENAME TABLE `test`.`t2` TO `test`.`_t2_old`, `test`.`_t2_new` TO `test`.`t2`
161121 12:50:35 39 Query DROP TABLE IF EXISTS `test`.`_t2_old`
161121 12:50:36 39 Query DROP TRIGGER IF EXISTS `test`.`pt_osc_test_t2_del`
39 Query DROP TRIGGER IF EXISTS `test`.`pt_osc_test_t2_upd`
39 Query DROP TRIGGER IF EXISTS `test`.`pt_osc_test_t2_ins`
39 Query SHOW TABLES FROM `test` LIKE '\_t2\_new'
161121 12:50:37 40 Quit
39 Quit
解释:
1. 在完成数据的拷贝后,会对新表执行ANALYZE操作,这样,可及时更新新表的统计信息。
官档的解释如下:
This circumvents a potentially serious issue related to InnoDB optimizer statistics. If the table being alerted is
busy and the tool completes quickly, the new table will not have optimizer statistics after being swapped. This
can cause fast, index-using queries to do full table scans until optimizer statistics are updated (usually after 10
seconds). If the table is large and the server very busy, this can cause an outage.
2. 对目标表和新表进行RENAME操作。
3. 删除原来的目标表
4. 删除触发器。
pt-online-schema-change的实现原理的更多相关文章
- schema change + ogg 变更手册
Check OGG until no data queuing in replication process:testRO:a)login test5 –l oggmgrb)oggc)#ggsci ...
- Online Schema Change for MySQL
It is great to be able to build small utilities on top of an excellent RDBMS. Thank you MySQL. This ...
- AppBoxFuture(四). 随需而变-Online Schema Change
需求变更是信息化过程中的家常便饭,而在变更过程中如何尽可能小的影响在线业务是比较头疼的事情.举个车联网监控的例子:原终端设备上传车辆的经纬度数据,新的终端设备支持同时上传速度数据,而旧的车辆状态表 ...
- Online, Asynchronous Schema Change in F1
F1: A Distributed SQL Database That Scales http://disksing.com/understanding-f1-schema-change ma ...
- MySQL OSC(在线更改表结构)原理
1 OSC介绍 在我们的数据库操作中,更改表结构是一个常见的操作,而当我们的表数据量非常大时,我们更改表结构的时间是非 常的长,并且在跟改期间,会生成一个互斥锁,阻塞对整个表的所有操作,这样,对于我们 ...
- Online Schema Upgrade in MySQL Galera Cluster using TOI Method
http://severalnines.com/blog/online-schema-upgrade-mysql-galera-cluster-using-toi-method As a fo ...
- OSC的原理
OSC是Online Schema Change简写,即在线架构改变.其实现步骤: 1. init,即初始化阶段,会对创建的表做一些验证工作,如检查表是否有主键,是否存在触发器或者外键等.2. cre ...
- Schema 与数据类型优化
这是<高性能 MySQL(第三版)>第四章<Schema 与数据类型优化>的读书笔记. 1. 选择优化的数据类型 数据类型的选择原则: 越小越好:选择满足需求的最小类型.注意, ...
- MongoDB 变更流(Change Stream)介绍
1. 什么是Change Stream Change Stream 是MongoDB用于实现变更追踪的解决方案,类似于关系数据库的触发器,但原理不完全相同: | | Change Stream | 触 ...
- iDB是如何运转的 一
郑昀 创建于2015/12/2 最后更新于2015/12/4 关键词:数据库,MySQL,自动化运维,DDL,DML,SQL审核,备份,回滚,Inception,osc 提纲: 普通DBA和文艺DBA ...
随机推荐
- Mongodb集群——master/slave
集群的配置 (本测试放于同一台机器进行配置,所以IP地址一样,如果是在不同的服务器上更换IP便可以) 1.目录结构 拷贝两份mongodb到/home/scotte.ye/mongo1 ...
- React 组件 API
React 组件 API 在本章节中我们将讨论 React 组件 API.我们将讲解以下7个方法: 设置状态:setState 替换状态:replaceState 设置属性:setProps 替换属性 ...
- Python基础一数据类型之数字类型
摘要: python基础一中提到了数据类型,这里主要讲解的是数字类型. 数字类型: 1,整型 2,长整型 3,浮点型 4,复数型 1,整型(int) 定义a = 1 通过type函数查看数据类型,整型 ...
- sqlserver tablediff 实用工具
tablediff 是sqlserver自带的实用工具 sqlserver 2012 在110目录下,sqlserver2008在100目录下 官方参考文档如下: https://docs.micro ...
- Sql server 使用drop database 语句,无法删除正在使用的数据库的解决办法
使用DROP DATABASE 删除数据库 显示“无法删除数据库 ,因为该数据库当前正在使用. 解决办法:在删除某一个数据库(下例中的“DB1”数据库)前,强制kill掉该数据库上的所有数据库连接. ...
- pycrypto加解密
下载pycrypto:http://www.voidspace.org.uk/python/modules.shtml#pycrypto (Windows版本) from Crypto.Cipher ...
- Linux nmap命令详解
nmap,也就是Network Mapper,是Linux下的网络扫描和嗅探工具包. nmap是在网络安全渗透测试中经常会用到的强大的扫描器.功能之强大,不言而喻.下面介绍一下它的几种扫描命令.具体的 ...
- IIS 部署SSL证书
1.导入证书 打开IIS服务管理器,点击计算机名称,双击‘服务器证书 双击打开服务器证书后,点击右则的导入 选择证书文件,点击确定 2.站点开启SSL 选择证书文件,点击确定 点击网站下的站点名称,点 ...
- [翻译] 提升树算法的介绍(Introduction to Boosted Trees)
[翻译] 提升树算法的介绍(Introduction to Boosted Trees) 1. 有监督学习的要素 XGBoost 适用于有监督学习问题.在此类问题中,我们使用多特征的训练数据集 \(x ...
- PyQt5---firstwindow
# -*- coding:utf-8 -*- ''' Created on Sep 13, 2018 @author: SaShuangYiBing ''' import sys from PyQt5 ...