RDSのMySQLでCloudWatchのFree Spaceに関するAlarmがでることがあります。
RDSに限ったことではないですが、MySQLを運用しているとディスクスペースが減っていき、データをDeleteしてもディスク空き容量が増えないことが多くあります。
データを削除しても、削除した箇所はフリースペースとして確保され、開放されないことが多く、長い間放置しているとディスク容量を圧迫してしまいます。
このようなときには、MySQLのOptimizeを行います。
サンプルとしてtpcc-mysqlのデータを投入しておきます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
./tpcc_load 10.0.1.58 deflag memorycraft xxxxxxxxx 10 |
RDSでデータの多いテーブルの偶数行を削除します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mysql> delete from order_line where ol_number % 2 = 0; | |
Query OK, 1417106 rows affected (45.03 sec) |
削除してしばらくしてもデータ空き容量は増えないため、Optimizeを行なってみます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mysql> OPTIMIZE TABLE order_line; | |
+-------------------+----------+----------+-------------------------------------------------------------------+ | |
| Table | Op | Msg_type | Msg_text | | |
+-------------------+----------+----------+-------------------------------------------------------------------+ | |
| deflag.order_line | optimize | note | Table does not support optimize, doing recreate + analyze instead | | |
| deflag.order_line | optimize | status | OK | | |
+-------------------+----------+----------+-------------------------------------------------------------------+ | |
2 rows in set (57.53 sec) |
"Table does not support optimize"と表示されます。
ドキュメントを見ると、InnoDBではOPTIMIZE TABLEはALTER TABLEにマップされると書いてあります。
そのため、ALTER TABLEを行なっても最適化されるようです。
再びいろいろデータを削除してみます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mysql> delete from stock where s_i_id % 2 = 0; | |
Query OK, 500000 rows affected (49.63 sec) | |
mysql> delete from order_line where ol_o_id % 2 = 0; | |
Query OK, 789689 rows affected (22.29 sec) | |
mysql> delete from customer where c_id % 2 = 0; | |
Query OK, 150000 rows affected (15.08 sec) |
今度はALTER TABLEをしてみます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mysql> ALTER TABLE order_line ENGINE=InnoDB; | |
Query OK, 790679 rows affected (31.92 sec) | |
Records: 790679 Duplicates: 0 Warnings: 0 | |
mysql> ALTER TABLE customer ENGINE=InnoDB; | |
Query OK, 150000 rows affected (14.18 sec) | |
Records: 150000 Duplicates: 0 Warnings: 0 | |
mysql> ALTER TABLE stock ENGINE=InnoDB; | |
Query OK, 500000 rows affected (30.85 sec) | |
Records: 500000 Duplicates: 0 Warnings: 0 |
少し待って、RDSのCloudWatchを見てみます。
すると、OPTIMIZE TABLEやALTER TABLEをした時点で使用領域が開放され、ディスク容量が増えているのがわかります。
ALTER TABLEなのでメンテナンス時間に行う必要がありますが、
追加削除の頻度が高いDBで、ディスク容量を増やすことが難しい場合には丁度よい対策になりますし、アクセスも高速になるため定期的におこなった方がよいかと思います。
オプティマイズに関してはRDSに限ったことではないのですが、CloudWatchを使うとオプティマイズ時期の目安にもなりとても便利です。
以上です。