When you work with large MySQL tables, running the DELETE statement does not immediately release disk space. Instead, it only marks database records or data pages as reusable. To reclaim the tablespace and reduce disk usage, you can run the OPTIMIZE TABLE command.
Prerequisites
-
The
OPTIMIZE TABLEstatement is supported only by the InnoDB and MyISAM storage engines. -
The instance's available disk space must be at least the size of the table you want to optimize.
NoteIf your instance has insufficient disk space, you must first expand the disk space. Afterward, you can scale down the disk space, and the system will refund the price difference.
Usage notes
-
Delete a large amount of data first: Running
OPTIMIZE TABLEis ineffective unless you first useDELETEto remove a significant amount of data. -
Temporary increase in disk space usage: When you run
OPTIMIZE TABLE, MySQL creates a temporary table to store the reorganized data. This causes a brief increase in disk space usage. After the operation completes, the temporary table is deleted, and disk space usage returns to normal. -
Table and index statistics may not change after space is reclaimed: Although the disk space is reclaimed, MySQL table statistics may not be updated immediately. For more information, see Why does the disk space usage of my ApsaraDB RDS for MySQL instance remain unchanged after I run OPTIMIZE TABLE?
-
Performance impact and risks during peak hours: On ApsaraDB RDS for MySQL 5.7 and 8.0,
OPTIMIZE TABLEuses online DDL, which supports concurrent DML operations. However, running this operation on large tables can cause sudden spikes in I/O and buffer resource consumption, creating a risk of table locking or resource contention. During peak hours, this can even lead to instance unavailability or interruptions in monitoring. Therefore, we recommend that you perform this operation during off-peak hours to avoid affecting your business. -
Manually terminating an OPTIMIZE TABLE operation: Pressing Ctrl+C in a database client, such as the MySQL command-line tool or a DMS SQL window, only disconnects the current client session. It does not stop the OPTIMIZE TABLE operation on the backend. To terminate the operation, you must establish another database connection, run
SHOW PROCESSLIST;to find the thread ID of the OPTIMIZE TABLE operation, and then runKILL <thread ID>;to terminate that thread.
Use the command line
-
Connect to your ApsaraDB RDS for MySQL instance by using a database client.
-
Use the
DELETEstatement to clean up unwanted data based on your business requirements. -
Run the
OPTIMIZE TABLEcommand to reclaim the tablespace.OPTIMIZE TABLE <$Database1>.<Table1>,<$Database2>.<Table2>;Note-
<$Database1> and <$Database2> represent database names, and <Table1> and <Table2> represent table names.
-
When you run
OPTIMIZE TABLEon an InnoDB table, the command returns the following message. This is expected behavior and can be ignored. The operation is successful if the final result is "ok". For more information, see the official MySQL documentation for OPTIMIZE TABLE Statement.Table does not support optimize, doing recreate + analyze instead
-
Use DMS
-
In the navigation pane on the left, select the target instance ID, double-click the database, right-click a target table, and then select Batch operation table.
-
Select the tables that you want to optimize, and then choose .
-
Verify the information in the dialog box and click OK.