When it comes to performance optimization for MySQL databases, creating indexes is one of the best ways. SQL indexes help improve database performance by providing fast access to the data stored in the database.

An SQL index is not that different from a normal book index with a curated list of information and where you can find them.

In this tutorial, we will discuss when to create an SQL index because, although indexes can help optimize performance, they can also cause slow performance and other negative impacts. We shall also discuss how to create an index using a real database.

Let us get started:

When To Create An Index In MySQL?

Unfortunately, I have no direct answer for when you should create an index. However, some scenarios and factors may influence the creation of an index. It is good to note that you may have to do some trade-offs when creating an index

  1. High Accessibility: If you have a table or column accessed regularly, you may increase its performance by creating an index.
  2. Size: The size of the data stored in a table or column may also play a role in deciding when an index is necessary. A large table may benefit more from indexes than a smaller table.
  3. Index Key: The data type of the integer key is also a factor. For example, an integer is a much better index key due to its small size.
  4. CRUD Operations: If you have a table or column with a higher number of CRUD operations, indexing that table or column might not be beneficial and might negatively impact database performance.
  5. Database Size: Indexing is a data structure that will occupy space on your database, which can be a factor, especially in already large databases.

The above are some of the key concepts that may come into play when deciding when to create a database index.

If you would like to learn more about how MySQL uses indexes, consider reading the resource provided below:

https://dev.mysql.com/doc/refman/8.0/en/mysql-indexes.html

Basic MySQL Indexes Usage

By default, once you create a table with a primary key, MySQL automatically creates a special index called PRIMARY, stored with the data. This is typically known as a clustered index.

To create an index in MySQL, we can use the syntax shown below:

We start by calling the CREATE INDEX clause, followed by the name of the index we wish to create. We then specify the table where the index resides and finally the columns.

You can also create an index when creating a table, as shown in the syntax below:

NOTE: The default MySQL index type is a BTREE unless explicitly specified.

Example Use case

Using an example, allow me to illustrate how we can create an index for the specific table to enhance performance.

I will be using the film table in the employees’ database. You can find the resource from the resource page below:

https://dev.mysql.com/doc/index-other.html

First, let us see the behind-the-scene process MySQL uses to process a simple SELECT statement where the gender is equal to F.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-479.png" data-lazy- height="149" src="data:image/svg xml,” width=”1516″>

Given the size of the table and queried data, scanning more than 200,000 rows is not very efficient. In that case, we can reduce this value by creating an index.

To create an index, we can do:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/word-image-480.png" data-lazy- height="153" src="data:image/svg xml,” width=”1589″>

Once we have the index created, MySQL will scan the values as shown in the output above.

Conclusion

I hope this tutorial has given you a deeper understanding of using MySQL indexes to enhance database performance.

Thank you for reading.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/05/echo/john.png60ab3f1eaf719.jpg" height="112" src="data:image/svg xml,” width=”112″>

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