MySQL Storage Space Optimization

MySQL users need to closely monitor the space utilization of their instances, as this is one of the key indicators of daily monitoring. If the storage space usage of an instance reaches its limit, it can severely impact the normal operation of the instance, leading to issues such as database write failures, backups not completing properly, and prolonged execution time for storage expansion tasks. Typically, when the storage space utilization of a MySQL instance reaches 80-85%, timely measures should be taken, either by reducing the actual database space usage or by expanding the storage space to avoid the risk of reaching the limit.

Recommendations for Optimizing Storage Space Issues

Excessive binlog Files

Enabling the binlog feature in MySQL allows for recording all modification operations on the database, which is crucial for scenarios such as disaster recovery and master-slave replication. However, if binlog files are not cleaned up in a timely manner, it may lead to excessive space usage. To reduce space wastage, it is advisable to regularly back up data and adjust the retention period of binlog files based on business requirements. This ensures the security of the database while minimizing the risk of space occupation.

Excessive Indexes

Having too many indexes can amplify the I/O throughput during writes. To reduce unnecessary space wastage, it is important to configure secondary indexes judiciously.

Large Fields

If the table structure definition includes large fields such as BLOB, TEXT, or very long VARCHAR, it will lead to larger table space usage. It is recommended to disable BLOB and TEXT types in favor of using JSON type or other document databases for storing this data. If necessary, data can be compressed before insertion.

Excessive Free Table Space (High Fragmentation Rate of InnoDB Tables)

InnoDB manages table space by Pages (16KB). If a Page is filled with records and then some records are deleted via DELETE SQL, subsequent deletions may lead to fragmentation of the table space if no new records are inserted in the deleted records' positions. This causes InnoDB to perform additional I/O operations to access scattered fragmented pages, negatively affecting database performance.

To check the free space on a table, you can use the following SQL statement:

show table status like '<table_name>'

If the free space is particularly large, you can execute the following statement to reorganize the table space.

optimize table <table_name>

Excessive Temporary Table Space

Certain operations generate temporary tables, such as semi-joins, distinct operations, and sorting without using indexes. If the amount of data involved is large, it may lead to excessive temporary table space. By checking the execution plan, you can determine if it includes Using Temporary.

Moreover, when DDL statements rebuild the table space, if the table is particularly large, the temporary files generated during index creation can also be quite large. If there is insufficient space, storage should be expanded in advance.