回收innodb表空间
以下论述均假定innodb_file_per_table开启
先用常规optimize回收:
[plain]
mysql> select count(*) from t; +----------+
| count(*) |
+----------+
| 1200096 |
+----------+
[mysql@even employees]$ ls -alh t.ibd
-rw-rw---- 1 mysql dba 72m 10-08 17:39 t.ibd
mysql> delete from t limit 800000;
mysql> select count(*) from t;
+----------+
| count(*) |
+----------+
| 400096 |
+----------+
[mysql@even employees]$ ls -alh t.ibd
-rw-rw---- 1 mysql dba 72m 10-08 17:41 t.ibd
mysql> optimize table t;
+-------------+----------+----------+-------------------------------------------------------------------+
| table | op | msg_type | msg_text |
+-------------+----------+----------+-------------------------------------------------------------------+
| employees.t | optimize | note | table does not support optimize, doing recreate + analyze instead |
| employees.t | optimize | status | ok |
+-------------+----------+----------+-------------------------------------------------------------------+
[mysql@even employees]$ ls -alh t.ibd
-rw-rw---- 1 mysql dba 29m 10-08 17:43 t.ibd
来看下这种方法的缺陷
[plain]
mysql> show processlist;
+----+------+-----------+-----------+---------+------+-------------------+------------------+
| id | user | host | db | command | time | state | info |
+----+------+-----------+-----------+---------+------+-------------------+------------------+
| 5 | root | localhost | null | query | 0 | null | show processlist |
| 6 | root | localhost | employees | query | 64 | copy to tmp table | optimize table t |
+----+------+-----------+-----------+---------+------+-------------------+------------------+
mysqladmin debug结果如下
[plain]
thread database.table_name locked/waiting lock_type
6 employees.t locked - read read lock without concurrent inserts
用optimize缺点显而易见:读锁,特别当你有大表时性能恐怕会深受影响
这里推荐使用percona公司:pt-online-schema-change,避免加锁
[plain]
$ pt-online-schema-change -uroot -poracle --alter engine=innodb d=employees,t=t --execute
执行pt工具时有可能发生的类似错误:
[plain]
cannot chunk the original table there is no good index and the table is oversized
这是因为被作用的表需要含有主键或者唯一索引,这或许也能成为这款工具的小bug吧
bitscn.com
