14.13 InnoDB and Online DDL

The online DDL feature provides support for in-place table alterations and concurrent DML. Benefits of this feature include:

Typically, you do not need to do anything special to enable online DDL. By default, MySQL performs the operation in place, as permitted, with as little locking as possible.

You can control aspects of a DDL operation using the 'ALGORITHM' and 'LOCK' clauses of the *note 'ALTER TABLE': alter-table. statement. These clauses are placed at the end of the statement, separated from the table and column specifications by commas. For example:

 ALTER TABLE TBL_NAME ADD PRIMARY KEY (COLUMN), ALGORITHM=INPLACE, LOCK=NONE;

The 'LOCK' clause is useful for fine-tuning the degree of concurrent access to the table. The 'ALGORITHM' clause is primarily intended for performance comparisons and as a fallback to the older table-copying behavior in case you encounter any issues. For example:

 File: manual.info.tmp, Node: innodb-online-ddl-operations, Next: innodb-online-ddl-performance, Prev: innodb-online-ddl, Up: innodb-online-ddl

14.13.1 Online DDL Operations

Online support details, syntax examples, and usage notes for DDL operations are provided under the following topics in this section.

Index Operations

The following table provides an overview of online DDL support for index operations. An asterisk indicates additional information, an exception, or a dependency. For details, see *note online-ddl-index-syntax-notes::.

Online DDL Support for Index Operations

Operation In Place Rebuilds Permits Only Table Concurrent Modifies DML Metadata

Creating or adding a secondary index

Yes No Yes No

Dropping an index

Yes No Yes Yes

Renaming an index

Yes No Yes Yes

Adding a 'FULLTEXT' index

Yes* No* No No

Adding a 'SPATIAL' index

Yes No No No

Changing the index type

Yes No Yes Yes

Syntax and Usage Notes

Primary Key Operations

The following table provides an overview of online DDL support for primary key operations. An asterisk indicates additional information, an exception, or a dependency. See *note online-ddl-primary-key-syntax-notes::.

Online DDL Support for Primary Key Operations

Operation In Place Rebuilds Permits Only Table Concurrent Modifies DML Metadata

Adding a primary key

Yes* Yes* Yes No

Dropping a primary key

No Yes No No

Dropping a primary key and adding another

Yes Yes Yes No

Syntax and Usage Notes

Column Operations

The following table provides an overview of online DDL support for column operations. An asterisk indicates additional information, an exception, or a dependency. For details, see *note online-ddl-column-syntax-notes::.

Online DDL Support for Column Operations

Operation In Place Rebuilds Permits Only Table Concurrent Modifies DML Metadata

Adding a column

Yes Yes Yes* No

Dropping a column

Yes Yes Yes No

Renaming a column

Yes No Yes* Yes

Reordering columns

Yes Yes Yes No

Setting a column default value

Yes No Yes Yes

Changing the column data type

No Yes No No

Extending 'VARCHAR' column size

Yes No Yes Yes

Dropping the column default value

Yes No Yes Yes

Changing the auto-increment value

Yes No Yes No*

Making a column 'NULL'

Yes Yes* Yes No

Making a column 'NOT NULL'

Yes* Yes* Yes No

Modifying the definition of an 'ENUM' or 'SET' column

Yes No Yes Yes

Syntax and Usage Notes

Generated Column Operations

The following table provides an overview of online DDL support for generated column operations. For details, see *note online-ddl-generated-column-syntax-notes::.

Online DDL Support for Generated Column Operations

Operation In Place Rebuilds Permits Only Table Concurrent Modifies DML Metadata

Adding a 'STORED' column

No Yes No No

Modifying 'STORED' column order

No Yes No No

Dropping a 'STORED' column

Yes Yes Yes No

Adding a 'VIRTUAL' column

Yes No Yes Yes

Modifying 'VIRTUAL' column order

No Yes No No

Dropping a 'VIRTUAL' column

Yes No Yes Yes

Syntax and Usage Notes

Foreign Key Operations

The following table provides an overview of online DDL support for foreign key operations. An asterisk indicates additional information, an exception, or a dependency. For details, see *note online-ddl-foreign-key-syntax-notes::.

Online DDL Support for Foreign Key Operations

Operation In Place Rebuilds Permits Only Table Concurrent Modifies DML Metadata

Adding a foreign key constraint

Yes* No Yes Yes

Dropping a foreign key constraint

Yes No Yes Yes

Syntax and Usage Notes

Note:

