Skip to content

Instantly share code, notes, and snippets.

@cecyc
Last active May 15, 2017 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cecyc/c6dcb8c86178ca02da43439d5eab0233 to your computer and use it in GitHub Desktop.
Save cecyc/c6dcb8c86178ca02da43439d5eab0233 to your computer and use it in GitHub Desktop.
Adding indexes to MySQL

How to add indexes in MySQL

Basics:

alter table NAME add index <column>

The above works if you are adding an index to just one column. If you need to index different variations:

alter table NAME add index INDEXNAME(things,to,index)

If you need to alter an existing index, you will need to drop it first, then recreate it.

alter table NAME drop index INDEXNAME

If you need to look up existing index data, you can:

show create table NAME

This will show existing keys.

Full example:

alter table TABLENAME
drop index foo, 
add index foo(foo,bar), ALGORITHM=INPLACE;

If there are issues while performing this, you couldshow process list; and then kill X where X is the id of the process, to terminate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment