All Products
Search
Document Center

ApsaraDB RDS:AliSQL DDL best practices

Last Updated:Jul 08, 2025

DDL is one of the most common operations in MySQL. As MySQL has evolved, its DDL capabilities have continuously improved. This topic introduces the development process of MySQL DDL capabilities, explains common DDL operations, and provides optimization techniques and best practices for DDL operations in AliSQL.

DDL capability development

  • MySQL 5.5 and earlier: Only supports the COPY algorithm.

    • Execution process: Creates a temporary table, copies data row by row, and then swaps tables.

    • Limitation: The table is read-only during DDL execution and cannot be written to.

  • MySQL 5.6 & 5.7: Supports the INPLACE algorithm.

    • Execution process: The engine layer handles DDL without requiring a temporary table.

    • Advantage: Supports reading and writing to the table during DDL execution.

    • Limitation: The table may be briefly locked (no read or write operations) at the beginning and end of DDL execution, and temporary space is consumed.

  • MySQL 8.0: Supports the INSTANT algorithm.

    • Execution process: Only modifies metadata without copying data.

    • Advantage: Completes in seconds, significantly reducing the impact of DDL on business operations.

    • Limitation: Supports limited DDL types (adding and dropping columns). At the beginning and end of DDL execution, an MDL X lock (no read or write operations) is still briefly required on the table.

image

Note

All algorithms require an MDL X lock during the start and end phases of DDL execution (phases 1 and 3 in the above diagram), resulting in a brief table lock (no read or write operations). AliSQL has optimized MDL lock waiting. For more information, see AliSQL DDL optimization features.

MySQL common DDL operations

MySQL supports specifying an algorithm when executing DDL operations. If not specified, MySQL selects the best algorithm based on the DDL operation type. The following describes the execution logic and impact of common operations (with the default InnoDB engine).

Table operations

DDL operation

DDL command

Default algorithm and whether it can be modified

Read/write property during DDL execution

Potential impact

Rename table

RENAME

Supports algorithm modification, default value:

  • 5.6, 5.7: INPLACE

  • 8.0: INSTANT

Read/write

Only modifies metadata, no impact.

Rebuild table

OPTIMIZE / ALTER ENGINE

Does not support algorithm modification, default value for 5.6, 5.7, and 8.0: INPLACE.

Read/write

Note

During AliSQL 8.0 data archiving, the table is read-only and cannot be written to.

  • Rebuilding a table involves data copying and requires reserved disk space.

  • Large tables take a long time.

  • May cause replication delay on read-only instances and replicas.

Update statistics

ANALYZE

Does not support algorithm modification.

Read/write

Only updates statistics information, but in some scenarios, it may cause business blocking. For more information, see the ANALYZE TABLE blocking scenarios in the Special cases tab.

Modify character set

CONVERT CHARSET

  • 5.6, 5.7: Does not support algorithm modification, default value COPY.

  • 8.0: Supports algorithm modification, default value COPY. Only supports INPLACE in special scenarios. For more information, see Using the INPLACE algorithm when modifying character sets (CONVERT CHARSET) in the Special cases tab.

Read-only, write operations are not allowed.

  • Copies data to a temporary table, requires reserved disk space.

  • Large tables take a long time.

  • May cause replication delay on read-only instances and replicas.

Modify table comment

ALTER COMMENT

Does not support algorithm modification, default value for 5.6, 5.7, and 8.0: INPLACE.

Read/write

Only modifies metadata, no impact.

Column operations

DDL operation

DDL command

Default algorithm and whether it can be modified

Read/write property during DDL execution

Potential impact

Rename column

RENAME

Supports algorithm modification, default value:

  • 5.6, 5.7: INPLACE

  • 8.0: INSTANT

Read/write

Only modifies metadata, no impact.

Add column

ADD

Supports algorithm modification, default value:

  • 5.6, 5.7: INPLACE

  • 8.0: INSTANT

Read/write

  • 5.6 and 5.7: Rebuilds the table, requires reserved disk space.

  • 8.0: Only modifies metadata, no impact.

Drop column

DROP

Supports algorithm modification, default value:

  • 5.6, 5.7: INPLACE

  • 8.0: INSTANT

Read/write

Modify column type

CHANGE / MODIFY

Supports algorithm modification, default value for 5.6, 5.7, and 8.0: COPY. VARCHAR special scenarios support INPLACE. For more information, see Using the INPLACE algorithm when modifying VARCHAR field length.

