Percona-Toolkit 之 pt-archiver 删除历史数据
pt-archiver是Percona-Toolkit工具集中的一个组件,是一个主要用于对MySQL表数据进行归档和清除工具。它可以将数据归档到另一张表或者是一个文件中。pt-archiver在清除表数据的过程中并不会影响OLTP事务的查询性能。对于数据的归档,它可以归档到另一台服务器上的另一张表,也可归档到一个文件中,文件可以用LOAD DATA INFILE进行数据装载,这个功能其实就类似是表历史数据的增量删除。
对线上历史数据的归档,本人直接用mysqldump后还原,但是归档后删除历史数据,通过pt-archiver感觉比写个shell脚本方便很多。
pt-archiver --help 参数说明主要介绍删除历史数据能用到的。
1、--progress 每多少行打印进度信息
2、--limit 限制select返回的行数
3、--sleep 指定select语句休眠时间
4、--txn-size 指定多少行提交一次事务
5、--bulk-delete 用单个DELETE语句批量删除每个行块。该语句删除块的第一行和最后一行之间的每一行,隐含--commit-each
6、--dry-run 打印查询,不做任何操作后退出
新建表employees_ptarc测试:
CREATE TABLE `employees_ptarc` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`v_int` int(11) DEFAULT NULL,
`v_string` varchar(50) DEFAULT NULL,
`s_string` char(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
通过存储过程造数据:
delimiter $$
CREATE PROCEDURE i_employees_ptarc (IN row_num INT)
BEGIN
DECLARE i INT DEFAULT 0 ;
WHILE i < row_num DO
INSERT INTO employees_ptarc (v_int, v_string, s_string)
VALUES
(
floor(1 + rand() * 1000000),
substring(
MD5(RAND()),
1,
floor(1 + rand() * 20)
),
substring(MD5(RAND()), 1, 20)
) ;
SET i = i + 1 ;
END
WHILE ; END$$
delimiter ;
call i_employees_ptarc(400000);
# pt-archiver --source h=localhost,P=7777,u=root,p='Asd.123@#',D=yoon,t=employees_ptarc,A=utf8 --purge --charset=utf8 --where "id < 400000" --progress=100 --txn-size=100 --limit=100 --bulk-delete --statistics --dry-run
SELECT /*!40001 SQL_NO_CACHE */ `id`,`v_int`,`v_string`,`s_string` FROM `yoon`.`employees_ptarc` FORCE INDEX(`PRIMARY`) WHERE (id < 400000) AND (`id` < '400000') ORDER BY `id` LIMIT 100
SELECT /*!40001 SQL_NO_CACHE */ `id`,`v_int`,`v_string`,`s_string` FROM `yoon`.`employees_ptarc` FORCE INDEX(`PRIMARY`) WHERE (id < 400000) AND (`id` < '400000') AND ((`id` >= ?)) ORDER BY `id` LIMIT 100
DELETE FROM `yoon`.`employees_ptarc` WHERE (((`id` >= ?))) AND (((`id` <= ?))) AND (id < 400000) LIMIT 100
生成的删除数据语句为:DELETE FROM `yoon`.`employees_ptarc` WHERE (((`id` >= ?))) AND (((`id` <= ?))) AND (id < 400000) LIMIT 100 如果不是核心业务历史数据,这样操作也是没问题的,但是对于核心库删除历史数据,更希望能够实现的删除语句为:delete from table where id=?
将pt-archiver的参数bulk-delete去掉,改成txn-size,并加上sleep来控制,这样不会影响DB,如下:
#pt-archiver --source h=localhost,P=7777,u=root,p='Asd.123@#',D=yoon,t=employees_ptarc --purge --charset=utf8 --where "id <= 400000" --progress=200 --limit=200 --sleep=1 --txn-size=200 --statistics --dry-run
SELECT /*!40001 SQL_NO_CACHE */ `id`,`v_int`,`v_string`,`s_string` FROM `yoon`.`employees_ptarc` FORCE INDEX(`PRIMARY`) WHERE (id <= 600000) AND (`id` < '400000') ORDER BY `id` LIMIT 100
SELECT /*!40001 SQL_NO_CACHE */ `id`,`v_int`,`v_string`,`s_string` FROM `yoon`.`employees_ptarc` FORCE INDEX(`PRIMARY`) WHERE (id <= 600000) AND (`id` < '400000') AND ((`id` >= ?)) ORDER BY `id` LIMIT 100
DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = ?)
查看审计日志:
root,localhost,79,608616,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110367\')',0
root,localhost,79,608617,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110368\')',0
root,localhost,79,608618,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110369\')',0
root,localhost,79,608619,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110370\')',0
root,localhost,79,608620,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110371\')',0
root,localhost,79,608621,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110372\')',0
root,localhost,79,608622,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110373\')',0
root,localhost,79,608623,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110374\')',0
root,localhost,79,608624,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110375\')',0
root,localhost,79,608625,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110376\')',0
root,localhost,79,608626,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110377\')',0
root,localhost,79,608627,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110378\')',0
root,localhost,79,608628,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110379\')',0
root,localhost,79,608629,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110380\')',0
root,localhost,79,608630,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110381\')',0
root,localhost,79,608631,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110382\')',0
root,localhost,79,608632,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110383\')',0
root,localhost,79,608633,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110384\')',0
root,localhost,79,608634,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110385\')',0
root,localhost,79,608635,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110386\')',0
root,localhost,79,608636,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110387\')',0
root,localhost,79,608637,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110388\')',0
root,localhost,79,608638,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110389\')',0
root,localhost,79,608639,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110390\')',0
root,localhost,79,608640,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110391\')',0
root,localhost,79,608641,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110392\')',0
root,localhost,79,608642,QUERY,`yoon`,'DELETE FROM `yoon`.`employees_ptarc` WHERE (`id` = \'110393\')',0
Percona-Toolkit 之 pt-archiver 删除历史数据的更多相关文章
- Percona Toolkit mysql辅助利器
1 PT介绍 Percona Toolkit简称pt工具—PT-Tools,是Percona公司开发用于管理MySQL的工具,功能包括检查主从复制的数据一致性.检查重复索引.定位IO占用高的表文件.在 ...
- Percona Toolkit工具使用
Percona Toolkit简称pt工具-PT-Tools,是Percona公司开发用于管理MySQL的工具,功能包括检查主从复制的数据一致性.检查重复索引.定位IO占用高的表文件.在线DDL等 下 ...
- RDS for MySQL 如何使用 Percona Toolkit
Percona Toolkit 包含多种用于 MySQL 数据库管理的工具. 下面介绍常用的 pt-online-schema-change 和 pt-archiver 搭配 RDS MySQL ...
- Percona Toolkit工具集介绍
部署mysql工具是一个非常重要的部分,所以工具的可靠性和很好的设计非常重要.percona toolkit是一个有30多个mysql工具的工具箱.兼容mysql,percona server,mar ...
- Centos 安装Percona Toolkit工具集
1.下载 下载地址: https://www.percona.com/downloads/percona-toolkit/LATEST/ [root@bogon ~]# wget https:// ...
- vue项目echarts画布删除历史数据重新渲染数据
vue用到echarts时,根据select多选下拉框进行echarts折线图渲染.发现折现只能增加不能减少,后来根据echarts API文档发现 调用方式: chart.setOption(opt ...
- Want to archive tables? Use Percona Toolkit’s pt-archiver--转载
原文地址:https://www.percona.com/blog/2013/08/12/want-to-archive-tables-use-pt-archiver/ Percona Toolkit ...
- Percona Toolkit工具连接MySQL 8报错的解决方案
使用Percona Toolkit的工具连接MySQL 8.x数据库时,会遇到类似"failed: Plugin caching_sha2_password could not be loa ...
- 使用Percona Toolkit解决Mysql主从不同步问题【备忘】
由于各种原因,mysql主从架构经常会出现数据不一致的情况出现,大致归结为如下几类 1:备库写数据 2:执行non-deterministic query 3:回滚掺杂事务表和非事务表的事务 4:bi ...
随机推荐
- 用instsrv.exe+srvany.exe将应用程序安装为windows服务
下载 链接:https://pan.baidu.com/s/1gKu_WwVo-TeWXmrGAr9qjw 提取码:s1vm 用instsrv.exe安装srvany.exe 将instsrv.exe ...
- ABC155E - Payment
简述题意,给你一个大数,你可以选择10的次幂进行加减运算,问如何用最少的次数从0到达这个大数 考虑从这个大数到0,从最低位开始,每次都将这个位置取完,2种策略,贪心的话不好处理进位的情况,可以想到是D ...
- 「CF1004E」Sonya and Ice Cream
题目描述 给定一个 \(N\) 个点的树,要选出一条所含点的个数不超过 \(K\) 的一条路径,使得路径外的点到这条路径的距离的最大值最小. 数据范围:\(1\le K \le N \le 10^5\ ...
- 设计模式课程 设计模式精讲 8-11 单例模式源码解析(jdk+spring+mybaties)
1 源码解析 1.1 单例解析1 1.2 单例解析2(容器单例) 1.3 单例解析3 1.4 单例解析4 1 源码解析 1.1 单例解析1 java.lang.Runtime /** * 饿汉式加载, ...
- 111、Java中String类之字符串文本全部拆分
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- 洛谷 P6046 [CTSC2000]快乐的蜜月
先讲解一下如何处理这道题的毒瘤输入.\(m\) 和 \(d\) 之间的"/"和" TO "都可以用 getchar() 强行吃掉,日期的转换可以用公式 \(s_ ...
- Django:验证码相关问题
http://blog.csdn.net/csapr1987/article/details/7728315 https://zhidao.baidu.com/question/13837387222 ...
- Windows中使用QEMU创建树莓派虚拟机
环境: windows 10 2018-04-18-raspbian-stretch.img 一.下载QEMU 根据你的系统情况,下载相应的版本,并安装完成 https://www.qemu.org/ ...
- 注册模块上线前安全测试checklist
许多应用系统都有注册模块,正常用户通过注册功能,获得应用系统使用权限:而非法用户通过注册模块,则是为了达到不可告人的目的,非法用户可以通过注册模块与服务端进行交互(一切用户输入都不可信),因此系统上线 ...
- 6专题总结-动态规划dynamic programming
专题6--动态规划 1.动态规划基础知识 什么情况下可能是动态规划?满足下面三个条件之一:1. Maximum/Minimum -- 最大最小,最长,最短:写程序一般有max/min.2. Yes/N ...