MySQL very slow for alter table query

前端 未结 5 1869
梦谈多话
梦谈多话 2020-12-07 13:02

Why is it taking more than an hour to simply update this table to add a column? This table has 15M rows. It has 2 indexes and a single key primary key. The ALTER TABLE quer

5条回答
  •  无人及你
    2020-12-07 14:01

    If you don't care about downtime, my suggestion is using three separated ALTER TABLE statements. The first statement removes all existing secondary indexes. The second statement applies all column related changes. The last statement adds dropped secondary indexes back and applies other index changes.

    Another two tips:

    1. Before apply index changes, execute the two following statements and change the values back to 1 after finishing the index change.

      SET unique_checks=0;
      SET foreign_key_checks=0;
      
    2. When create multiple secondary indexes, put them in one ALTER TABLE statement rather than multiple separated ALTER TABLE statements.

    The following picture shows the performance difference. Approach 1 is your approach and approach 2 is my way. Approach 2 takes about 3.47% time comparing with approach 1 for a 50m table. The solution only works for MySQL (>=5.5) InnoDB engine.

    enter image description here

提交回复
热议问题