ALTER TABLE without locking the table?

后端 未结 19 2007
梦毁少年i
梦毁少年i 2020-11-30 17:41

When doing an ALTER TABLE statement in MySQL, the whole table is read-locked (allowing concurrent reads, but prohibiting concurrent writes) for the duration of the statement

19条回答
  •  抹茶落季
    2020-11-30 17:55

    Percona makes a tool called pt-online-schema-change that allows this to be done.

    It essentially makes a copy of the table and modifies the new table. To keep the new table in sync with the original it uses triggers to update. This allows the original table to be accessed while the new table is prepared in the background.

    This is similar to Dems suggested method above, but this does so in an automated fashion.

    Some of their tools have a learning curve, namely connecting to the database, but once you have that down, they are great tools to have.

    Ex:

    pt-online-schema-change --alter "ADD COLUMN c1 INT" D=db,t=numbers_are_friends
    

提交回复
热议问题