Read-only, write operations are not allowed.

  • Copies data to a temporary table, requires reserved disk space.

  • Large tables take a long time.

  • May cause replication delay on read-only instances and replicas.

Modify column character set

ALTER CHARSET

Read-only, write operations are not allowed.

Modify column comment

ALTER COMMENT

Supports algorithm modification, default value:

  • 5.6, 5.7: INPLACE

  • 8.0: INSTANT

Read/write

Only modifies metadata, no impact.

Index operations

DDL operation

DDL command

Default algorithm and whether it can be modified

Read/write property during DDL execution

Potential impact

Rename index

RENAME

Supports algorithm modification, default value:

  • 5.6, 5.7: INPLACE

  • 8.0: INSTANT

Read/write

Only modifies metadata, no impact.

Create index

ADD / CREATE

Supports algorithm modification, default value for 5.6, 5.7, and 8.0: INPLACE.

Read/write

  • Requires building a B+ tree and writing data, which consumes index space.

  • Large tables take a long time.

  • May cause replication delay on read-only instances and replicas.

Drop index

DROP

Supports algorithm modification, default value for 5.6, 5.7, and 8.0: INPLACE.

Read/write

Only modifies metadata, no impact.

Special cases

Using the INPLACE algorithm when modifying VARCHAR field length

VARCHAR type fields have a critical value for length. When you need to use the INPLACE algorithm to modify the length of a VARCHAR type field, you must ensure that both the length before and after modification are less than or equal to the critical value, or both are greater than the critical value. If you need to change across the critical value, you must use the COPY algorithm. Different character sets have different critical values (number of characters):

Character set

Critical value (number of characters)

Length change range

Available algorithms

utf8 / utf8mb3

85

Length before and after modification both in (0,85]

INPLACE, COPY

Length before and after modification both in (85,65535]

Cross-critical value change

COPY

utf8mb4

63

Length before and after modification both in (0,63]

INPLACE, COPY

Length before and after modification both in (63,65535]

Cross-critical value change

COPY

latin1

255

Length before and after modification both in (0,255]

INPLACE, COPY

Length before and after modification both in (255,65535]

Cross-critical value change

COPY

Critical value explanation

When storing VARCHAR type data, InnoDB uses 1 or 2 bytes to store the data length. 1 byte can store a maximum length of 2^8 - 1 = 255. When the length exceeds 255, 2 bytes are needed for storage. The critical value (in bytes) for all character sets is 255.

However, generally, the field length in DDL commands is set using number of characters, such as VARCHAR(10) indicating that the field length is 10 characters. In different character sets, the conversion ratio between characters and bytes varies (for example, in the utf8 character set, 1 character converts to 3 bytes, and in the utf8mb4 character set, 1 character converts to 4 bytes). Therefore, if the field length is counted in characters, the critical value (number of characters) will also differ across character sets.

Character set

Critical value (bytes)

Critical value (characters)

Conversion ratio

utf8 / utf8mb3

255

85

1 character equals 3 bytes

utf8mb4

63

1 character equals 4 bytes

latin1

255

1 character equals 1 byte

The INPLACE algorithm does not support modifying field length by changing the storage space size. Therefore, the field length values before and after modification must both use 1-byte storage (less than or equal to the critical value) or both use 2-byte storage (greater than the critical value).

Using the INPLACE algorithm when modifying character sets (CONVERT CHARSET)

Character set modification operations modify all columns with character set information in the table. Currently, MySQL columns with character set information include CHAR, VARCHAR, TEXT, ENUM, and SET. When you need to use the INPLACE algorithm to modify character sets, you must meet the following conditions:

  • MySQL version 8.0 or later.

  • If the table contains CHAR or VARCHAR type fields, these columns cannot be indexed (or part of an index), and the column length must be less than or equal to the minimum of the two critical values (before and after character set modification) or greater than the maximum of the two critical values.

Taking the change from utf8 / utf8mb3 character set to utf8mb4 character set as an example:

  • The critical value for utf8 / utf8mb3 character set is 85.

  • The critical value for utf8mb4 character set is 63.

When the table contains CHAR or VARCHAR type fields that are not index columns, the corresponding field length needs to be in the range of (0,63] or (85,65535] to use the INPLACE algorithm for character set modification.image

ANALYZE TABLE blocking scenarios

  • Issue: If the table has unfinished slow SQL statements, all table access after ANALYZE needs to wait for the slow SQL to finish before refreshing the table cache.

  • Solution: This issue has been fixed in MySQL 8.0.

DDL execution time estimation

Method 1: Estimation based on Performance Schema (PFS)

