mysql中optimize表命令測試例子

來源:互聯網
上載者:User

今天對myiam資料表進行大量刪除後,發現空間沒有回收,查了資料後,發現要通過optimize table來回收空間
測試如下,建立資料表:

 代碼如下 複製代碼
CREATE TABLE `ttext` (
`id` int(11) DEFAULT NULL,
`context` text
) ENGINE=MyISAM DEFAULT CHARSET=latin1
insert into ttext values( 1,’tiger1′),(2,’tiger2′),(3,’tiger3′);

然後重複執行insert into ttext select * from ttext;這樣重複灌資料,很快資料檔案就達到240MB,這個時候,可以確認的是id為1的資料佔據了1/3的空間,那麼接下來,可以刪除delete from ttext where id=1;
按正常的想法,這樣的操作後,空間應該有回收的,但是事實沒有。
OPTIMIZE TABLE should be used if you have deleted a large part of a
table or if you have made many changes to a table with variable-length
rows (tables that have VARCHAR, VARBINARY, BLOB, or TEXT columns).
You can use OPTIMIZE TABLE to
reclaim the unused space and to defragment the data file.
通過資料來看可以通過optimize table ttext來回收相應的空間。
繼續查看了官方手冊後發現,這個操作與表所用的引擎有很大的關係。
innodb表必須是使用獨立資料表空間的才行,其次在mysql5.1.27開始optimize table is also supported for partitioned tables.就是可以對分區表進行最佳化。
針對myisam引擎,使用optimize table 還有如下功能:
If the table has deleted or split rows, repair the table. [修複表]
If the index pages are not sorted, sort them. [索引未排序,會排序]
If the table’s statistics are not up to date (and the repair could not be accomplished by sorting the index), update them.[若表的統計資訊不是最新的,更新它]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.