pt-archiver(数据导入导出工具)
数据导入导出工具pt-archiver
工具可以将MySQL的表数据导出到一个新表或者一个文件,也有自己的应用场景,比如数据归档,删除数据,数据合并等。
具体用法:
pt-archiver [OPTIONS] --source DSN --where WHERE
PT工具链接参数DSN一般适用于所有toolkit工具。
DSN的详细参数:
a:查询
A:字符集
b:true代表禁用binlog
D:数据库
u:数据库链接账号
p:数据库链接密码
h:主机IP
F:配置文件位置
i:是否使用某索引
m:插件模块
P:端口号
S:socket文件
t:表
具体使用,从一张表导入到另外一张表,要注意的是新表必须是已经建立好的一样的表结构,不会自动创建表,而且where条件是必须指定的:
[root@mxqmongodb2 bin]# ./pt-archiver --source h=172.16.16.35,P=3306,D=tpcc,t=new_orders,u=root,p=123456 --dest h=172.16.16.35,P=3306,D=test,t=new_orders,u=root,p=123456 --no-check-charset --where 'no_o_id>3800' --progress 1000 --no-delete --limit=1000 --statistics TIME ELAPSED COUNT 2017-06-21T11:44:15 0 0 2017-06-21T11:45:20 64 1000 2017-06-21T11:46:24 128 2000 2017-06-21T11:47:28 192 3000 2017-06-21T11:48:31 255 4000 2017-06-21T11:49:34 318 5000 2017-06-21T11:50:39 383 6000 2017-06-21T11:51:41 445 7000 2017-06-21T11:52:45 509 8000 2017-06-21T11:53:48 572 9000 2017-06-21T11:53:52 576 9056 Started at 2017-06-21T11:44:15, ended at 2017-06-21T11:53:52 Source: D=tpcc,P=3306,h=172.16.16.35,p=...,t=new_orders,u=root Dest: D=test,P=3306,h=172.16.16.35,p=...,t=new_orders,u=root SELECT 9056 INSERT 9056 DELETE 0 Action Count Time Pct commit 18114 568.6790 98.71 inserting 9056 4.9776 0.86 select 11 0.0689 0.01 other 0 2.4091 0.42
我们打开general_log,发现这个操作是一条一条执行的,每一条数据都是一个事物。例如下面:
2017-06-21T03:03:08.328427Z 17054 Query INSERT INTO `test`.`new_orders`(`no_o_id`,`no_d_id`,`no_w_id`) VALUES ('3230','7','1') 2017-06-21T03:03:08.328819Z 17054 Query commit 2017-06-21T03:03:08.362113Z 17053 Query commit 2017-06-21T03:03:08.362565Z 17054 Query INSERT INTO `test`.`new_orders`(`no_o_id`,`no_d_id`,`no_w_id`) VALUES ('3231','7','1') 2017-06-21T03:03:08.362977Z 17054 Query commit
而且整个处理过程不会对原表进行锁定,对于作为导出工具来说很优秀了。
具体使用,将表中数据导出到文件:
可以看下导出到文件:
[root@mxqmongodb2 bin]# ./pt-archiver --source h=172.16.16.35,P=3306,D=tpcc,t=new_orders,u=root,p=123456 --no-check-charset --where 'no_o_id>3800' --progress 1000 --no-delete --file "/home/sa/pt-archiver.dat" --limit=1000 TIME ELAPSED COUNT 2017-06-21T13:39:01 0 0 2017-06-21T13:39:02 0 1000 2017-06-21T13:39:02 0 2000 2017-06-21T13:39:02 0 3000 2017-06-21T13:39:02 1 4000 2017-06-21T13:39:03 1 5000 2017-06-21T13:39:03 1 6000 2017-06-21T13:39:03 1 7000 2017-06-21T13:39:04 2 8000 2017-06-21T13:39:04 2 9000 2017-06-21T13:39:04 2 9056
我们要注意的是--no-delete这个参数,默认情况下是不指定这个参数会直接将数据删除,我们最好还是指定一下。
看一下日志,他是通过主键取数据,每次取1000条,而且分割成不同的批次来取:
2017-06-21T05:39:04.332458Z 17063 Query SELECT /*!40001 SQL_NO_CACHE */ `no_o_id`,`no_d_id`,`no_w_id` FROM `tpcc`.`new_orders` FORCE INDEX(`PRIMARY`) WHERE (no_o_id>3800) AND ((`no_w_id` > '10') OR (`no_w_id` = '10' AND `no_d_id` > '10') OR (`no_w_id` = '10' AND `no_d_id` = '10' AND `no_o_id` > '3915')) ORDER BY `no_w_id`,`no_d_id`,`no_o_id` LIMIT 1000 2017-06-21T05:39:04.332963Z 17063 Query commit 2017-06-21T05:39:04.333220Z 17063 Quit
再看一下导出的文件,发现是固定格式的文件:
[root@mxqmongodb2 sa]# head -10 pt-archiver.dat 3801 1 1 3802 1 1 3803 1 1 3804 1 1 3805 1 1 3806 1 1 3807 1 1 3808 1 1 3809 1 1 3810 1 1
最后看一下删除数据:
mysql> select count(*) from test.new_orders where no_o_id>3900; +----------+ | count(*) | +----------+ | 862 | +----------+ 1 row in set (0.00 sec)
删除:
[root@mxqmongodb2 bin]# ./pt-archiver --source h=172.16.16.35,P=3306,D=test,t=new_orders,u=root,p=123456 --no-check-charset --where 'no_o_id>3900' --purge --limit=100 mysql> select count(*) from test.new_orders where no_o_id>3900; +----------+ | count(*) | +----------+ | 0 | +----------+ 1 row in set (0.00 sec)
然后看一下日志文件:
2017-06-21T05:59:46.655361Z 17065 Query SELECT /*!40001 SQL_NO_CACHE */ `no_o_id`,`no_d_id`,`no_w_id` FROM `test`.`new_orders` FORCE INDEX(`PRIMARY`) WHERE (no_o_id>3900) AND ((`no_w_id` > '6') OR (`no_w_id` = '6' AND `no_d_id` > '3') OR (`no_w_id` = '6' AND `no_d_id` = '3' AND `no_o_id` >= '3930')) ORDER BY `no_w_id`,`no_d_id`,`no_o_id` LIMIT 100 2017-06-21T05:59:46.656582Z 17065 Query DELETE FROM `test`.`new_orders` WHERE (`no_w_id` = '6' AND `no_d_id` = '3' AND `no_o_id` = '3931') 2017-06-21T05:59:46.657058Z 17065 Query commit 2017-06-21T05:59:46.710106Z 17065 Query DELETE FROM `test`.`new_orders` WHERE (`no_w_id` = '6' AND `no_d_id` = '3' AND `no_o_id` = '3932') 2017-06-21T05:59:46.710659Z 17065 Query commit 2017-06-21T05:59:46.756656Z 17065 Query DELETE FROM `test`.`new_orders` WHERE (`no_w_id` = '6' AND `no_d_id` = '3' AND `no_o_id` = '3933') 2017-06-21T05:59:46.757106Z 17065 Query commit 2017-06-21T05:59:46.814019Z 17065 Query DELETE FROM `test`.`new_orders` WHERE (`no_w_id` = '6' AND `no_d_id` = '3' AND `no_o_id` = '3934') 2017-06-21T05:59:46.814538Z 17065 Query commit 2017-06-21T05:59:46.869874Z 17065 Query DELETE FROM `test`.`new_orders` WHERE (`no_w_id` = '6' AND `no_d_id` = '3' AND `no_o_id` = '3935') 2017-06-21T05:59:46.870439Z 17065 Query commit 2017-06-21T05:59:46.926640Z 17065 Query SELECT 'pt-archiver keepalive' 2017-06-21T05:59:46.927001Z 17065 Query DELETE FROM `test`.`new_orders` WHERE (`no_w_id` = '6' AND `no_d_id` = '3' AND `no_o_id` = '3936')
是先分批量查出来,然后逐条进行删除。
热衷于学习讨论MySQL和SQL Server,NoSQL等数据库技术,欢迎加入SQL优化群:659336691