今天在看CU的時候,發現有人問有關optimize來表最佳化的問題,當年因為這個問題,困擾我很長一段時間,今天有空我把這個問題,用實際資料來展示出來,讓大家可以親眼來看看,optimize table的重要作用,而不是似是而非的估計了。
一,未經處理資料
1,資料量
mysql> select count(*) as total from ad_visit_history;
+---------+
| total |
+---------+
| 1187096 | //總共有118萬多條資料
+---------+
1 row in set (0.04 sec)
2,存放在硬碟中的表檔案大小
[root@ www.bkjia.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)
索引資訊中的列的資訊說明。
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)
二,刪除一半資料
mysql> delete from ad_visit_history where id>598000; //刪除一半資料
Query OK, 589096 rows affected (4 min 28.06 sec)
[root@ www.bkjia.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)
對比一下,這次索引查詢和上次索引查詢,裡面的資料資訊基本上是上次一次的一本,這點還是合乎常理。