Indexing for high performance, simplified.

·

3 min read

Indexes are essential components of databases that help improve performance by allowing for efficient data retrieval. They work by creating a data structure that maps the values in a column to their corresponding rows in a table. In this blog post, we'll take a closer look at four different types of indexes - composite, prefix, covering, and full-text - and how they can be used to improve database performance.

Composite Indexes

A composite index is an index that includes multiple columns. They are useful when queries involve multiple columns as they allow for faster retrieval of data by combining the values of the indexed columns. To create a composite index in MySQL, we use the following syntax:

CREATE INDEX index_name ON table_name (column1, column2);

Let's say we have a table called employees that contains the columns first_name, last_name, age, and salary. We can create a composite index on the first_name and last_name columns using the following command:

CREATE INDEX name_index ON employees (first_name, last_name);

This index can then be used to improve the performance of queries that involve the first_name and last_name columns.

Prefix Indexes

A prefix index is a type of composite index that only indexes a portion of each column. They are useful when the indexed columns have long values, and only the first few characters are needed to identify the rows. To create a prefix index in MySQL, we use the following syntax:

CREATE INDEX index_name ON table_name (column1(length), column2(length));

Let's modify our employees table by adding a column called email that contains email addresses. We can create a prefix index on the first 10 characters of the email column using the following command:

CREATE INDEX email_index ON employees (email(10));

This index can then be used to improve the performance of queries that involve the first 10 characters of the email column.

Covering Indexes

A covering index is an index that contains all the columns needed to satisfy a query, so the database doesn't have to look up the actual table data. They are useful when queries involve multiple columns or require a large amount of data to be retrieved. To create a covering index in MySQL, we use the following syntax:

CREATE INDEX index_name ON table_name (column1, column2) INCLUDE (column3, column4);

Let's say we have a query that retrieves the first_name, last_name, and salary columns from the employees table. We can create a covering index that includes all three columns using the following command:

CREATE INDEX emp_index ON employees (first_name, last_name) INCLUDE (salary);

This index can then be used to improve the performance of the query as the database doesn't have to look up the actual table data.

Full-Text Indexes

A full-text index is an index that allows for efficient searching of text columns by creating a dictionary of the words in the column. They are useful when queries involve searching for specific words or phrases in large text columns. To create a full-text index in MySQL, we use the following syntax:

CREATE FULLTEXT INDEX index_name ON table_name (column);
SELECT title FROM articles;
MATCH (name) AGAINST ('REACT');
-- this used the index instead of full table scan.

Did you find this article valuable?

Support Karan CS by becoming a sponsor. Any amount is appreciated!