1. How to execute in parallel.
There is a table with a very large data volume in the database. When alter is performed on this table, the table is read-only at this time. Because of the large data volume, alter takes a long time, if there is an insert or update operation at this time, is there any way to perform the insert or update operation during this period?
MySQL can be inserted and updated when it is alter, but the operation will be delayed. Here is my reference to the MySQL Official Website:
* "During the alter table operation, the original table will be temporarily copied, changed on the copy, deleted from the original table, and renamed. When you execute alter table, other users can read the original table, but the update and modification operations on the table will be delayed until the new table is generated. After a new table is generated, the update and modification information is automatically transferred to the new table.
Note: If you use an option other than rename when executing alter table, MySQL creates a temporary table. MySQL performs this operation even if the data does not need to be copied (for example, when you change the column name. For MyISAM tables, you can set the myisam_sort_buffer_size system variable to a high value to speed up index re-creation (this operation is the slowest part of the change process.
If you use alter table tbl_name Rename to new_tbl_name without any other options, MySQL only renames the file corresponding to table tbl_name. You do not need to create a temporary table. (You can also use the rename table statement to rename the table ." *
Therefore, we can see that MySQL copies the table structure during alter, so I suggest you save time:
1. Create table_new and remove two additional fields + Add 5 fields
2. insert into table_new select * from table: Copy all data to the new table structure.
3. Drop table; rename table_new to table; Delete the table and change the new table name to the original table name.
Of course, when the alter of a large table is used, I suggest you handle it when few users access it (for example, in the early morning)
From: http://www.dewen.org/q/3956