pt-osc原理

1、检查设置环境

测试db是否可连通,并且验证database是否存在
SET SESSION innodb_lock_wait_timeout=1 //InnoDB事务等待行锁的超时时间
SET SESSION lock_wait_timeout=60 //设置获取元数据琐超时为60s
SET SESSION wait_timeout=10000 //交互超时时间
权限验证 show grants for current_user()

2、创建临时表_tablename_new并修改临时表结构

3、在源表创建三个触发器

--创建delete触发器
CREATE TRIGGER `pt_osc_darren_t_user_del` AFTER DELETE ON `darren`.`t_user` FOR EACH ROW DELETE IGNORE FROM `darren`.`_t_user_new`
WHERE `darren`.`_t_user_new`.`id` <=> OLD.`id` --创建update触发器
CREATE TRIGGER `pt_osc_darren_t_user_upd` AFTER UPDATE ON `darren`.`t_user` FOR EACH ROW
BEGIN
DELETE IGNORE FROM `darren`.`_t_user_new` WHERE !(OLD.`id` <=> NEW.`id`) AND `darren`.`_t_user_new`.`id` <=> OLD.`id`;
REPLACE INTO `darren`.`_t_user_new` (`id`, `name`, `phone`, `gender`, `type`, `birth`, `is_delete`, `c1`, `c2`, `c3`)
VALUES (NEW.`id`, NEW.`name`, NEW.`phone`, NEW.`gender`, NEW.`type`, NEW.`birth`, NEW.`is_delete`, NEW.`c1`, NEW.`c2`, NEW.`c3`);
END; --创建insert触发器
CREATE TRIGGER `pt_osc_darren_t_user_ins` AFTER INSERT ON `darren`.`t_user`
FOR EACH ROW
REPLACE INTO `darren`.`_t_user_new` (`id`, `name`, `phone`, `gender`, `type`, `birth`, `is_delete`, `c1`, `c2`, `c3`)
VALUES (NEW.`id`, NEW.`name`, NEW.`phone`, NEW.`gender`, NEW.`type`, NEW.`birth`, NEW.`is_delete`, NEW.`c1`, NEW.`c2`, NEW.`c3`)

4、开始迁移数据:

   1)估算表的大小,仅仅用于计算进度:EXPLAIN SELECT * FROM `darren`.`t_user` WHERE 1=1;
2)最小值:SELECT `id` FROM `darren`.`t_user` FORCE INDEX(`PRIMARY`) ORDER BY `id` LIMIT 1;
3) 根据chunk-size,计算本次chunk最大值及下一个chunk开始值:SELECT `id` FROM `darren`.`t_user` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= '1'))
ORDER BY `id` LIMIT 2, 2;
4) 插入数据:
INSERT LOW_PRIORITY IGNORE INTO `darren`.`_t_user_new` (`id`, `name`, `phone`, `gender`, `type`, `birth`, `is_delete`, `c1`, `c2`, `c3`)
SELECT `id`, `name`, `phone`, `gender`, `type`, `birth`, `is_delete`, `c1`, `c2`, `c3` FROM `darren`.`t_user` FORCE INDEX(`PRIMARY`)
WHERE ((`id` >= '1')) AND ((`id` <= '3')) LOCK IN SHARE MODE /*pt-online-schema-change 4670 copy nibble*/
5)开始下一轮迭代......
6) 何时copy结束?
根据SELECT `id` FROM `darren`.`t_user` FORCE INDEX(`PRIMARY`) WHERE ((`id` >= '1')) ORDER BY `id` LIMIT 2, 2;
如果获取不到下一个chunk开始值,视为表copy完成,相关源码片段如下:
sub _get_bounds {
my ($self) = @_; if ( $self->{one_nibble} ) {
if ( $self->{resume} ) {
$self->{no_more_boundaries} = 1;
}
return;
} my $dbh = $self->{Cxn}->dbh(); $self->{first_lower} = $dbh->selectrow_arrayref($self->{first_lb_sql});
PTDEBUG && _d('First lower boundary:', Dumper($self->{first_lower})); if ( my $nibble = $self->{resume} ) {
if ( defined $nibble->{lower_boundary}
&& defined $nibble->{upper_boundary} ) {
my $sth = $dbh->prepare($self->{resume_lb_sql});
my @ub = split ',', $nibble->{upper_boundary};
PTDEBUG && _d($sth->{Statement}, 'params:', @ub);
$sth->execute(@ub);
$self->{next_lower} = $sth->fetchrow_arrayref();
$sth->finish();
}
}
else {
$self->{next_lower} = $self->{first_lower};
}
PTDEBUG && _d('Next lower boundary:', Dumper($self->{next_lower})); if ( !$self->{next_lower} ) {
PTDEBUG && _d('At end of table, or no more boundaries to resume');
$self->{no_more_boundaries} = 1; $self->{last_upper} = $dbh->selectrow_arrayref($self->{last_ub_sql});
PTDEBUG && _d('Last upper boundary:', Dumper($self->{last_upper}));
} return;
}

5、rename表

ANALYZE TABLE `darren`.`_t_user_new`;
RENAME TABLE `darren`.`t_user` TO `darren`.`_t_user_old`, `darren`.`_t_user_new` TO `darren`.`t_user`;

6、清理战场

