Optimize mySql for faster alter table add column
I faced a very similar situation in the past and i improve the performance of the operation in this way :
- Create a new table (using the structure of the current table) with the new column(s) included.
- execute a
INSERT INTO new_table (column1,..columnN) SELECT (column1,..columnN) FROM current_table;
- rename the current table
- rename the new table using the name of the current table.
ALTER TABLE
in MySQL is actually going to create a new table with new schema, then re-INSERT
all the data and delete the old table. You might save some time by creating the new table, loading the data and then renaming the table.
From "High Performance MySQL book" (the percona guys):
The usual trick for loading MyISAM table efficiently is to disable keys, load the data and renalbe the keys:
mysql> ALTER TABLE test.load_data DISABLE KEYS;
-- load data
mysql> ALTER TABLE test.load_data ENABLE KEYS;
Well, I would recommend using latest Percona MySQL builds plus since there is the following note in MySQL manual
In other cases, MySQL creates a temporary table, even if the data wouldn't strictly need to be copied. For MyISAM tables, you can speed up the index re-creation operation (which is the slowest part of the alteration process) by setting the myisam_sort_buffer_size system variable to a high value.
You can do ALTER TABLE DISABLE KEYS
first, then add column and then ALTER TABLE ENABLE KEYS
. I don't see anything can be done here.
BTW, can't you go MongoDB? It doesn't rebuild anything when you add column.