表碎片處理方法OPTIMIZE

來看看手冊中關於 OPTIMIZE 的描述:mysql

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

若是您已經刪除了表的一大部分,或者若是您已經對含有可變長度行的表(含有VARCHAR, BLOB或TEXT列的表)進行了不少更改,則應使用
OPTIMIZE TABLE。被刪除的記錄被保持在連接清單中,後續的INSERT操做會從新使用舊的記錄位置。您可使用OPTIMIZE TABLE來從新
利用未使用的空間,並整理數據文件的碎片。

在多數的設置中,您根本不須要運行OPTIMIZE TABLE。即便您對可變長度的行進行了大量的更新,您也不須要常常運行,每週一次或每個月一次
便可,只對特定的表運行。

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

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


原始數據linux

1,數據量sql

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

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

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

3,查看一下索引信息優化

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

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

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

二,刪除一半數據

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

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

按常規思想來講,若是在數據庫中刪除了一半數據後,相對應的.MYD,.MYI文件也應當變爲以前的一半。可是刪除一半數據後,.MYD.MYI盡然連1KB都沒有減小,這是多麼的可怕啊。

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

對比一下,此次索引查詢和上次索引查詢,裏面的數據信息基本上是上次一次的一本,這點仍是合乎常理。

 

三,用optimize table來優化一下

??mysql> optimize table ad_visit_history; //數據表數據刪除或更新後留下的碎片優化語句
+------------------------+----------+----------+----------+ 
| Table | Op | Msg_type | Msg_text | 
+------------------------+----------+----------+----------+ 
| test1.ad_visit_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 ad_visit_history.MYD //數據文件差很少爲優化前的一半 
66024 ad_visit_history.MYI //索引文件也同樣,差很少是優化前的一半 
12 ad_visit_history.frm

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

從以上數據咱們能夠得出,ad_code,ad_code_ind,from_page_url_ind等索引機會差很少都提升了85%,這樣效率提升了好多。

四,小結

結合mysql官方網站的信息,我的是這樣理解的。當你刪除數據時,mysql並不會回收,被已刪除數據的佔據的存儲空間,以及索引位。而是空在那裏,而是等待新的數據來彌補這個空缺,這樣就有一個缺乏,若是一時半會,沒有數據來填補這個空缺,那這樣就太浪費資源了。因此對於寫比較頻煩的表,要按期進行optimize,一個月一次,看實際狀況而定了。

相關文章
相關標籤/搜索