During tests on MySQL, I wanted to add multiple indexes to a table with more than 50 million rows. Does MySQL support adding 2 indexes at the same time for different columns? If yes, Do I need to open 2 sessions or it can be done from one command?
3 Answers
Yes. But...
In older versions, use
ALTER TABLE tbl
ADD INDEX(...),
ADD INDEX(...);
so that it will do all the work in one pass.
In newer versions, ALGORITHM=INPLACE makes it so that the work can be done in the "background" for InnoDB tables, thereby having less impact on other processing. However, to get the INPLACE processing, you may need to add each index separately. [Check the manual for the specific syntax; there have been many changes in the 6+ years since I wrote this Answer. Also, MySQL and MariaDB are not in lock-step.]
The Ref manual [ref manual] Ref manual lists some caveats, such as dealing with PRIMARY KEY.
5 Comments
ALTER: ALTER TABLE tbl MODIFY ... ADD PRIMARY KEY (...), ADD INDEX ..., etc;content_id already has data in it, are you adding AUTO_INCREMENT and PRIMARY KEY? Is it already BIGINT? (Please show us SHOW CREATE TABLE)table1 ( content_id bigint(20) unsigned NOT NULL , pid int(11) unsigned NOT NULL, status int(11) unsigned NOT NULL DEFAULT '0', datecreated datetime DEFAULT NULL, datemodified datetime DEFAULT NULL, ) ENGINE=InnoDB DEFAULT CHARSET=utf8;MAX(content_id)? What is your tentative ALTER, based on the advice so far?Have you checked the MySQL manual? InnoDB supports fast index creation, and maybe it is what you are looking for:
https://dev.mysql.com/doc/refman/5.5/en/innodb-create-index.html