mysql optimize innodb 最佳化詳解

來源:互聯網
上載者:User


如果對mysql表,進行大範圍刪除後,最好做一個表最佳化,這樣會快一些。以前寫過一篇類似的,針對myisam儲存引擎的。請參考:執行個體說明optimize table在最佳化mysql時很重要

1,刪除資料,並嘗試最佳化


mysql> delete FROM `test` WHERE d_id>397136; 
Query OK, 306356 rows affected (19.48 sec) 
 
mysql> optimize table test; 
+------------+----------+----------+-------------------------------------------------------------------+ 
| Table      | Op       | Msg_type | Msg_text                                                          | 
+------------+----------+----------+-------------------------------------------------------------------+ 
| test1.test | optimize | note     | Table does not support optimize, doing recreate + analyze instead | 
| test1.test | optimize | status   | OK                                                                | 
+------------+----------+----------+-------------------------------------------------------------------+ 
2 rows in set (0.35 sec) 
執行optimize的時候,非常慢,可能會卡死。msg_text資訊的意思是innodb引擎,不支援optimize。在官網找了一下,發現以下內空
Table does not support optimize, doing recreate + analyze instead.
It is because the table that you are using is InnoDB.
You can optimize the InnoDB tables by using this.
ALTER TABLE table.name ENGINE='InnoDB';

一般情況下,由myisam轉成innodb,會用alter table table.name engine='innodb'進行轉換,最佳化也可以用這個

2,alter最佳化資料


mysql> alter table test engine='innodb'; 
Query OK, 384781 rows affected (19.88 sec) 
Records: 384781  Duplicates: 0  Warnings: 0 
alter如果很慢,最佳化一下my.cnf,[mysqld]加上以下內容,並重新載入。
innodb_buffer_pool_size=1G 
innodb_file_io_threads=4 
innodb_file_io_threads常規配置,小於等CPU核心數。innodb_buffer_pool_size小於等於實體記憶體的1/2,原則上夠用就好。

3,最佳化後的對比


mysql> use information_schema 
 
mysql> select concat(round(sum(data_length/1024/1024),2),'MB') as data_length_MB, 
 -> concat(round(sum(index_length/1024/1024),2),'MB') as index_length_MB 
 -> from tables where 
 -> table_schema='test1' 
 -> and table_name = 'test'; 
+----------------+-----------------+ 
| data_length_MB | index_length_MB | 
+----------------+-----------------+ 
| 20.55MB        | 27.55MB         |       //最佳化前 
+----------------+-----------------+ 
1 row in set (0.01 sec) 
 
mysql> select concat(round(sum(data_length/1024/1024),2),'MB') as data_length_MB, 
 -> concat(round(sum(index_length/1024/1024),2),'MB') as index_length_MB 
 -> from tables where 
 -> table_schema='test1' 
 -> and table_name = 'test'; 
+----------------+-----------------+ 
| data_length_MB | index_length_MB | 
+----------------+-----------------+ 
| 20.55MB        | 16.55MB         |    //最佳化後 
+----------------+-----------------+ 
1 row in set (0.00 sec) 
data_length_MB,第一列是資料;index_length_MB,第二列是索引

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.