InnoDB Online DDL Overview
MariaDB starting with 10.0
InnoDB tables support online DDL, which permits concurrent DML and uses optimizations to avoid unnecessary table copying.
Contents
In MariaDB 10.0 and later, the ALTER TABLE
statement supports two clauses that are used to implement online DDL:
ALGORITHM
- This clause controls how the DDL operation is performed.LOCK
- This clause controls how much concurrency is allowed while the DDL operation is being performed.
ALGORITHM
In MariaDB 5.5 and before, ALTER TABLE
operations required making a temporary copy of the table, which can be slow for large tables.
In MariaDB 10.0, the ALTER TABLE
statement supports the ALGORITHM
clause. This clause is one of the clauses that is used to implement online DDL. ALTER TABLE
supports several different algorithms. An algorithm can be explicitly chosen for an ALTER TABLE
operation by setting the ALGORITHM
clause. The supported values are:
ALGORITHM=DEFAULT
- This implies the default behavior for the specific statement, such as if noALGORITHM
clause is specified.ALGORITHM=COPY
ALGORITHM=INPLACE
ALGORITHM=NOCOPY
- This was added in MariaDB 10.3.7.ALGORITHM=INSTANT
- This was added in MariaDB 10.3.7.
ALGORITHM=DEFAULT
The default behavior, which occurs if ALGORITHM=DEFAULT
is specified, or if ALGORITHM
is not specified at all, usually only makes a copy if the operation doesn't support being done in-place at all. In this case, the most efficient available algorithm will usually be used.
However, in MariaDB 10.3.6 and before, if the value of the old_alter_table
system variable is set to ON
, then the default behavior is to perform ALTER TABLE
operations by making a copy of the table using the old algorithm.
In MariaDB 10.3.7 and later, the old_alter_table
system variable is deprecated. Instead, the alter_algorithm
system variable defines the default algorithm for ALTER TABLE
operations.
ALGORITHM=COPY
ALGORITHM=COPY
was introduced in MariaDB 10.0 as the name for the original ALTER TABLE
algorithm.
When ALGORITHM=INPLACE
is set, MariaDB essentially does the following operations:
-- Create a temporary table with the new definition CREATE TEMPORARY TABLE tmp_tab ( ... ); -- Copy the data from the original table INSERT INTO tmp_tab SELECT * FROM original_tab; -- Drop the original table DROP TABLE original_tab; -- Rename the temporary table, so that it replaces the original one RENAME TABLE tmp_tab TO original_tab;
This algorithm is very inefficient, but it is generic, so it works for all storage engines.
If ALGORITHM=COPY
is specified, then the copy algorithm will be used even if it is not necessary. This can result in a lengthy table copy. If multiple ALTER TABLE
operations are required that each require the table to be rebuilt, then it is best to specify all operations in a single ALTER TABLE
statement, so that the table is only rebuilt once.
Using ALGORITHM=COPY
with InnoDB
If ALGORITHM=COPY
is used with an InnoDB table, then the following statements apply:
- The table will be rebuilt using the current values of the
innodb_file_per_table
,innodb_file_format
, andinnodb_default_row_format
system variables.
- The operation will have to create a temporary table to perform the the table copy. This temporary table will be in the same directory as the original table, and it's file name will be in the format
, where#
sql-ib${TABLE_ID}-${RAND}${RAND}
is a randomly initialized number. Therefore, thedatadir
may contain files with file names like
.#
sql-ib230291-1363966925.ibd
- The operation inserts one record at a time into each index, which is very inefficient.
- InnoDB does not use a sort buffer.
- In MariaDB 10.2.13, MariaDB 10.3.5 and later, the table copy operation creates a lot fewer InnoDB undo log writes. See MDEV-11415 for more information.
- The table copy operation creates a lot of InnoDB redo log writes.
ALGORITHM=INPLACE
ALGORITHM=INPLACE
was introduced in MariaDB 10.0.
ALGORITHM=COPY
can be incredibly slow, because the whole table has to be copied and rebuilt. ALGORITHM=INPLACE
was introduced as a way to avoid this by performing operations in-place and avoiding the table copy and rebuild, when possible.
When ALGORITHM=INPLACE
is set, the underlying storage engine uses optimizations to perform the operation while avoiding the table copy and rebuild. However, INPLACE
is a bit of a misnomer, since some operations may still require the table to be rebuilt for some storage engines. Regardless, several operations can be performed without a full copy of the table for some storage engines.
A more accurate name would have been ALGORITHM=ENGINE
, where ENGINE
refers to an "engine-specific" algorithm.
If an ALTER TABLE
operation supports ALGORITHM=INPLACE
, then it can be performed using optimizations by the underlying storage engine, but it may rebuilt.
If ALGORITHM=INPLACE
is specified for an ALTER TABLE
operation that does not support ALGORITHM=INPLACE
, then an error will be raised. For example:
SET SESSION alter_algorithm='INPLACE'; ALTER TABLE tab MODIFY COLUMN c int; ERROR 1846 (0A000): ALGORITHM=INPLACE is not supported. Reason: Cannot change column type INPLACE. Try ALGORITHM=COPY
In this case, raising an error is preferable, if the alternative is for the operation to make a copy of the table, and perform unexpectedly slowly.
Using ALGORITHM=INPLACE
with InnoDB
If ALGORITHM=INPLACE
is used with an InnoDB table, then the following statements apply:
- The operation might have to write sort files in the directory defined by the
innodb_tmpdir
system variable.
- The operation might also have to write a temporary log file to track data changes by DML queries executed during the operation. The maximum size for this log file is configured by the
innodb_online_alter_log_max_size
system variable.
- Some operations require the table to be rebuilt, even though the algorithm is inaccurately called "in-place". This includes operations such as adding or dropping columns, adding a primary key, changing a column to
NULL
, etc.
- If the operation requires the table to be rebuilt, then the operation might have to create temporary tables.
- It may have to create a temporary intermediate table for the actual table rebuild operation.
- In MariaDB 10.2.18 and before, this temporary table will be in the same directory as the original table, and it's file name will be in the format
, where#
sql-ib${TABLESPACE_ID}-${RAND}${TABLESPACE_ID}
is the table's tablespace ID within InnoDB and${RAND}
is a randomly initialized number. Therefore, thedatadir
may contain files with file names like
.#
sql-ib230291-1363966925.ibd - In MariaDB 10.2.19 and later, this temporary table will be in the same directory as the original table, and it's file name will be in the format
, where#
sql${PID}_${THREAD_ID}_${TMP_TABLE_COUNT}${PID}
is the process ID ofmysqld
,${THREAD_ID}
is the connection ID, and${TMP_TABLE_COUNT}
is the number of temporary tables that the connection has open. Therefore, thedatadir
may contain files with file names like
.#
sql1234_12_1.ibd
- In MariaDB 10.2.18 and before, this temporary table will be in the same directory as the original table, and it's file name will be in the format
- When it replaces the original table with the rebuilt table, it may also have to rename the original table using a temporary table name.
- If the server is running MariaDB 10.1 or before or if it is running MariaDB 10.2 and the
innodb_safe_truncate
system variable is set toON
, then the renamed table will have a temporary table name in the format
, where#
sql-ib${TABLESPACE_ID}${TABLESPACE_ID}
is the table's tablespace ID within InnoDB. Therefore, thedatadir
may contain files with file names like
.#
sql-ib230291.ibd - If the server is MariaDB 10.3 or later or if it is running MariaDB 10.2 and the
innodb_safe_truncate
system variable is set toOFF
, then the format will actually be
, where#
sql-ib${TABLESPACE_ID}-${RAND}${TABLESPACE_ID}
is the table's tablespace ID within InnoDB and${RAND}
is a randomly initialized number. Therefore, thedatadir
may contain files with file names like
.#
sql-ib230291-1363966925.ibd
- If the server is running MariaDB 10.1 or before or if it is running MariaDB 10.2 and the
- It may have to create a temporary intermediate table for the actual table rebuild operation.
- The storage needed for the above items can add up to the size of the original table, or more in some cases.
- Some operations are instantaneous, if they only require the table's metadata to be changed. This includes operations such as renaming a column, changing a column's
DEFAULT
value, etc.
Operations Supported by InnoDB for ALGORITHM=INPLACE
With respect to the allowed operations, ALGORITHM=INPLACE
supports a subset of the operations supported by ALGORITHM=COPY
, and it supports a superset of the operations supported by ALGORITHM=NOCOPY
.
See InnoDB Online DDL Operations with ALGORITHM=INPLACE for more information.
ALGORITHM=NOCOPY
ALGORITHM=NOCOPY
was introduced in MariaDB 10.3.7.
ALGORITHM=INPLACE
can sometimes be surprisingly slow in instances where it has to rebuild the clustered index, because when the clustered index has to be rebuilt, the whole table has to be rebuilt. ALGORITHM=NOCOPY
was introduced as a way to avoid this.
If an ALTER TABLE
operation supports ALGORITHM=NOCOPY
, then it can be performed without rebuilding the clustered index.
If ALGORITHM=NOCOPY
is specified for an ALTER TABLE
operation that does not support ALGORITHM=NOCOPY
, then an error will be raised. For example:
SET SESSION alter_algorithm='NOCOPY'; ALTER TABLE tab MODIFY COLUMN c int; ERROR 1846 (0A000): ALGORITHM=NOCOPY is not supported. Reason: Cannot change column type INPLACE. Try ALGORITHM=COPY
In this case, raising an error is preferable, if the alternative is for the operation to rebuild the clustered index, and perform unexpectedly slowly.
Operations Supported by InnoDB for ALGORITHM=NOCOPY
With respect to the allowed operations, ALGORITHM=NOCOPY
supports a subset of the operations supported by ALGORITHM=INPLACE
, and it supports a superset of the operations supported by ALGORITHM=INSTANT
.
See InnoDB Online DDL Operations with ALGORITHM=NOCOPY for more information.
ALGORITHM=INSTANT
ALGORITHM=INSTANT
was introduced in MariaDB 10.3.7.
ALGORITHM=INPLACE
can sometimes be surprisingly slow in instances where it has to modify data files. ALGORITHM=INSTANT
was introduced as a way to avoid this.
If an ALTER TABLE
operation supports ALGORITHM=INSTANT
, then it can be performed without modifying any data files.
If ALGORITHM=INSTANT
is specified for an ALTER TABLE
operation that does not support ALGORITHM=INSTANT
, then an error will be raised. For example:
SET SESSION alter_algorithm='INSTANT'; ALTER TABLE tab MODIFY COLUMN c int; ERROR 1846 (0A000): ALGORITHM=INSTANT is not supported. Reason: Cannot change column type INPLACE. Try ALGORITHM=COPY
In this case, raising an error is preferable, if the alternative is for the operation to modify data files, and perform unexpectedly slowly.
Operations Supported by InnoDB for ALGORITHM=INSTANT
With respect to the allowed operations, ALGORITHM=INSTANT
supports a subset of the operations supported by ALGORITHM=NOCOPY
.
See InnoDB Online DDL Operations with ALGORITHM=INSTANT for more information.
LOCK
In MariaDB 10.0 and later, the ALTER TABLE
statement supports the LOCK
clause. This clause is one of the clauses that is used to implement online DDL. ALTER TABLE
supports several different locking strategies. A locking strategy can be explicitly chosen for an ALTER TABLE
operation by setting the LOCK
clause. The supported values are:
DEFAULT
: Acquire the least restrictive lock on the table that is supported for the specific operation. Permit the maximum amount of concurrency that is supported for the specific operation.NONE
: Acquire no lock on the table. Permit all concurrent DML. If this locking strategy is not permitted for an operation, then an error is raised.SHARED
: Acquire a read lock on the table. Permit read-only concurrent DML. If this locking strategy is not permitted for an operation, then an error is raised.EXCLUSIVE
: Acquire a write lock on the table. Do not permit concurrent DML.
If the LOCK
clause is not explicitly set, then the operation uses LOCK=DEFAULT
.
ALTER ONLINE TABLE
is equivalent to LOCK=NONE
. Therefore, the ALTER ONLINE TABLE
statement can be used to ensure that your ALTER TABLE
operation allows all concurrent DML.
To see which locking strategies InnoDB supports for each operation, see the pages that describe which operations are supported for each algorithm: