ITPub博客

首页 > 数据库 > MySQL > MyISAM的OPTIMIZE

MyISAM的OPTIMIZE

原创 MySQL 作者:fei890910 时间:2016-02-29 16:09:59 0 删除 编辑
一些介绍
   通过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


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29108064/viewspace-2016408/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: mysql自增长列
请登录后发表评论 登录
全部评论

注册时间:2013-08-15

  • 博文量
    122
  • 访问量
    811205