If foreign keys are already present in the table being altered (that is, it is a child table containing a 'FOREIGN KEY ... REFERENCE' clause), additional restrictions apply to online DDL operations, even those not directly involving the foreign key columns:

Table Operations

The following table provides an overview of online DDL support for table operations. An asterisk indicates additional information, an exception, or a dependency. For details, see *note online-ddl-table-syntax-notes::.

Online DDL Support for Table Operations

Operation In Place Rebuilds Permits Only Table Concurrent Modifies DML Metadata

Changing the 'ROW_FORMAT'

Yes Yes Yes No

Changing the 'KEY_BLOCK_SIZE'

Yes Yes Yes No

Setting persistent table statistics

Yes No Yes Yes

Specifying a character set

Yes Yes* Yes No

Converting a character set

No Yes* No No

Optimizing a table

Yes* Yes Yes No

Rebuilding with the 'FORCE' option

Yes* Yes Yes No

Performing a null rebuild

Yes* Yes Yes No

Renaming a table

Yes No Yes Yes

Syntax and Usage Notes

Tablespace Operations

The following table provides an overview of online DDL support for tablespace operations. For details, see *note online-ddl-tablespace-syntax-notes::.

Online DDL Support for Tablespace Operations

Operation In Place Rebuilds Permits Only Table Concurrent Modifies DML Metadata

Enabling or disabling file-per-table tablespace encryption

No Yes No No

Syntax and Usage Notes

Enabling or disabling file-per-table tablespace encryption

 ALTER TABLE TBL_NAME ENCRYPTION='Y', ALGORITHM=COPY;

Encryption is only supported for file-per-table tablespaces. For related information, see *note innodb-data-encryption::.

Partitioning Operations

With the exception of most *note 'ALTER TABLE': alter-table. partitioning clauses, online DDL operations for partitioned 'InnoDB' tables follow the same rules that apply to regular 'InnoDB' tables.

Most note 'ALTER TABLE': alter-table. partitioning clauses do not go through the same internal online DDL API as regular non-partitioned 'InnoDB' tables. As a result, online support for note 'ALTER TABLE': alter-table. partitioning clauses varies.

The following table shows the online status for each 'ALTER TABLE' partitioning statement. Regardless of the online DDL API that is used, MySQL attempts to minimize data copying and locking where possible.

note 'ALTER TABLE': alter-table. partitioning options that use 'ALGORITHM=COPY' or that only permit ''ALGORITHM=DEFAULT, LOCK=DEFAULT'', repartition the table using the 'COPY' algorithm. In other words, a new partitioned table is created with the new partitioning scheme. The newly created table includes any changes applied by the note 'ALTER TABLE': alter-table. statement, and table data is copied into the new table structure.

Online DDL Support for Partitioning Operations

Partitioning In Place Permits Notes Clause DML

*note 'PARTITION BY': alter-table.

No No Permits 'ALGORITHM=COPY', 'LOCK={DEFAULT|SHARED|EXCLUSIVE}'

*note 'ADD PARTITION': alter-table.

No No Only permits 'ALGORITHM=DEFAULT', 'LOCK=DEFAULT'. Does not copy existing data for tables partitioned by 'RANGE' or 'LIST'. Concurrent queries are permitted for tables partitioned by 'HASH' or 'LIST'. MySQL copies the data while holding a shared lock.

*note 'DROP PARTITION': alter-table.

No No Only permits 'ALGORITHM=DEFAULT', 'LOCK=DEFAULT'. Does not copy existing data for tables partitioned by 'RANGE' or 'LIST'.

*note 'DISCARD PARTITION': alter-table.

No No Only permits 'ALGORITHM=DEFAULT', 'LOCK=DEFAULT'

*note 'IMPORT PARTITION': alter-table.

No No Only permits 'ALGORITHM=DEFAULT', 'LOCK=DEFAULT'

*note 'TRUNCATE PARTITION': alter-table.

Yes Yes Does not copy existing data. It merely deletes rows; it does not alter the definition of the table itself, or of any of its partitions.

*note 'COALESCE PARTITION': alter-table.

No No Only permits 'ALGORITHM=DEFAULT', 'LOCK=DEFAULT'. Concurrent queries are permitted for tables partitioned by 'HASH' or 'LIST', as MySQL copies the data while holding a shared lock.

*note 'REORGANIZE PARTITION': alter-table.