Starting from MySQL 5.7, DDL-related Stage statistics were added to PFS. You can view DDL progress through processlist and the events_stages_current table in the P_S database, and estimate the remaining time.

  • Applicable scenario: Real-time monitoring of DDL progress during execution.

  • Query statement:

    SELECT * FROM information_schema.processlist WHERE ID = <Replace with actual ID>;
    SELECT THREAD_ID, EVENT_ID, EVENT_NAME, WORK_COMPLETED, WORK_ESTIMATED,(WORK_COMPLETED/WORK_ESTIMATED)*100 AS PROGRESS  
    FROM performance_schema.events_stages_current WHERE THREAD_ID = <Replace with actual ID>;  
    Note

    Execute the SHOW Processlist command to obtain the ID based on the executed SQL.

  • Example: For a local Sysbench 10 GB table, the query result shows TIME=116, PROGRESS=83.9961%, as shown in the following figure:image

    • Calculated time = TIME / PROGRESS = 116 / (83.9961%) = 138.1 s

    • Actual execution time: 2 min 3.14 s = 123.14 simage

Method 2: Estimation based on tablespace size

  • Applicable scenario: Estimation without enabling PFS (which consumes memory and affects performance, RDS MySQL disables it by default).

  • Steps:

    1. Query table size:

      SELECT * FROM information_schema.tables WHERE TABLE_SCHEMA =  <Replace with actual database name> AND TABLE_NAME = <Replace with actual table name>;  
    2. Based on the DATA_LENGTH size obtained from the query, directly estimate the DDL time. Taking the OPTIMIZE TABLE operation as an example:

      • Cloud disk instances (premium performance disk and ESSD cloud disk): Estimate at a speed of 30~60 MB/s.

      • High-performance local disk instances: Estimate at a speed of 50~100 MB/s.

        Note

        The speeds of 30~60 MB/s and 50~100 MB/s are estimated values based on the IO capability of the instance and may vary in practice.

    3. (Optional) Index impact: If the table has many secondary indexes, you also need to consider the time for sorting and building secondary indexes. Estimate the index building time at 1/3 of the main table speed.

  • Example: For a local Sysbench 10 GB table, the query result is as follows:image

    • Calculated time = DATA_LENGTH / 1024 / 1024 / 100 + INDEX_LENGTH / 1024 / 1024 / 100 × 3 = 10270785536 / 1024 / 1024 / 100 + 665829376 / 1024 / 1024 / 100 × 3 = 117 s

    • Actual execution time: 2 min 3.14 s = 123.14 simage

AliSQL DDL best practices

Basic principles

  1. DDL is a change operation. Try to avoid business peak hours.

  2. DDL operations that support the INSTANT algorithm, or other operations that only modify metadata, can be executed safely (to avoid being unable to obtain MDL X locks at the beginning and end of the operation, still pay close attention to slow queries and large transactions).

  3. DDL operations that only support the COPY algorithm make the table read-only during execution and do not allow write operations. Execute these operations with caution.

  4. Other DDL operations that support the INPLACE algorithm allow the table to be read and written during execution, but you need to comprehensively evaluate the execution time, space usage, and potential replication delay issues.

AliSQL DDL optimization features

  • Large table deletion optimization

    • Feature: Asynchronously cleans up large files to avoid file system jitter.

    • Parameter: innodb_data_file_purge=ON (enabled by default).

  • Buffer Pool management optimization

    • Feature: Reduces DDL execution time and lowers the performance impact of DDL operations.

    • Parameter: loose_innodb_rds_faster_ddl=ON (needs to be manually enabled).

  • Second-level column addition

    • Feature: Metadata change without rebuilding the table.

    • Parameter: loose_innodb_instant_ddl_enabled=ON (enabled by default in MySQL 8.0, needs to be manually enabled in version 5.7 and below).

  • Other optimizations

    • AHI cleanup optimization: Optimizes performance issues caused by AHI cleanup during table and index deletion. Parameter: innodb_rds_drop_ahi_ahead, default is OFF.

    • Unique index conflict optimization: Optimizes issues where unique index conflicts cause DDL failures during DDL operations.

    • MDL lock wait logic optimization: Optimizes the blocking wait for MDL X locks to non-blocking wait, solving the problem of business-wide blocking caused by DDL operations that cannot obtain MDL locks for a long time.

    • ANALYZE TABLE optimization: Solves the problem where ANALYZE TABLE may cause business blocking in MySQL 5.7.

    • Parallel DDL: Fixes DDL performance regression issues.

References