Starting from Percona Toolkit 3.6.0, pt-online-schema-change supports the option –where, so you can not only alter your table definition live but copy only rows that satisfy certain criteria.

It may happen that you may not need to copy all the data when changing table definition. For example, if the table is too big and you need only recent data.

To achieve this task, run pt-online-schema-change with options –where=’YOUR WHERE CONDITION’ –no-drop-new-table –no-swap-tables.

The code snippet above will create a new table, comments_2023, and will leave the original table untouched.

Options –no-drop-new-table –no-swap-tables are needed to protect rest of the data in the original table. pt-online-schema-change will refuse to execute if you start it with option –where but omit these safety options.

Using option –where together with –drop-new-table and –swap-tables may lead to data loss, therefore this operation is only allowed if option –force also specified. Aborting.

If you are sure you do not need to keep data, not matching WHERE clause, you can override this safety check with option –force:

As a result of the two operations shown, the database would have two tables: archived, with comments from the year 2023, and another, actual, with comments from the year 2024:

Note, that option –where does not have any effect on triggers that pt-online-schema-change creates to copy newly inserted or updated rows, and we do not recommend using it with conditions that may be affected by the ongoing queries.

Summary

Use option –where to copy only part of rows while modifying the table.

Use options –no-drop-new-table –no-swap-tables to prevent data loss.

Use option –new-table-name if you do not want to perform drop and swap tables operation manually and want to keep two tables instead.

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments