MyISAM的OPTIMIZE有什么作用

发布时间:2021-11-19 11:41:57 作者:iii
来源:亿速云 阅读:142

这篇文章主要介绍“MyISAM的OPTIMIZE有什么作用”,在日常操作中,相信很多人在MyISAM的OPTIMIZE有什么作用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”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

到此,关于“MyISAM的OPTIMIZE有什么作用”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. MyISAM特点
  2. TEZ MRR optimize to MR?

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

myisam optimize

上一篇:docker中k8s基于canal的网络策略是什么

下一篇:docker中ingress资源和ingress controller的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》