MyISAM的OPTIMIZE

一些介绍
   通过OPTIMIZE 命令来整理MyISAM 表的文件。这就像我们使用Windows 操作系统会每过一段时间
后都会做一次磁盘碎片整理,让系统中的文件尽量使用连续空间,提高文件的访问速度。MyISAM 在
通过OPTIMIZE 优化整理的时候,主要也是将因为数据删除和更新造成的碎片空间清理,使整个文件
连续在一起。一般来说,在每次做了较大的数据删除操作之后都需要做一次OPTIMIZE 操作。而且每
个季度都应该有一次OPTIMIZE 的维护操作。

一、创建一张表,并插入大量数据

(localhost@testdb)[root]> select * from test5;
+----+------+
| id | name |
+----+------+
|  1 | aa   |

(localhost@testdb)[root]> insert into test5 select * from test5;
Query OK, 12288 rows affected (0.07 sec)
(localhost@testdb)[root]> 
(localhost@testdb)[root]> 

(localhost@testdb)[root]> show index from test5;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| test5 |          1 | id       |            1 | id          | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)


(localhost@testdb)[root]> 
(localhost@testdb)[root]> 
(localhost@testdb)[root]> 

[root@node1 testdb]# ls -trl
total 2096
-rw-r----- 1 mysql mysql     65 Feb 23 09:54 db.opt
-rw-r----- 1 mysql mysql   8586 Feb 26 16:18 test5.frm
-rw-r----- 1 mysql mysql 293888 Feb 29 15:52 test5.MYI
-rw-r----- 1 mysql mysql 491520 Feb 29 15:52 test5.MYD
二、删除表部分数据
(localhost@testdb)[root]> delete from test5 where id=1;
Query OK, 8192 rows affected (0.16 sec)
(localhost@testdb)[root]> 
(localhost@testdb)[root]> 
(localhost@testdb)[root]> 
(localhost@testdb)[root]> 
(localhost@testdb)[root]> commit;
Query OK, 0 rows affected (0.00 sec)
(localhost@testdb)[root]> delete from test5 where id=2;
Query OK, 8192 rows affected (0.14 sec)
(localhost@testdb)[root]> commit;
Query OK, 0 rows affected (0.00 sec)

三、做optimize操作
(localhost@testdb)[root]> optimize table test5;
+--------------+----------+----------+----------+
| Table        | Op       | Msg_type | Msg_text |
+--------------+----------+----------+----------+
| testdb.test5 | optimize | status   | OK       |
+--------------+----------+----------+----------+
1 row in set (0.02 sec)

(localhost@testdb)[root]> show index from test5;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| test5 |          1 | id       |            1 | id          | A         |           1 |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)

四、操作完成之后,索引和数据都减小
[root@node1 testdb]# ls -trl
total 1576
-rw-r----- 1 mysql mysql     65 Feb 23 09:54 db.opt
-rw-r----- 1 mysql mysql   8586 Feb 26 16:18 test5.frm
-rw-r----- 1 mysql mysql  87040 Feb 29 15:53 test5.MYI
-rw-r----- 1 mysql mysql 163840 Feb 29 15:53 test5.MYD


请使用浏览器的分享功能分享到微信等