MySQL MariaDB

How MySQL Adds an Index to an Existing Table

An index, otherwise known as indices, refers to a data structure that enhances data fetching speeds in a MySQL table. If a table does not have an index, executing a query prompts MySQL to scan all the rows until it finds the requested information. If the table has many records, this process will take a long time to complete, significantly affecting the database performance.

This tutorial will show you how to create an index on a MySQL table for both existing and new tables.

Before we get to execute queries and create indices, let us look at a few MySQL index concepts.

Types of MySQL Indices

MySQL supports two types of indices:

  1. A primary or Clustered index
  2. Secondary Index

MySQL automatically creates an index, called PRIMARY, any time we create a table with a PRIMARY KEY. Depending on the Database Engine, if the Primary Key or Unique key is not available in a table, MySQL may create a hidden key on the column with id values.

The primary index created by MySQL is stored together with the data in the same table. Other indices that exist within a table apart from the PRIMARY index are known as secondary indices.

How to Add an Index to an Existing Table

Although MySQL recommends adding an index during table creation, there are some instances where you may need to add an index to an existing table, such as in a regularly accessed column.

To add an index to an existing table, we can use the ALTER query.

Let us take a sample database with the tables as shown in the query below:

CREATE DATABASE IF NOT EXISTS mysql_indices;
USE mysql_indices;
CREATE TABLE treks(
    id INT AUTO_INCREMENT PRIMARY KEY,
    cpt_name
    VARCHAR(255) NOT NULL,
    ship
    VARCHAR(255) NOT NULL
);
INSERT INTO treks(cpt_name, ship)
VALUES ('Carol Freeman', 'USS Cerritos'),
    ('Christopher Pike', 'USS Discovery'),
    ('Jean-Luc Picard', 'USS Enterprise'),
    ('James T. Kirk', 'USS Enterprise'),
    ('Jonathan Archer', 'USS Enterprise');

In the example above, MySQL will create an index key using the id column because it is specified as the PRIMARY KEY.

You can verify this using the query:

SHOW INDEX FROM treks;

To create a custom index, use the ALTER query as:

ALTER TABLE treks ADD INDEX (cpt_name);

In the above example, we use the cpt_name as the second primary key. To show the indices, use the query:

If not specified, MySQL will default any index as a B-TREE. Other supported index types include HASH and FULLTEXT.

The index type will depend on the Storage Engine for the specified table.

To create an index for a column or list of columns, we use the CREATE INDEX query. For example, to create an index for the “ship: column, we can do:

CREATE INDEX ship_index ON treks(ship);

If we run the SHOW INDEX query on the table, we should see an index called “ship_index” on the “ship” column.

How to Add an Index to a New Table

The recommended way is to create an index or indices when creating a table. To do this, specify the columns to use as indices inside the INDEX () query.

Let us start by dropping the treks table in the previous example:

DROP TABLE treks;

Next, let us recreate the table and specify the INDEX during creation. An example query is:

CREATE TABLE treks(
    id INT AUTO_INCREMENT PRIMARY KEY,
    cpt_name
    VARCHAR(255) NOT NULL,
    ship
    VARCHAR(255) NOT NULL,
    INDEX (ship)
);
INSERT INTO treks(cpt_name, ship)
VALUES ('Carol Freeman', 'USS Cerritos'),
    ('Christopher Pike', 'USS Discovery'),
    ('Jean-Luc Picard', 'USS Enterprise'),
    ('James T. Kirk', 'USS Enterprise'),
    ('Jonathan Archer', 'USS Enterprise');

In the example query above, we add the ship column as an index during table creation. Since the id is the PRIMARY KEY, MySQL automatically adds it as an index.

SHOW INDEX FROM treks;

Conclusion

In this tutorial, you learned how to add an index to an existing table, add an index for a column, and create an index during table creation.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list