使用OPTIMIZE TABLE命令来整理表碎片实践

来源:互联网 发布:怎么加入摄影协会 知乎 编辑:程序博客网 时间:2024/04/30 07:33
操作环境:ubuntu 14.10   mysql 5.6.25

对含有BLOB或TEXT字段的表,若经常做修改或删除类的操作,需要定期执行OPTIMIZE TABLE命令来整理碎片。

1.create table t1(id varchar(64), content text)engine=myisam, default charset=utf8;

存储引擎使用myisam

 

2.插入数据

insert into t1 values(1, repeat('tony', 100));          --repeat('tony', 100),返回tony重复100次后的数据

insert into t1 values(2, repeat('tony', 100));

insert into t1 values(3, repeat('tony', 100));

快速往表中插入大量数据的一种好方式,重复执行下面的语句

insert into t1 select * from t1;   --从t1查询出所有数据,再插入t1表,数据成倍增长

 

3.查看文件大小

root@ubuntu2:/var/lib/mysql/tsetest# du -sh t1.*
12K t1.frm
310M t1.MYD
4.0K t1.MYI

 

4.删除数据再查看文件大小

mysql> delete from t1 where id = 2;
Query OK, 262144 rows affected (2.04 sec)

root@ubuntu2:/var/lib/mysql/tsetest# du -sh t1.*
12K t1.frm
310M t1.MYD
4.0K t1.MYI

发现删除大量数据后,表文件的物理大小并没有减少。

 

5.使用OPTIMIZE TABLE命令整理表碎片

mysql> optimize table t1;
+------------+----------+----------+----------+
| Table      | Op       | Msg_type | Msg_text |
+------------+----------+----------+----------+
| tsetest.t1 | optimize | status   | OK       |
+------------+----------+----------+----------+
1 row in set (2.34 sec)

root@ubuntu2:/var/lib/mysql/tsetest# du -sh t1.*
12K t1.frm
206M t1.MYD
4.0K t1.MYI

使用OPTIMIZE TABLE命令整理表碎片后,表文件大小减少了将近三分之一。

0 0