InnoDB Online DDL Operations with ALGORITHM=INPLACE
Contents
Column Operations
ALTER TABLE ... ADD COLUMN
InnoDB supports adding columns to a table with ALGORITHM=INPLACE
.
The table is rebuilt, which means that all of the data is reorganized substantially, and the indexes are rebuilt. As a result, the operation is quite expensive.
With the exception of adding an auto-increment column, concurrent DML is permitted.
For example:
CREATE TABLE tab ( a int PRIMARY KEY, b varchar(50) ); SET SESSION alter_algorithm='INPLACE'; ALTER TABLE tab ADD COLUMN c varchar(50); Query OK, 0 rows affected (0.006 sec)
ALTER TABLE ... DROP COLUMN
InnoDB supports dropping columns from a table with ALGORITHM=INPLACE
.
The table is rebuilt, which means that all of the data is reorganized substantially, and the indexes are rebuilt. As a result, the operation is quite expensive.
Concurrent DML is permitted.
ALTER TABLE ... MODIFY COLUMN
Reordering Columns
InnoDB supports reordering columns within a table with ALGORITHM=INPLACE
.
The table is rebuilt, which means that all of the data is reorganized substantially, and the indexes are rebuilt. As a result, the operation is quite expensive.
Concurrent DML is permitted.
Changing the Data Type of a Column
InnoDB does not support modifying a column's data type with ALGORITHM=INPLACE
in most cases. If you try to do so, then you will see an error. For example:
CREATE TABLE t1 (a INT, e ENUM ('red','green')); ALTER TABLE t1 MODIFY a INT UNSIGNED, ALGORITHM=INPLACE; -> ERROR 1846 (0A000): ALGORITHM=INPLACE is not supported. Reason: Cannot change column type INPLACE. Try ALGORITHM=COPY.
In MariaDB 10.2.2 and later, the only exception is that increasing the size of a VARCHAR
column, which is performed as an instantaneous metadata update.
Changing a Column to NULL
InnoDB supports modifying a column to allow NULL
values with ALGORITHM=INPLACE
.
The table is rebuilt, which means that all of the data is reorganized substantially, and the indexes are rebuilt. As a result, the operation is quite expensive.
Concurrent DML is permitted.
Changing a Column to NOT NULL
InnoDB supports modifying a column to not allow NULL
values with ALGORITHM=INPLACE
. It is required for strict mode to be enabled in SQL_MODE
. The operation will fail if the column contains any NULL
values. Changes that would interfere with referential integrity are also not permitted.
The table is rebuilt, which means that all of the data is reorganized substantially, and the indexes are rebuilt. As a result, the operation is quite expensive.
Concurrent DML is permitted.
Setting a Column's Default Value
InnoDB supports modifying a column's default value with ALGORITHM=INPLACE
.
This operation only changes the table's metadata, so the table does not have to be rebuilt.
Concurrent DML is permitted.
Removing a Column's Default Value
InnoDB supports removing a column's default value with ALGORITHM=INPLACE
.
This operation only changes the table's metadata, so the table does not have to be rebuilt.
Concurrent DML is permitted.
Adding a New ENUM
Option
InnoDB supports adding a new ENUM
option to a column with ALGORITHM=INPLACE
. In order to add a new ENUM
option with ALGORITHM=INPLACE
, the following requirements must be met:
- It must be added to the end of the list.
- The storage requirements must not change.
This operation only changes the table's metadata, so the table does not have to be rebuilt..
Concurrent DML is permitted.
For example:
CREATE TABLE t1 (a INT, e ENUM ('red','green')); ALTER TABLE t1 MODIFY e ENUM('red','green','blue'), ALGORITHM=INPLACE; -> Query OK, 0 rows affected (0.11 sec) -> Records: 0 Duplicates: 0 Warnings: 0
Adding a New SET
Option
InnoDB supports adding a new SET
option to a column with ALGORITHM=INPLACE
. In order to add a new SET
option with ALGORITHM=INPLACE
, the following requirements must be met:
- It must be added to the end of the list.
- The storage requirements must not change.
This operation only changes the table's metadata, so the table does not have to be rebuilt..
Concurrent DML is permitted.
ALTER TABLE ... CHANGE COLUMN
InnoDB supports renaming a column with ALGORITHM=INPLACE
.
This operation only changes the table's metadata, so the table does not have to be rebuilt.
Concurrent DML is permitted, unless the column's data type or attributes changed in addition to the name.
Generated Columns
Generated columns do not currently support online DDL for all of the same operations that are supported for "real" columns.
See Generated (Virtual and Persistent/Stored) Columns: Statement Support for more information on the limitations.
Index Operations
ALTER TABLE ... ADD PRIMARY KEY
InnoDB supports adding a primary key to a table with ALGORITHM=INPLACE
. If the new primary key column has to be modified to NOT NULL
, then it is required for strict mode to be enabled in SQL_MODE
.
The table is rebuilt, which means that all of the data is reorganized substantially, and the indexes are rebuilt. As a result, the operation is quite expensive.
Concurrent DML is permitted.
ALTER TABLE ... DROP PRIMARY KEY
InnoDB does not support dropping a primary key with ALGORITHM=INPLACE
in most cases. If you try to do so, then you will see an error. Only ALGORITHM=COPY
is supported. Concurrent DML is *not* permitted.
However, there is an exception. If you are dropping a primary key, and adding a new one at the same time, then that operation can be performed with ALGORITHM=INPLACE
. Concurrent DML is permitted.
ALTER TABLE ... ADD INDEX
and CREATE INDEX
InnoDB supports adding an index to a table with ALGORITHM=INPLACE
in most cases. The table is not rebuilt.
Concurrent DML is permitted.
However, there is an exception. FULLTEXT indexes without a user-defined FTS_DOC_ID
column require the table to be rebuilt once. When the table is rebuilt, the system adds a hidden FTS_DOC_ID
column. From that point, FULLTEXT indexes added to the same table do not require the table to be rebuilt.
ALTER TABLE ... DROP INDEX
and DROP INDEX
InnoDB supports dropping indexes from a table with ALGORITHM=INPLACE
.
This operation only changes the table's metadata, so the table does not have to be rebuilt.
Concurrent DML is permitted.
ALTER TABLE ... ADD FOREIGN KEY
InnoDB supports adding foreign key constraints to a table with ALGORITHM=INPLACE
. In order to add a new foreign key constraint to a table with ALGORITHM=INPLACE
, the foreign_key_checks
system variable needs to be set to 0
. If it is set to 1
, then ALGORITHM=COPY
is required.
This operation only changes the table's metadata, so the table does not have to be rebuilt.
Concurrent DML is permitted.
ALTER TABLE ... DROP FOREIGN KEY
InnoDB supports dropping foreign key constraints from a table with ALGORITHM=INPLACE
.
This operation only changes the table's metadata, so the table does not have to be rebuilt.
Concurrent DML is permitted.
Table Operations
ALTER TABLE ... AUTO_INCREMENT=...
InnoDB supports changing a table's AUTO_INCREMENT
value with ALGORITHM=INPLACE
. This operation should finish instantly. The table is not rebuilt.
ALTER TABLE ... ROW_FORMAT=...
InnoDB supports changing a table's row format with ALGORITHM=INPLACE
.
The table is rebuilt, which means that all of the data is reorganized substantially, and the indexes are rebuilt. As a result, the operation is quite expensive.
Concurrent DML is permitted.
ALTER TABLE ... KEY_BLOCK_SIZE=...
InnoDB supports changing a table's KEY_BLOCK_SIZE
with ALGORITHM=INPLACE
.
The table is rebuilt, which means that all of the data is reorganized substantially, and the indexes are rebuilt. As a result, the operation is quite expensive.
Concurrent DML is permitted.
ALTER TABLE ... FORCE
InnoDB supports forcing a table rebuild with ALGORITHM=INPLACE
.
The table is rebuilt, which means that all of the data is reorganized substantially, and the indexes are rebuilt. As a result, the operation is quite expensive.
Concurrent DML is permitted.
ALTER TABLE ... ENGINE=InnoDB
InnoDB supports forcing a table rebuild with ALGORITHM=INPLACE
.
The table is rebuilt, which means that all of the data is reorganized substantially, and the indexes are rebuilt. As a result, the operation is quite expensive.
Concurrent DML is permitted.
OPTIMIZE TABLE ...
InnoDB supports optimizing a table with ALGORITHM=INPLACE
.
If the innodb_defragment
system variable is set to OFF
and the innodb_optimize_fulltext_only
system variable is set to OFF
, then OPTIMIZE TABLE
will be equivalent to ALTER TABLE … FORCE
.
The table is rebuilt, which means that all of the data is reorganized substantially, and the indexes are rebuilt. As a result, the operation is quite expensive.
If either of the previously mentioned system variables is set to ON
, then OPTIMIZE TABLE
will optimize some data without rebuilding the table. However, the file size will not be reduced.
Concurrent DML is permitted.
ALTER TABLE ... RENAME TO
and RENAME TABLE ...
InnoDB supports renaming a table with ALGORITHM=INPLACE
.
This operation only changes the table's metadata, so the table does not have to be rebuilt.
Concurrent DML is permitted.