1. 语法
注意:pt-archiver操作的表必须有主键。
1.1 选项:
Specify at least one of "--dest","--file", or "--purge".
下面几个参数都是互斥的,只能选其一
"--ignore"and "--replace" are mutually exclusive.
"--txn-size"and "--commit-each" are mutually exclusive.
"--low-priority-insert"and "--delayed-insert" are mutually exclusive.
"--share-lock"and "--for-update" are mutually exclusive.
"--analyze"and "--optimize" are mutually exclusive.
"--no-ascend"and "--no-delete" are mutually exclusive.
1.2 常用参数
--limit10000 每次取1000行数据用pt-archive处理,Number of rows to fetch and archive per statement.
--txn-size 1000 设置1000行为一个事务提交一次,Number of rows pertransaction.
--where‘id<3000‘ 设置操作条件
--progress5000 每处理5000行输出一次处理信息
--statistics 输出执行过程及最后的操作统计。(只要不加上--quiet,默认情况下pt-archive都会输出执行过程的)
--charset=UTF8 指定字符集为UTF8
--bulk-delete 批量删除source上的旧数据(例如每次1000行的批量删除操作)
--bulk-insert 批量插入数据到dest主机 (看dest的general log发现它是通过在dest主机上LOAD DATA LOCAL INFILE插入数据的)
--replace 将insert into 语句改成replace写入到dest库
--sleep120 每次归档了limit个行记录后的休眠120秒(单位为秒)
--file‘/root/test.txt‘
--purge 删除source数据库的相关匹配记录
--header 输入列名称到首行(和--file一起使用)
--no-check-charset 不指定字符集
--check-columns 检验dest和source的表结构是否一致,不一致自动拒绝执行(不加这个参数也行。默认就是执行检查的)
--no-check-columns 不检验dest和source的表结构是否一致,不一致也执行(会导致dest上的无法与source匹配的列值被置为null或者0)
--chekc-interval 默认1s检查一次
--local 不把optimize或analyze操作写入到binlog里面(防止造成主从延迟巨大)
--retries 超时或者出现死锁的话,pt-archiver进行重试的间隔(默认1s)
--no-version-check 目前为止,发现部分pt工具对阿里云RDS操作必须加这个参数
--analyze=ds 操作结束后,优化表空间(d表示dest,s表示source)
默认情况下,pt-archiver操作结束后,不会对source、dest表执行analyze或optimize操作,因为这种操作费时间,并且需要你提前预估有足够的磁盘空间用于拷贝表。一般建议也是pt-archiver操作结束后,在业务低谷手动执行analyze table用以回收表空间。
2. 例子
2.1 删除老数据
pt-archiver --txn-size=2000 --sleep=1 --limit=2000 --statistics --no-check-charset --no-version-check --purge --bulk-delete --source h=rm-bp191tt4364tbtpqb143.mysql.rds.aliyuncs.com,P=3306,u=,p='',D=esjobdata,t=job_execution_log,i=idx_complete_time --where "complete_time < '2019-04-1 00:00:00'"
2.2 归档
pt-archiver --txn-size=1000 --limit=1000 --sleep=1 --bulk-insert --no-delete --statistics --no-check-charset --no-version-check --source h=rm-bp13xy75phh7c60em206.mysql.rds.aliyuncs.com,L=1,P=3306,u=,p='',D=bx_app_chatroom,t=t_chatroom_behavior_log --dest h=rm-bp13xy75phh7c60em206.mysql.rds.aliyuncs.com,L=1,P=3306,u=,p='',D=bx_app_chatroom,t=t_chatroom_behavior_log_tmp --where "create_time < '2019-03-28 11:00:00'"
# 37---> 存放历史数据 rm-bp1vwq001c60rav2o.mysql.rds.aliyuncs.com [ ypp_app_2016 ]
## 反归档
pt-archiver --txn-size=1000 --sleep=1 --limit=1000 --bulk-insert --no-delete --statistics --no-check-charset --no-version-check --source h=rm-bp1vwq001c60rav2o.mysql.rds.aliyuncs.com,L=1,P=3306,u=,p='',D=ypp_app_2016,t=risk_hit_data_20190119 --dest h=rm-bp1p8i0ia67e44cbi.mysql.rds.aliyuncs.com,L=1,P=3306,u=inception_rw,p='',D=ypp_fengkong,t=risk_hit_data --where "play_order_create_time = '1970-01-01 00:00:00'"
## 删除控制延迟
pt-archiver --txn-size=3000 --limit=6000 --statistics --no-check-charset --no-version-check --purge --bulk-delete --source u=,p='',h=rm-bp1p8i0ia67e44cbi.mysql.rds.aliyuncs.com,P=3306,D=ypp_fengkong,t=risk_message_record --where "created_at < '2019-03-01 00:00:00'" --max-lag=30 --check-slave-lag u=,p='',h=rr-bp18xi82q15s3ahao.mysql.rds.aliyuncs.com,P=3306
2.3 pt-archiver 测试对表数据重复插入
#test表3条数据, test_tmp空表
#pt-archiver --txn-size=1000 --limit=1000 --sleep=1 --bulk-insert --no-delete --statistics --no-check-charset --no-version-check --source h=rm-bp1q926uimur046k5905.mysql.rds.aliyuncs.com,L=1,P=3306,u=,p='',D=test_ince,t=test --dest h=rm-bp1q926uimur046k5905.mysql.rds.aliyuncs.com,L=1,P=3306,u=,p='',D=test_ince,t=test_tmp --where "id < 4"
Started at 2019-03-28T17:27:07, ended at 2019-03-28T17:27:08
Source: D=test_ince,L=1,P=3306,h=rm-bp1q926uimur046k5905.mysql.rds.aliyuncs.com,p=...,t=test,u=
Dest: D=test_ince,L=1,P=3306,h=rm-bp1q926uimur046k5905.mysql.rds.aliyuncs.com,p=...,t=test_tmp,u=
SELECT 3
INSERT 3
DELETE 0
Action Count Time Pct
sleep 1 1.0002 98.67
bulk_inserting 1 0.0021 0.21
commit 2 0.0021 0.21
select 2 0.0017 0.17
print_bulkfile 3 0.0000 0.00
other 0 0.0076 0.75
#test 表3条数据, test_tmp 3条数据
#pt-archiver --txn-size=1000 --limit=1000 --sleep=1 --bulk-insert --no-delete --statistics --no-check-charset --no-version-check --source h=rm-bp1q926uimur046k5905.mysql.rds.aliyuncs.com,L=1,P=3306,u=,p='',D=test_ince,t=test --dest h=rm-bp1q926uimur046k5905.mysql.rds.aliyuncs.com,L=1,P=3306,u=,p='',D=test_ince,t=test_tmp --where "id < 4"
Started at 2019-03-28T17:27:40, ended at 2019-03-28T17:27:41
Source: D=test_ince,L=1,P=3306,h=rm-bp1q926uimur046k5905.mysql.rds.aliyuncs.com,p=...,t=test,u=
Dest: D=test_ince,L=1,P=3306,h=rm-bp1q926uimur046k5905.mysql.rds.aliyuncs.com,p=...,t=test_tmp,u=
SELECT 3
INSERT 0
DELETE 0
Action Count Time Pct
sleep 1 1.0002 98.67
bulk_inserting 1 0.0022 0.21
select 2 0.0019 0.19
commit 2 0.0014 0.14
print_bulkfile 3 0.0000 0.00
other 0 0.0080 0.79
#mysql> delete from test_tmp where id = 2;
#pt-archiver --txn-size=1000 --limit=1000 --sleep=1 --bulk-insert --no-delete --statistics --no-check-charset --no-version-check --source h=rm-bp1q926uimur046k5905.mysql.rds.aliyuncs.com,L=1,P=3306,u=,p='',D=test_ince,t=test --dest h=rm-bp1q926uimur046k5905.mysql.rds.aliyuncs.com,L=1,P=3306,u=,p='',D=test_ince,t=test_tmp --where "id < 4"
Started at 2019-03-28T17:29:35, ended at 2019-03-28T17:29:36
Source: D=test_ince,L=1,P=3306,h=rm-bp1q926uimur046k5905.mysql.rds.aliyuncs.com,p=...,t=test,u=
Dest: D=test_ince,L=1,P=3306,h=rm-bp1q926uimur046k5905.mysql.rds.aliyuncs.com,p=...,t=test_tmp,u=
SELECT 3
INSERT 1
DELETE 0
Action Count Time Pct
sleep 1 1.0002 98.51
select 2 0.0027 0.27
bulk_inserting 1 0.0024 0.24
commit 2 0.0023 0.22
print_bulkfile 3 0.0000 0.00
other 0 0.0077 0.75
结论: 已有数据不会重复插入
主键非递增类型
建议强制走索引
# 默认语句
pt-archiver --txn-size=2000 --limit=4000 --sleep=1 --bulk-insert --no-delete --statistics --no-check-charset --no-version-check --source h=rm-bp13xy75phh7c60em206.mysql.rds.aliyuncs.com,L=1,P=3306,u=,p='',D=bx_app_chatroom,t=t_chatroom_behavior_log_old --dest h=rm-bp13xy75phh7c60em206.mysql.rds.aliyuncs.com,L=1,P=3306,u=,p='',D=bx_app_chatroom,t=t_chatroom_behavior_log --where "create_time >= '2019-03-28 18:45:00' "
#执行的语句是全表扫描
mysql> explain SELECT /*!40001 SQL_NO_CACHE */ `id`,`room_id`,`user_id`,`platform`,`app_version`,`bundle_id`,`content`,`type`,`status`,`memo`,`create_time`,`app_id` FROM `bx_app_chatroom`.`t_chatroom_behavior_log_old` FORCE INDEX(`PRIMARY`) WHERE (create_time >= '2019-03-28 18:45:00' ) AND ((`id` > '12881a67935244d6b4b0190bea499822')) ORDER BY `id` LIMIT 4000;
+----+-------------+-----------------------------+-------+---------------+---------+---------+------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------------------------+-------+---------------+---------+---------+------+----------+-------------+
| 1 | SIMPLE | t_chatroom_behavior_log_old | range | PRIMARY | PRIMARY | 98 | NULL | 11111765 | Using where |
+----+-------------+-----------------------------+-------+---------------+---------+---------+------+----------+-------------+
# i指定索引
pt-archiver --txn-size=2000 --limit=4000 --sleep=1 --bulk-insert --no-delete --statistics --no-check-charset --no-version-check --source h=rm-bp13xy75phh7c60em206.mysql.rds.aliyuncs.com,L=1,P=3306,u=,p='',D=bx_app_chatroom,t=t_chatroom_behavior_log_old,i=idx_create_time --dest h=rm-bp13xy75phh7c60em206.mysql.rds.aliyuncs.com,L=1,P=3306,u=,p='',D=bx_app_chatroom,t=t_chatroom_behavior_log --where "create_time >= '2019-03-28 18:45:00' "
mysql> explain SELECT /*!40001 SQL_NO_CACHE */ `id`,`room_id`,`user_id`,`platform`,`app_version`,`bundle_id`,`content`,`type`,`status`,`memo`,`create_time`,`app_id` FROM `bx_app_chatroom`.`t_chatroom_behavior_log_old` FORCE INDEX(`idx_create_time`) WHERE (create_time >= '2019-03-28 18:45:00' ) AND ((`create_time` > '2019-03-28 18:55:04')) ORDER BY `create_time` LIMIT 4000;
+----+-------------+-----------------------------+-------+-----------------+-----------------+---------+------+--------+-----------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------------------------+-------+-----------------+-----------------+---------+------+--------+-----------------------+
| 1 | SIMPLE | t_chatroom_behavior_log_old | range | idx_create_time | idx_create_time | 4 | NULL | 231168 | Using index condition |
+----+-------------+-----------------------------+-------+-----------------+-----------------+---------+------+--------+-----------------------+
<wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style="display: none;">