共計 3420 個字符,預計需要花費 9 分鐘才能閱讀完成。
這篇文章主要介紹“MyISAM 的 OPTIMIZE 有什么作用”,在日常操作中,相信很多人在 MyISAM 的 OPTIMIZE 有什么作用問題上存在疑惑,丸趣 TV 小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”MyISAM 的 OPTIMIZE 有什么作用”的疑惑有所幫助!接下來,請跟著丸趣 TV 小編一起來學習吧!
一些介紹
通過 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 有什么作用”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注丸趣 TV 網站,丸趣 TV 小編會繼續努力為大家帶來更多實用的文章!