简体   繁体   中英

How to optimize when adding key to a big table, with innodb as engine?

There is a table of several million rows. I commit the following query:

ALTER TABLE my_data ADD KEY `sex_birth` (`sex`, `birth`);

This takes 38 min 44.53 sec!

How can I optimize this to run faster?

You can't really make this query faster. But you can surely do something so that your application does not go down for too long.

Create a copy of this table with some other name, say my_data_alter . Run alter on this table.

You will need the log of all the updates/insert/delete that have run, since you took the copy to the time when alter is completed. Then you will have to run these queries to my_data_alter .

It would be kind of a never ending loop if the number of transactions happening with the table are too large.

At the end you just have to rename the two tables, in a single query.

I understand that this would be very somewhat tricky. Do this kind of thing in the off-peak time.

I suggest you write some script that does this job for you..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM