MySQL Online DDL and pt-online-schema-change
This is Oniumi (@fat47) from the Service Reliability Group (SRG) of the Technology Headquarters.
#SRG(Service Reliability Group) is a group that mainly provides cross-sectional support for the infrastructure of our media services, improving existing services, launching new ones, and contributing to OSS.
This article is located in SRGDBWGWe will publish materials regarding the database provided by the DB Working Group for the entire company.
I hope this helps in some way.
overviewconclusionmethodUse official MySQL features (MySQL 5.6 and later)Use pt-online-schema-changeConclusion
overview
When adding specifications for a service or developing a new one, it may be necessary to perform DDL (Data Definition Language) operations such as adding columns or indexes to existing tables.
If you perform a normal DDL operation while the service is running, a table lock may occur, which could lead to a serious service failure.
By using the following method, you can prevent table locking and perform DDL operations online.
conclusion
AWS AuroraIn this case
- Use the official MySQL online DDL feature
- If this is not possible for the reasons described below, the next best option is pt-online-schema-change
RDS(MySQL) ,orPure MySQL (5.5~5.7)In this case, use pt-online-schema-change.
method
Use official MySQL features (MySQL 5.6 and later)
The online DDL feature implemented in MySQL 5.6 allows some operations to be performed without locking.
To summarize, the following operations can be performed online:
- OPTIMIZE TABLE
- ADD INDEX
- CREATE INDEX
- DROP INDEX
- Adding a Column
- Deleting columns (beware of high costs)
- Adding and deleting foreign key constraints
In addition, MySQL 8.0.12 added a feature called INSTANT DDL, which allows you to perform DDL operations quickly by updating only the metadata.
However, there are the following restrictions when executing with ALGORITHM=INSTANT.
- Adding a column cannot be combined in the same statement with other ALTER TABLE actions that do not support ALGORITHM=INSTANT.
- A column can only be added as the last column in a table. Adding a column in any other position between other columns is not supported.
- You cannot add a column to a table that uses ROW_FORMAT=COMPRESSED.
- You cannot add a column to a table that has a FULLTEXT index.
- Columns cannot be added to temporary tables. Only ALGORITHM=COPY is supported for temporary tables.
- You cannot add a column to a table that resides in the Data Dictionary tablespace.
- The row size limit is not evaluated when adding a column. However, the row size limit is checked during DML operations that insert and update rows in a table.
In 8.0.29 and later, when executing an ALTER statement, if the operation supports INSTANT, INSTANT will be used by default even if ALGORITHM is not specified.
In addition, the following features have been added:
- It is possible to add columns not only to the end but also to other positions.
- Drop column compatible
For more information, please refer to the Online DDL section of the official documentation.
Please note the following points when using this online DDL function:
If ALGORITHM=INSTANT cannot be executed and the table has a large data size, replication delays will occur.
That is the point.
If a DDL execution on the master DB takes 20 minutes, it will be executed on the slave DB after the execution is completed on the master,
That translates directly into a 20 minute replication delay.
If the execution environment is Amazon RDS Aurora, this is not the case because the architecture is different.
To prevent this, execute the DDL on the slave first, then execute the DDL on the master DB with the binlog temporarily disabled.
This is commonly called a Rolling Schema Upgrade, but this method also has its drawbacks.
Since the column type change and column addition are already performed on the slave,
You need to check in advance whether the application will still function properly in this state.
If these points are not acceptable, you can use the Percona tool described below.
Use pt-online-schema-change
pt-online-schema-change (hereinafter referred to as pt-osc) is a tool included in the Percona ToolKit that executes ALTER statements without locking tables, which normally require a table lock.
Please refer to the following page for details and architecture of this tool.
Please refer to the official documentation for how to install the percona toolkit.
By using pt-osc, you can execute online DDL on MySQL 5.5, which does not have the online DDL function.
DDL that takes a long time to execute can also be executed with relatively little replication delay.
Execution sample when adding a column
This tool also has limitations and may not be usable in all situations.
Please test it in a verification environment beforehand.
Conclusion
SRG is looking for people to work with us.
If you're interested, please contact us here.