溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MyISAM的OPTIMIZE有什么作用

發(fā)布時(shí)間:2021-11-19 11:41:57 來源:億速云 閱讀:150 作者:iii 欄目:MySQL數(shù)據(jù)庫

這篇文章主要介紹“MyISAM的OPTIMIZE有什么作用”,在日常操作中,相信很多人在MyISAM的OPTIMIZE有什么作用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”MyISAM的OPTIMIZE有什么作用”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

一些介紹
   通過OPTIMIZE 命令來整理MyISAM 表的文件。這就像我們使用Windows 操作系統(tǒng)會(huì)每過一段時(shí)間
后都會(huì)做一次磁盤碎片整理,讓系統(tǒng)中的文件盡量使用連續(xù)空間,提高文件的訪問速度。MyISAM 在
通過OPTIMIZE 優(yōu)化整理的時(shí)候,主要也是將因?yàn)閿?shù)據(jù)刪除和更新造成的碎片空間清理,使整個(gè)文件
連續(xù)在一起。一般來說,在每次做了較大的數(shù)據(jù)刪除操作之后都需要做一次OPTIMIZE 操作。而且每
個(gè)季度都應(yīng)該有一次OPTIMIZE 的維護(hù)操作。

一、創(chuàng)建一張表,并插入大量數(shù)據(jù)
(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
二、刪除表部分?jǐn)?shù)據(jù)
(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)

四、操作完成之后,索引和數(shù)據(jù)都減小
[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

到此,關(guān)于“MyISAM的OPTIMIZE有什么作用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI