溫馨提示×

溫馨提示×

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

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

mysql如何進行OPTIMIZE TABLE整理碎片

發(fā)布時間:2021-11-08 10:24:59 來源:億速云 閱讀:104 作者:柒染 欄目:建站服務器

這篇文章給大家介紹mysql如何進行OPTIMIZE TABLE整理碎片,內(nèi)容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

來看看手冊中關于 OPTIMIZE 的描述:

OPTIMIZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name [, tbl_name] ...

如果您已經(jīng)刪除了表的一大部分,或者如果您已經(jīng)對含有可變長度行的表(含有VARCHAR, BLOB或TEXT列的表)進行了很多更改,則應使用
OPTIMIZE TABLE。被刪除的記錄被保持在鏈接清單中,后續(xù)的INSERT操作會重新使用舊的記錄位置。您可以使用OPTIMIZE TABLE來重新
利用未使用的空間,并整理數(shù)據(jù)文件的碎片。

在多數(shù)的設置中,您根本不需要運行OPTIMIZE TABLE。即使您對可變長度的行進行了大量的更新,您也不需要經(jīng)常運行,每周一次或每月一次
即可,只對特定的表運行。

OPTIMIZE TABLE只對MyISAM, BDB和InnoDB表起作用。

注意,在OPTIMIZE TABLE運行過程中,MySQL會鎖定表。


原始數(shù)據(jù)

1,數(shù)據(jù)量

mysql> select count(*) as total from test_history; 
+---------+ 
| total | 
+---------+ 
| 1187096 | //總共有118萬多條數(shù)據(jù) 
+---------+ 
1 row in set (0.04 sec)

2,存放在硬盤中的表文件大小

[root@ www.linuxidc.com test1]# ls |grep visit |xargs -i du {} 
382020 test_history.MYD //數(shù)據(jù)文件占了380M 
127116 test_history.MYI //索引文件占了127M 
12 test_history.frm //結構文件占了12K

3,查看一下索引信息

mysql> show index from test_history from test1; //查看一下該表的索引信息 
+------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | 
+------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
| test_history | 0 | PRIMARY | 1 | id | A | 1187096 | NULL | NULL | | BTREE | | 
| test_history | 1 | ad_code | 1 | ad_code | A | 46 | NULL | NULL | YES | BTREE | | 
| test_history | 1 | unique_id | 1 | unique_id | A | 1187096 | NULL | NULL | YES | BTREE | | 
| test_history | 1 | ad_code_ind | 1 | ad_code | A | 46 | NULL | NULL | YES | BTREE | | 
| test_history | 1 | from_page_url_ind | 1 | from_page_url | A | 30438 | NULL | NULL | YES | BTREE | | 
| test_history | 1 | ip_ind | 1 | ip | A | 593548 | NULL | NULL | YES | BTREE | | 
| test_history | 1 | port_ind | 1 | port | A | 65949 | NULL | NULL | YES | BTREE | | 
| test_history | 1 | session_id_ind | 1 | session_id | A | 1187096 | NULL | NULL | YES | BTREE | | 
+------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
8 rows in set (0.28 sec)

索引信息中的列的信息說明。

Table :表的名稱。
Non_unique:如果索引不能包括重復詞,則為0。如果可以,則為1。
Key_name:索引的名稱。
Seq_in_index:索引中的列序列號,從1開始。
Column_name:列名稱。
Collation:列以什么方式存儲在索引中。在MySQLSHOW INDEX語法中,有值’A’(升序)或NULL(無分類)。
Cardinality:索引中唯一值的數(shù)目的估計值。通過運行ANALYZE TABLE或myisamchk -a可以更新。基數(shù)根據(jù)被存儲為整數(shù)的統(tǒng)計數(shù)據(jù)來計數(shù),所以即使對于小型表,該值也沒有必要是精確的。基數(shù)越大,當進行聯(lián)合時,MySQL使用該索引的機會就越大。
Sub_part:如果列只是被部分地編入索引,則為被編入索引的字符的數(shù)目。如果整列被編入索引,則為NULL。
Packed:指示關鍵字如何被壓縮。如果沒有被壓縮,則為NULL。
Null:如果列含有NULL,則含有YES。如果沒有,則為空。
Index_type:存儲索引數(shù)據(jù)結構方法(BTREE, FULLTEXT, HASH, RTREE)

二,刪除一半數(shù)據(jù)

mysql> delete from test_history where id>598000; //刪除一半數(shù)據(jù) 
Query OK, 589096 rows affected (4 min 28.06 sec)

[root@ www.linuxidc.com test1]# ls |grep visit |xargs -i du {} //相對應的MYD,MYI文件大小沒有變化 
382020 test_history.MYD 
127116 test_history.MYI 
12 test_history.frm

按常規(guī)思想來說,如果在數(shù)據(jù)庫中刪除了一半數(shù)據(jù)后,相對應的.MYD,.MYI文件也應當變?yōu)橹暗囊话搿5莿h除一半數(shù)據(jù)后,.MYD.MYI盡然連1KB都沒有減少,這是多么的可怕啊。

我們在來看一看,索引信息
mysql> show index from test_history; 
+------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | 
+------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
| test_history | 0 | PRIMARY | 1 | id | A | 598000 | NULL | NULL | | BTREE | | 
| test_history | 1 | ad_code | 1 | ad_code | A | 23 | NULL | NULL | YES | BTREE | | 
| test_history | 1 | unique_id | 1 | unique_id | A | 598000 | NULL | NULL | YES | BTREE | | 
| test_history | 1 | ad_code_ind | 1 | ad_code | A | 23 | NULL | NULL | YES | BTREE | | 
| test_history | 1 | from_page_url_ind | 1 | from_page_url | A | 15333 | NULL | NULL | YES | BTREE | | 
| test_history | 1 | ip_ind | 1 | ip | A | 299000 | NULL | NULL | YES | BTREE | | 
| test_history | 1 | port_ind | 1 | port | A | 33222 | NULL | NULL | YES | BTREE | | 
| test_history | 1 | session_id_ind | 1 | session_id | A | 598000 | NULL | NULL | YES | BTREE | | 
+------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
8 rows in set (0.00 sec)

對比一下,這次索引查詢和上次索引查詢,里面的數(shù)據(jù)信息基本上是上次一次的一本,這點還是合乎常理。

三,用optimize table來優(yōu)化一下


show table status like 'XXX'\G;

data_free選項代表數(shù)據(jù)碎片。 
針對MySQL的不同數(shù)據(jù)庫存儲引擎,在optimize使用清除碎片,回收閑置的數(shù)據(jù)庫空間,把分散存儲(fragmented)的數(shù)據(jù)和索引重新挪到一起(defragmentation),對I/O速度有好處。


mysql> optimize table test_history; //刪除數(shù)據(jù)后的優(yōu)化 
+------------------------+----------+----------+----------+ 
| Table | Op | Msg_type | Msg_text | 
+------------------------+----------+----------+----------+ 
| test1.test_history | optimize | status | OK | 
+------------------------+----------+----------+----------+ 
1 row in set (1 min 21.05 sec)

1,查看一下.MYD,.MYI文件的大小

[root@ www.linuxidc.com test1]# ls |grep visit |xargs -i du {} 
182080 test_history.MYD //數(shù)據(jù)文件差不多為優(yōu)化前的一半 
66024 test_history.MYI //索引文件也一樣,差不多是優(yōu)化前的一半 
12 test_history.frm

2,查看一下索引信息
mysql> show index from test_history; 
+------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | 
+------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
| test_history | 0 | PRIMARY | 1 | id | A | 598000 | NULL | NULL | | BTREE | | 
| test_history | 1 | ad_code | 1 | ad_code | A | 42 | NULL | NULL | YES | BTREE | | 
| test_history | 1 | unique_id | 1 | unique_id | A | 598000 | NULL | NULL | YES | BTREE | | 
| test_history | 1 | ad_code_ind | 1 | ad_code | A | 42 | NULL | NULL | YES | BTREE | | 
| test_history | 1 | from_page_url_ind | 1 | from_page_url | A | 24916 | NULL | NULL | YES | BTREE | | 
| test_history | 1 | ip_ind | 1 | ip | A | 598000 | NULL | NULL | YES | BTREE | | 
| test_history | 1 | port_ind | 1 | port | A | 59800 | NULL | NULL | YES | BTREE | | 
| test_history | 1 | session_id_ind | 1 | session_id | A | 598000 | NULL | NULL | YES | BTREE | | 
+------------------+------------+-------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+ 
8 rows in set (0.00 sec)

從以上數(shù)據(jù)我們可以得出,ad_code,ad_code_ind,from_page_url_ind等索引機會差不多都提高了85%,這樣效率提高了好多。

四,小結

結合mysql官方網(wǎng)站的信息,個人是這樣理解的。當你刪除數(shù)據(jù)時,mysql并不會回收,被已刪除數(shù)據(jù)的占據(jù)的存儲空間,以及索引位。而是空在那里,而是等待新的數(shù)據(jù)來彌補這個空缺,這樣就有一個缺少,如果一時半會,沒有數(shù)據(jù)來填補這個空缺,那這樣就太浪費資源了。所以對于寫比較頻煩的表,要定期進行optimize,一個月一次,看實際情況而定了。

關于mysql如何進行OPTIMIZE TABLE整理碎片就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

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

AI