Use pt-online-schema-change to add new column and populate it

1k views Asked by At

I need to add a new column to existing MySQL table and populate with value calculated from another column. Is there a way to do this using pt-online-schema-change?

Thanks

1

There are 1 answers

0
Bill Karwin On BEST ANSWER

pt-online-schema change runs ALTER TABLE statements, not UPDATE statements. If you need to populate a column you have two options:

  • Add the column with pt-online-schema change. Then after that is complete, use UPDATE to populate the column with data. It is recommended to do the UPDATE in batches. For example, I'd run it on 1000 rows at a time, and repeat that until the UPDATE reports that you've changes 0 rows.

    UPDATE mytable SET mynewcolumn = <expr> 
    WHERE mynewcolumn IS NULL LIMIT 1000;
    
  • If you use MySQL 5.7 or later, define the column as a GENERATED column.