今天对myiam数据表进行批量删除后,发现空间没有回收,查了资料后,发现要通过optimize table来回收空间.
测试如下,建立数据表:
- CREATE TABLE `ttext` (
- `id` int(11) DEFAULT NULL,
- `context` text
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1
- insert into ttext values( 1,’tiger1′),(2,’tiger2′),(3,’tiger3′);
-
然后重复执行insert into ttext select * from ttext;这样重复数据,很快数据文件就达到240MB,这个时候,可以确认的是id为1的数据占据了1/3的空间,那么接下来,可以删除delete from ttext where id=1;
按正常的想法,这样的操作后,空间应该有回收的,但是事实没有.
OPTIMIZE TABLE should be used if you have deleted a large part of a
table or if you have made many changes to a table with variable-length
rows (tables that have VARCHAR, VARBINARY, BLOB, or TEXT columns).
You can use OPTIMIZE TABLE to//phpfensi.com
reclaim the unused space and to defragment the data file.
通过资料来看可以通过optimize table ttext来回收相应的空间,继续查看了官方手册后发现,这个操作与表所用的引擎有很大的关系.
innodb表必须是使用独立表空间的才行,其次在mysql5.1.27开始optimize table is also supported for partitioned tables.就是可以对分区表进行优化.
针对myisam引擎,使用optimize table 还有如下功能:
If the table has deleted or split rows, repair the table. [修复表]
If the index pages are not sorted, sort them. [索引未排序,会排序]
If the table’s statistics are not up to date (and the repair could not be accomplished by sorting the index), update them.[若表的统计信息不是最新的,更新它] |