Mysqlaltertable memo in most cases, a temporary backup (temporarily named B) of the target table (temporarily named a) will be copied during alter table operations ), all the alter operations are performed on the backup file (B), then the target table (a) is deleted, and the backup table (B) is renamed as the target table (). When alter table is executed, other sessions can read the target table (a), but the update and write operations will be blocked until B is named, can be redirected to a without losing any DML operations. This temporary Table B is created in the same database as the target Table.
The following is the test process:
Alter table cdb_posts drop index fid;
Drop index fid on cdb_posts;
Alter table cdb_posts add index idx_fid (fid );
Create index idx_fid on cdb_posts (fid );
These operations are the same:
Copy to temporary table
Temporary table file created:
How to quickly create an index with a large amount of data, I have posted on the Internet that, first create an empty table B, add the index field, and then insert .... select ....., then rename the old table, rename B to the target table, and then delete the old table. at first, I tested this in a million table, and the efficiency was worse than simply alter table add index ..., alter table add index Time: 9min19. 36sec: Cost: 11min17. 69sec
Reference: MySQL-alter table usage