Indexing in MySQL

In SQL, indexing is a technique used to optimize the retrieval of rows from a table. Indexes provide a quick and efficient way to locate rows based on the values in one or more columns. Indexing can significantly improve the performance of SELECT queries but may slightly impact the performance of INSERT, UPDATE, and DELETE operations.

Here’s a brief overview of indexing in SQL:

Types of Indexes:

  • Single-Column Index: Created on a single column.
  • Composite Index (or Multi-Column Index): Created on multiple columns. Useful when queries involve conditions on multiple columns.

Creating an Index:

  • The syntax to create an index is as follows:
CREATE INDEX index_name
ON table_name (column1, column2, ...);

For example:

CREATE INDEX idx_lastname
ON employees (last_name);

Unique Index:

  • Ensures that the values in the indexed columns are unique.
ON table_name (column1, column2, ...);

Removing an Index:

  • To remove an index, you can use the DROP INDEX statement:
DROP INDEX index_name ON table_name;

How Indexing Works:

  • When a query is executed, the database engine can use the index to quickly locate the rows that satisfy the conditions specified in the WHERE clause.
  • This is similar to the way a book index helps you find information quickly.


  • While indexes can improve read performance, they come with some trade-offs. Indexes consume disk space and can slightly slow down write operations (INSERT, UPDATE, DELETE).
  • Over-indexing can be counterproductive, as each index requires maintenance and takes up storage space.
  • Regularly monitor and analyze the performance of your queries to determine whether indexes are beneficial.

Here’s an example of creating a composite index:

CREATE INDEX idx_name_age
ON employees (last_name, age);

This creates an index named idx_name_age on the last_name and age columns of the employees table. It can be useful for queries that involve conditions on both the last name and age.