Adding multiple indexes at same time in MySQL

三世轮回 提交于 2020-01-11 09:12:26

问题


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?


回答1:


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 the each index separately.

The Ref manual lists some caveats, such as dealing with PRIMARY KEY.




回答2:


It is useful to create it in the time of the table creation, which can be done like this:

CREATE TABLE name_of_a_new_table (col1 variable_type_1, col2 variable_type_2,
       INDEX idx_col1 (col1), INDEX idx_col2 (col2));



回答3:


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



来源:https://stackoverflow.com/questions/30627258/adding-multiple-indexes-at-same-time-in-mysql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!