博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

pt-find 使用实例

Posted on 2016-12-27 10:37  moss_tan_jun  阅读(341)  评论(0编辑  收藏  举报

pt-find - Find MySQL tables and execute actions, like GNU find.

用法:
pt-find [OPTION...] [DATABASE...]

例子:

找出创建于一天之前,并且是myisam存储引擎的表
[root@goolen ~]# pt-find --ctime +1 --engine MyISAM -uroot -proot
`mysql`.`db`
`mysql`.`event`
`mysql`.`func`
`mysql`.`help_category`
`mysql`.`help_keyword`
`mysql`.`help_relation`
`mysql`.`help_topic`
`mysql`.`host`
`mysql`.`ndb_binlog_index`
`mysql`.`plugin`
`mysql`.`proc`
`mysql`.`procs_priv`
`mysql`.`proxies_priv`
`mysql`.`servers`
`mysql`.`tables_priv`
`mysql`.`time_zone`
`mysql`.`time_zone_leap_second`
`mysql`.`time_zone_name`
`mysql`.`time_zone_transition`
`mysql`.`time_zone_transition_type`
`mysql`.`user`

找出goolen库中的innodb引擎的表,并且把他们转换为myisam表:
mysql> select table_name,engine from information_schema.tables where table_schema='goolen';
+-----------------+--------+
| table_name      | engine |
+-----------------+--------+
| a               | InnoDB |
| b               | InnoDB |
| c               | InnoDB |
| g1              | InnoDB |
| topic_indicator | InnoDB |
+-----------------+--------+
[root@goolen ~]# pt-find --engine InnoDB --exec "ALTER TABLE %D.%N ENGINE=MyISAM" -uroot -proot goolen

mysql> select table_name,engine from information_schema.tables where table_schema='goolen';
+-----------------+--------+
| table_name      | engine |
+-----------------+--------+
| a               | MyISAM |
| b               | MyISAM |
| c               | MyISAM |
| g1              | MyISAM |
| topic_indicator | MyISAM |
+-----------------+--------+

找出goolen库中的空表,然后删除:
mysql> use goolen;
Database changed
mysql> show tables;
+------------------+
| Tables_in_goolen |
+------------------+
| a                |
| b                |
| c                |
| g1               |
| topic_indicator  |
+------------------+
5 rows in set (0.00 sec)
[root@goolen ~]# pt-find --empty goolen --exec-plus "DROP TABLE %s"       

mysql> show tables;
+------------------+
| Tables_in_goolen |
+------------------+
| g1               |
+------------------+
1 row in set (0.00 sec)

找出goolen库中size大于200M的表
[root@goolen goolen]# ll -h bigsize_table.*
-rw-rw---- 1 mysql mysql 9.4K Dec  2 16:52 bigsize_table.frm
-rw-rw---- 1 mysql mysql 420M Dec  2 16:54 bigsize_table.ibd

[root@goolen ~]# pt-find --tablesize +200M -uroot -proot goolen
`goolen`.`bigsize_table`

Find all tables and print their total data and index size, and sort largest tables first (sort is a different program, by the way).
列出所有的表,包括表的总行数和索引的size,并按总数倒序排序(排序操作有系统命令sort完成,pt-find本身没排序功能)
[root@goolen ~]# pt-find --printf "%T\t%D.%N\n" -uroot -proot | sort -rn
425639936  `goolen`.`bigsize_table`
461592  `mysql`.`help_topic`
108816  `mysql`.`help_keyword`
32768   `test`.`goolen3`
32768   `test`.`goolen2`
27675   `mysql`.`help_relation`
25150   `mysql`.`help_category`
16384   `test`.`t1`
16384   `test`.`goolen`
6880    `mysql`.`db`
6506    `mysql`.`proxies_priv`
。。。
。。。

As above, but this time, insert the data back into the database for posterity:
列出所有的表,把输出信息保存到goolen库里的tblsize表里:
mysql> create table tblsize(db varchar(20),tbl varchar(35) ,size int);  
Query OK, 0 rows affected (0.12 sec)

[root@goolen ~]# pt-find --noquote --exec "INSERT INTO goolen.tblsize(db, tbl, size) VALUES('%D', '%N', %T)" -uroot -proot

mysql> select * from tblsize;
+--------------------+-------------------------------------+-----------+
| db                 | tbl                                 | size      |
+--------------------+-------------------------------------+-----------+
| goolen             | bigsize_table                       | 425639936 |
| goolen             | g1                                  |      2108 |
| goolen             | tblsize                             |     16384 |
| mysql              | columns_priv                        |      4096 |
| mysql              | db                                  |      6880 |
| mysql              | event                               |      2048 |
| mysql              | func                                |      1024 |
| mysql              | general_log                         |         0 |
+--------------------+-------------------------------------+-----------+

参数说明:
--ask-pass
Prompt for a password when connecting to MySQL.
连接的时候提示输出密码

例:
[root@goolen ~]#
[root@goolen ~]# pt-find --printf "%T\t%D.%N\n" -uroot --ask-pass goolen 
Enter password: 
425639936       `goolen`.`bigsize_table`
2108    `goolen`.`g1`
16384   `goolen`.`tblsize`

--case-insensitive
Specifies that all regular expression searches are case-insensitive.

例:
默认匹配区分大小写,innodb全部小写,没有输出匹配信息 
[root@goolen ~]# pt-find --engine innodb --printf "ALTER TABLE %D.%N ENGINE=MyISAM" -uroot -proot goolen      
指定忽略大小写:
[root@goolen ~]# pt-find --engine innodb --printf "ALTER TABLE %D.%N ENGINE=MyISAM" -uroot -proot goolen --case-insensitive
ALTER TABLE `goolen`.`bigsize_table` ENGINE=MyISAMALTER TABLE `goolen`.`tblsize` ENGINE=MyISAM

[root@goolen ~]# pt-find --engine InnoDB --printf "ALTER TABLE %D.%N ENGINE=MyISAM" -uroot -proot goolen        
ALTER TABLE `goolen`.`bigsize_table` ENGINE=MyISAMALTER TABLE `goolen`.`tblsize` ENGINE=MyISAM
[root@goolen ~]#