当前位置: 首页 > MySQL数据库

MyISAM的OPTIMIZE有什么作用

时间:2026-01-28 14:10:28

一些介绍
   通过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


上一篇:mysql的thread_running数量分析
下一篇:Mysql升级为GTID模式的方法是什么
myisam optimize
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器
  • 英特尔第五代 Xeon CPU 来了:详细信息和行业反应
  • 由于云计算放缓引发扩张担忧,甲骨文股价暴跌
  • Web开发状况报告详细介绍可组合架构的优点
  • 如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳
  • 美光在数据中心需求增长后给出了强有力的预测
  • 2027服务器市场价值将接近1960亿美元
  • 生成式人工智能的下一步是什么?
  • 分享在外部存储上安装Ubuntu的5种方法技巧
  • 全球数据中心发展的关键考虑因素
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器

    英特尔第五代 Xeon CPU 来了:详细信息和行业反应

    由于云计算放缓引发扩张担忧,甲骨文股价暴跌

    Web开发状况报告详细介绍可组合架构的优点

    如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳

    美光在数据中心需求增长后给出了强有力的预测

    2027服务器市场价值将接近1960亿美元

    生成式人工智能的下一步是什么?

    分享在外部存储上安装Ubuntu的5种方法技巧

    全球数据中心发展的关键考虑因素