No No Only permits 'ALGORITHM=DEFAULT', 'LOCK=DEFAULT'. Concurrent queries are permitted for tables partitioned by 'LINEAR HASH' or 'LIST'. MySQL copies data from affected partitions while holding a shared metadata lock.

*note 'EXCHANGE PARTITION': alter-table.

Yes Yes

*note 'ANALYZE PARTITION': alter-table.

Yes Yes

*note 'CHECK PARTITION': alter-table.

Yes Yes

*note 'OPTIMIZE PARTITION': alter-table.

No No 'ALGORITHM' and 'LOCK' clauses are ignored. Rebuilds the entire table. See *note partitioning-maintenance::.

*note 'REBUILD PARTITION': alter-table.

No No Only permits 'ALGORITHM=DEFAULT', 'LOCK=DEFAULT'. Concurrent queries are permitted for tables partitioned by 'LINEAR HASH' or 'LIST'. MySQL copies data from affected partitions while holding a shared metadata lock.

*note 'REPAIR PARTITION': alter-table.

Yes Yes

*note 'REMOVE PARTITIONING': alter-table.

No No Permits 'ALGORITHM=COPY', 'LOCK={DEFAULT|SHARED|EXCLUSIVE}'

Non-partitioning online note 'ALTER TABLE': alter-table. operations on partitioned tables follow the same rules that apply to regular tables. However, note 'ALTER TABLE': alter-table. performs online operations on each table partition, which causes increased demand on system resources due to operations being performed on multiple partitions.

For additional information about note 'ALTER TABLE': alter-table. partitioning clauses, see note alter-table-partition-options::, and note alter-table-partition-operations::. For information about partitioning in general, see note partitioning::.

 File: manual.info.tmp, Node: innodb-online-ddl-performance, Next: innodb-online-ddl-space-requirements, Prev: innodb-online-ddl-operations, Up: innodb-online-ddl

14.13.2 Online DDL Performance and Concurrency

Online DDL improves several aspects of MySQL operation:

The LOCK clause

By default, MySQL uses as little locking as possible during a DDL operation. The 'LOCK' clause can be specified to enforce more restrictive locking, if required. If the 'LOCK' clause specifies a less restrictive level of locking than is permitted for a particular DDL operation, the statement fails with an error. 'LOCK' clauses are described below, in order of least to most restrictive:

Online DDL and Metadata Locks

Online DDL operations can be viewed as having three phases:

Due to the exclusive metadata lock requirements outlined above, an online DDL operation may have to wait for concurrent transactions that hold metadata locks on the table to commit or rollback. Transactions started before or during the DDL operation can hold metadata locks on the table being altered. In the case of a long running or inactive transaction, an online DDL operation can time out waiting for an exclusive metadata lock. Additionally, a pending exclusive metadata lock requested by an online DDL operation blocks subsequent transactions on the table.

The following example demonstrates an online DDL operation waiting for an exclusive metadata lock, and how a pending metadata lock blocks subsequent transactions on the table.

Session 1:

 mysql> CREATE TABLE t1 (c1 INT) ENGINE=InnoDB;
 mysql> START TRANSACTION;
 mysql> SELECT * FROM t1;

The session 1 *note 'SELECT': select. statement takes a shared metadata lock on table t1.

Session 2:

 mysql> ALTER TABLE t1 ADD COLUMN x INT, ALGORITHM=INPLACE, LOCK=NONE;

The online DDL operation in session 2, which requires an exclusive metadata lock on table t1 to commit table definition changes, must wait for the session 1 transaction to commit or roll back.

Session 3:

 mysql> SELECT * FROM t1;

The note 'SELECT': select. statement issued in session 3 is blocked waiting for the exclusive metadata lock requested by the note 'ALTER TABLE': alter-table. operation in session 2 to be granted.

You can use *note 'SHOW FULL PROCESSLIST': show-processlist. to determine if transactions are waiting for a metadata lock.

 mysql> SHOW FULL PROCESSLIST\G
 ...
 *************************** 2. row ***************************
      Id: 5
    User: root
    Host: localhost
      db: test
 Command: Query
    Time: 44
   State: Waiting for table metadata lock
    Info: ALTER TABLE t1 ADD COLUMN x INT, ALGORITHM=INPLACE, LOCK=NONE
 ...
 *************************** 4. row ***************************
      Id: 7
    User: root
    Host: localhost
      db: test
 Command: Query
    Time: 5
   State: Waiting for table metadata lock
    Info: SELECT * FROM t1
 4 rows in set (0.00 sec)

