January 6, 2024 By Matthew Rathbone *

Introduction

For database optimization, creating indexes is a fundamental technique to enhance query performance. This guide will walk you through the why and how of index creation, ensuring your MySQL queries run as efficiently as possible. By understanding how to effectively use indexes, you can significantly reduce query execution time, especially in databases with large volumes of data.

A Database Manager That Is Modern, Fast, & Easy To Use

Tried a few tools. Beekeeper was the only one that I found that felt right. Most had a very 1990's feel to them - Allan

I built Beekeeper Studio because, like Allan, I wanted something more intuitive and modern than all the existing clunky apps I could find. My customers agree - they love using Beekeeper and they tell me every day! Give it a try, I bet you'll like it too.

Beekeeper's Linux version is 100% full-featured, no cut corners, no feature compromises.

Understanding Indexes in MySQL

An index in MySQL is a data structure that improves the speed of data retrieval operations on a database table at the cost of additional writes and storage space to maintain the index data structure. Indexes are used to quickly locate data without having to search every row in a database table every time a database table is accessed.

How to Create an Index in MySQL

Basic Index Creation

Creating an index in MySQL can be done using the CREATE INDEX statement:

Query:

CREATE INDEX idx_lastname ON employees(lastname);

This command creates an index named idx_lastname on the lastname column of the employees table, making searches based on the lastname column faster.

Sample Output:

Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

Creating a Unique Index

A unique index ensures that all values in the indexed column are distinct.

Query:

CREATE UNIQUE INDEX idx_employee_id ON employees(employee_id);

This command creates a unique index on the employee_id column, ensuring no two rows have the same employee_id.

Sample Output:

Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

Using Indexes with Composite Keys

You can create an index on multiple columns to optimize queries that filter or sort based on those columns.

Query:

CREATE INDEX idx_name_department ON employees(lastname, department_id);

This command creates an index that includes both the lastname and department_id columns, which is useful for queries involving both of these fields.

Sample Output:

Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

Practical Use Cases

Improving Query Performance

Indexes significantly improve query performance, especially for SELECT statements that involve large datasets.

Before Indexing:

Assuming a SELECT query on the employees table without an index on the lastname column.

After Indexing:

The same SELECT query following the creation of idx_lastname.

Query:

SELECT * FROM employees WHERE lastname = 'Doe';

Sample Output Without Index:

/* Takes significantly longer, e.g., 1.5 seconds */

Sample Output With Index:

/* Takes significantly less time, e.g., 0.02 seconds */

Ensuring Data Integrity

Unique indexes prevent duplicate values in a column, ensuring data integrity.

Scenario:

Attempting to insert a duplicate employee_id after creating a unique index.

Sample Output:

ERROR 1062 (23000): Duplicate entry '12345' for key 'idx_employee_id'

Conclusion

Creating indexes in MySQL is an important technique for optimizing your database queries. By selecting appropriate columns for indexing—especially those frequently used in search conditions or join clauses—you can achieve significant performance gains.

Remember, while indexes speed up data retrieval, they also slow down data insertion and update operations due to the additional overhead of maintaining the index structure. Use them judiciously to strike the right balance between read and write performance.

Experiment based on these examples in your MySQL databases to see the benefits of indexing. With careful planning and implementation, indexes will become an invaluable part of your database optimization toolkit.

Happy optimizing!