pt-online-schema-change implements online DDL

Reference article: http://seanlook.com/2016/05/27/mysql-pt-online-schema-change/

The above article is very detailed. In this article, I want to add some of my own understanding.

1. How to ensure the consistency of the data when copying the three triggers created in the original table?

  • insert : When an insert occurs in the original table, the data will also be inserted into the new table at the same time;
  • update : It is used in the way of replace...into. If the data does not exist in the new table, the data will be inserted. If the data already exists in the new table, the old data will be deleted and the updated data will be inserted. But replace...into can only be used when there is a primary key or unique key in the table, otherwise replace...into will be the same as insert, and the update function cannot be implemented.
  • delete : If there is no related data in the new table, the data in the original table will be deleted, and the trigger will ignore the delete statement defined inside the trigger. If the data has already been copied in the new table, it will start to delete the data. Specific explanation:
    • create trigger db_tb_del after delete on db.tb for each row delete ignore from db._tb_new where db._tb_new.id <=> OLD.id; delete trigger operation is to use after delete, that is, delete first, and then trigger delete trigger , execute the SQL defined in the trigger (the meaning of the SQL is: in the where condition, if the id of the new table is equal to the id of the original table in the delete operation in the previous step, execute the delete statement, if it does not meet the conditions, ignore the deletion of this sentence);

2. Several options for online DDL

In MySQL 5.6, the efficiency of online DDL is very low, and sometimes the table is severely locked. The pt-osc in percona can do online DDL with high efficiency, but pay attention to the usage restrictions of pt-osc.

  • pt-osc cannot be used when any of the following conditions are met in a table:
    • There is no primary or unique key in the table;
    • A trigger already exists in the table;
    • There are foreign keys in the table
  • Determine whether the native online DDL of MySQL 5.6 can be used:
    • Create a table with the same structure as the original table, import a small amount of data, execute the DDL of the desired operation, and check whether "rows affected" is 0. If it is 0, it can be used. If it is not 0, it means that the operation will rebuild the entire table. ,unavailable.
  • When the pt-osc tool cannot be used, and the native online DDL also seriously affects the business, there is another way:
    • Do DDL on the slave, then upgrade the slave to master, then DDL on the old master.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325340898&siteId=291194637