MySQL · 2016-10-16 0

Percona-Toolkit系列之pt-find查找利器

1.pt-find

pt-find也是一个perl语言写的find脚本,查找并且执行一些动作的工具,类似 GNU find命令。

2.常用模式

2.1找出最近一天创建的MyISAM存储引擎表

[mysql@hpc02 ~]$  pt-find --ctime -1 --engine MyISAM --noquote --printf "this is res->>> tab_name:%D.%N\tsize:%T\n" 
this is res->>> tab_name:db01.a size:1024
this is res->>> tab_name:db01.b size:1024
this is res->>> tab_name:db01.c size:1024
this is res->>> tab_name:db01.d size:1024
this is res->>> tab_name:test.a size:1024
this is res->>> tab_name:test.b size:1024
this is res->>> tab_name:test.c size:1024
this is res->>> tab_name:test.d size:1024

结果可以使用printf自定义打印

–ctime:创建时间

–engine:表的存储引擎

–noquote:不打印mysql的反引号

–printf:对于匹配的表,我们可以打印出自定义的信息。%T表大小,%D数据库名,%N表名

2.2查找出最近一天创建的MyISAM表,并且将存储引擎改为INNODB

[mysql@hpc02 ~]$ pt-find --ctime -1 --engine MyISAM --noquote --printf "table altered->>> tab_name:%D.%N\tsize:%T\n"  --exec "alter table %D.%N ENGINE=InnoDB"
table altered->>> tab_name:db01.a       size:1024
table altered->>> tab_name:db01.b       size:1024
table altered->>> tab_name:db01.c       size:1024
table altered->>> tab_name:db01.d       size:1024
table altered->>> tab_name:test.a       size:1024
table altered->>> tab_name:test.b       size:1024
table altered->>> tab_name:test.c       size:1024
table altered->>> tab_name:test.d       size:1024

只需用–exec加上我们需要只需的SQL语句即可。

2.3查找出db01数据库中的空表,并且删除

[mysql@hpc02 ~]$ pt-find db01 --empty  --noquote --printf "table dropped->>> tab_name:%D.%N\tsize:%T\n"  --exec-plus "DROP TABLE %s"
table dropped->>> tab_name:db01.a       size:16384
table dropped->>> tab_name:db01.b       size:16384
table dropped->>> tab_name:db01.c       size:16384
table dropped->>> tab_name:db01.d       size:16384

–empty代表空表

2.4找出test数据库中大于1M的表,并且按照大小排序

[mysql@hpc02 ~]$ pt-find --config find.cnf test --tablesize +1M   --noquote --printf "%T \t%D.%N\n" |sort -rn    
61440000        test.example3
15220736        test.example2
12075008        test.example1
7880704         test.example

–tablesize:按照表尺寸查找

我们可以将常用的登录信息放在一个培训文件中:

[mysql@hpc02 ~]$ cat find.cnf 
host=192.168.56.103
user=root
password=111111
port=3306

更多选项参考–help