日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql空洞_optimize table 删除空洞--MYSQL

發布時間:2025/3/21 数据库 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql空洞_optimize table 删除空洞--MYSQL 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

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會鎖定表。

原始數據

1,數據量

mysql> select count(*) as total from ad_visit_history;

+---------+

| total |

+---------+

| 1187096 | //總共有118萬多條數據

+---------+

1 row in set (0.04 sec)

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

[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)

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

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.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,一個月一次,看實際情況而定了。

總結

以上是生活随笔為你收集整理的mysql空洞_optimize table 删除空洞--MYSQL的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。