DROP TABLE IF EXISTS `darren`.`_t_user_old`
DROP TRIGGER IF EXISTS `darren`.`pt_osc_darren_t_user_del`
DROP TRIGGER IF EXISTS `darren`.`pt_osc_darren_t_user_upd`
DROP TRIGGER IF EXISTS `darren`.`pt_osc_darren_t_user_ins`
SHOW TABLES FROM `darren` LIKE '_t_user_new'

【copy rows和触发器实现数据迁移和增量写入原理:】

DDL功能 映射执行的SQL语句
insert触发器 replace into
update触发器 delete ignore + replace into
delete触发器 delete ignore
copy rows insert ignore into

pt-osc原理的更多相关文章

  1. pt-online-schema-change 最佳实践(转)

    pt的详细步骤 Step 1: Create the new table. Step 2: Alter the new, empty table. This should be very quick, ...

  2. MySQL OSC(在线更改表结构)原理

    1 OSC介绍 在我们的数据库操作中,更改表结构是一个常见的操作,而当我们的表数据量非常大时,我们更改表结构的时间是非 常的长,并且在跟改期间,会生成一个互斥锁,阻塞对整个表的所有操作,这样,对于我们 ...

  3. OSC的原理

    OSC是Online Schema Change简写,即在线架构改变.其实现步骤: 1. init,即初始化阶段,会对创建的表做一些验证工作,如检查表是否有主键,是否存在触发器或者外键等.2. cre ...

  4. iptables防火墙原理详解+mysql pt工具

    http://seanlook.com/2014/02/23/iptables-understand/

  5. 【算法】(查找你附近的人) GeoHash核心原理解析及代码实现

    本文地址 原文地址 分享提纲: 0. 引子 1. 感性认识GeoHash 2. GeoHash算法的步骤 3. GeoHash Base32编码长度与精度 4. GeoHash算法 5. 使用注意点( ...

  6. 玩转Windows服务系列——Debug、Release版本的注册和卸载,及其原理

    Windows服务Debug版本 注册 Services.exe -regserver 卸载 Services.exe -unregserver Windows服务Release版本 注册 Servi ...

  7. 【系统篇】从int 3探索Windows应用程序调试原理

    探索调试器下断点的原理 在Windows上做开发的程序猿们都知道,x86架构处理器有一条特殊的指令——int 3,也就是机器码0xCC,用于调试所用,当程序执行到int 3的时候会中断到调试器,如果程 ...

  8. java web学习总结(十四) -------------------JSP原理

    一.什么是JSP? JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP这门技术的最大的特点在于,写jsp就像在写h ...

  9. 深入解析SQL Server并行执行原理及实践(上)

    在成熟领先的企业级数据库系统中,并行查询可以说是一大利器,在某些场景下他可以显著的提升查询的相应时间,提升用户体验.如SQL Server, Oracle等, Mysql目前还未实现,而Postgre ...

  10. 深入解析SQL Server并行执行原理及实践(下)

    谈完并行执行的原理,咱们再来谈谈优化,到底并行执行能给我们带来哪些好处,我们又应该注意什么呢,下面展开. Amdahl’s  Law 再谈并行优化前我想有必要谈谈阿姆达尔定律,可惜老爷子去年已经驾鹤先 ...

随机推荐

  1. ios 怎样将不支持ARC的文件设为支持ARC的--JSON

    怎样将不支持ARC的文件设为支持ARC的 双击须要改动的文件加上一句话就可以 -fno-objc-arc

  2. java中Comparator的用法 -- 实现集合和数组排序

    在java中,如果要对集合对象或数组对象进行排序,需要实现Comparator接口以达到我们想要的目标. 接下来我们模拟下在集合对象中对日期属性进行排序 一.实体类Step package com.l ...

  3. opencv2.4.9+VS2010配置

    opencv2.4.9 https://pan.baidu.com/s/15b5bEY65R4CptayEYVAG4A 安装包路径:D:\文件及下载相关\文档\Tencent Files\845235 ...

  4. PHP 代码规范

    FIG制定的 PHP 规范,简称 PSR,是 PHP 开发的事实标准.FIG 是 Framework Interoperability Group (框架可互用小组) 的缩写,由几位开源框架的开发者成 ...

  5. linux 项目自动部署脚本

    1.使用maven获取源码部署,并可替换配置文件(金融数据分析平台) #!/bin/bash#设置变量cd /home#停止tomcatecho "开始停止tomcat..." p ...

  6. 开源平台ghost博客系统

    http://docs.ghost.org/installation/windows/ 不会安装的童鞋可以到这里论坛看看 此教程只说windows下的安装: mac的安装教程请移步这里 mac版安装 ...

  7. 如何修改Myeclipse的JSP模板

    先找到MyEclipse的安装目录, 再找到myeclipse/eclipse/plugins/com.genuitec.eclipse.wizards_5.1.0/templates/jsp (co ...

  8. php 解决上传中文文件名时出现乱码的问题

    有时候上传文件是中文的文件名会出现乱码, 可以在移动文件时使用icov('utf-8','gb2312',filename)转换 代码: <?php //header('Content-type ...

  9. You must reset your password using ALTER USER

    mac mysql error You must reset your password using ALTER USER statement before executing this statem ...

  10. Neutron相关资料链接

    1.OpenStack Neturon 官方文档: https://docs.openstack.org/mitaka/networking-guide/ 2.Neturon理解系列文章: http: ...