Metadata lock information is also exposed through the Performance Schema note 'metadata_locks': performance-schema-metadata-locks-table. table, which provides information about metadata lock dependencies between sessions, the metadata lock a session is waiting for, and the session that currently holds the metadata lock. For more information, see note performance-schema-metadata-locks-table::.

Online DDL Performance

The performance of a DDL operation is largely determined by whether the operation is performed in place and whether it rebuilds the table.

To assess the relative performance of a DDL operation, you can compare results using 'ALGORITHM=INPLACE' with results using 'ALGORITHM=COPY'. Alternatively, you can compare results with 'old_alter_table' disabled and enabled.

For DDL operations that modify table data, you can determine whether a DDL operation performs changes in place or performs a table copy by looking at the 'rows affected' value displayed after the command finishes. For example:

Before running a DDL operation on a large table, check whether the operation is fast or slow as follows:

  1. Clone the table structure.

  2. Populate the cloned table with a small amount of data.

  3. Run the DDL operation on the cloned table.

  4. Check whether the 'rows affected' value is zero or not. A nonzero value means the operation copies table data, which might require special planning. For example, you might do the DDL operation during a period of scheduled downtime, or on each replica server one at a time.

Note:

For a greater understanding of the MySQL processing associated with a DDL operation, examine Performance Schema and 'INFORMATION_SCHEMA' tables related to 'InnoDB' before and after DDL operations to see the number of physical reads, writes, memory allocations, and so on.

Performance Schema stage events can be used to monitor note 'ALTER TABLE': alter-table. progress. See note monitor-alter-table-performance-schema::.

Because there is some processing work involved with recording the changes made by concurrent DML operations, then applying those changes at the end, an online DDL operation could take longer overall than the table-copy mechanism that blocks table access from other sessions. The reduction in raw performance is balanced against better responsiveness for applications that use the table. When evaluating the techniques for changing table structure, consider end-user perception of performance, based on factors such as load times for web pages.

 File: manual.info.tmp, Node: innodb-online-ddl-space-requirements, Next: innodb-online-ddl-single-multi, Prev: innodb-online-ddl-performance, Up: innodb-online-ddl

14.13.3 Online DDL Space Requirements

Online DDL operations have the following space requirements:

 File: manual.info.tmp, Node: innodb-online-ddl-single-multi, Next: innodb-online-ddl-failure-conditions, Prev: innodb-online-ddl-space-requirements, Up: innodb-online-ddl

14.13.4 Simplifying DDL Statements with Online DDL

Before the introduction of online DDL, it was common practice to combine many DDL operations into a single note 'ALTER TABLE': alter-table. statement. Because each note 'ALTER TABLE': alter-table. statement involved copying and rebuilding the table, it was more efficient to make several changes to the same table at once, since those changes could all be done with a single rebuild operation for the table. The downside was that SQL code involving DDL operations was harder to maintain and to reuse in different scripts. If the specific changes were different each time, you might have to construct a new complex *note 'ALTER TABLE': alter-table. for each slightly different scenario.

For DDL operations that can be done in place, you can separate them into individual *note 'ALTER TABLE': alter-table. statements for easier scripting and maintenance, without sacrificing efficiency. For example, you might take a complicated statement such as:

 ALTER TABLE t1 ADD INDEX i1(c1), ADD UNIQUE INDEX i2(c2),
   CHANGE c4_old_name c4_new_name INTEGER UNSIGNED;

and break it down into simpler parts that can be tested and performed independently, such as:

 ALTER TABLE t1 ADD INDEX i1(c1);
 ALTER TABLE t1 ADD UNIQUE INDEX i2(c2);
 ALTER TABLE t1 CHANGE c4_old_name c4_new_name INTEGER UNSIGNED NOT NULL;

You might still use multi-part *note 'ALTER TABLE': alter-table. statements for:

 File: manual.info.tmp, Node: innodb-online-ddl-failure-conditions, Next: innodb-online-ddl-limitations, Prev: innodb-online-ddl-single-multi, Up: innodb-online-ddl

14.13.5 Online DDL Failure Conditions

The failure of an online DDL operation is typically due to one of the following conditions:

 File: manual.info.tmp, Node: innodb-online-ddl-limitations, Prev: innodb-online-ddl-failure-conditions, Up: innodb-online-ddl

14.13.6 Online DDL Limitations

The following limitations apply to online DDL operations:

 File: manual.info.tmp, Node: innodb-data-encryption, Next: innodb-parameters, Prev: innodb-online-ddl, Up: innodb-storage-engine