Indexes in MySQL work like the indexes you’ll find in a library.
Unless you are using indexes that will group books by subject and alphabetically.
Side note: Usually, indexes in MySQL are used to make the program run faster. So, provided that you are interested in learning about another tool that does the same job, head right into our tutorial on the views in SQL. Also, we are going to be using the ‘employees’ database. You can download it here.
The index of a table functions like the index of a book.
Basically, data is taken from a column of the table and is stored in a certain order in a distinct place, called an index.
If your dataset contained about 100 rows, you would find the record you are looking for in a millisecond. But working with such datasets is a mirage. Your datasets will typically contain hundreds of thousands or even millions of records. Logically, the larger a database is, the slower the process of finding the record or records you need.
How to Create Indexes in MySQL?
So, for a large database, such as the “employees” database, we can use an index that will increase the speed of searches related to a table.
Let’s create one!
As usual, it is intuitive.
- CREATE INDEX
- index name
- table name
- and a column name, or column names, attached in parentheses.
The parentheses serve us to indicate the column names on which our search will be based. It will be sped up, and the data will be filtered in a quicker way. Speaking technically, the idea is to choose columns, so your search will be optimized. These must be fields from your data table you will search frequently.
Side note: Imagine we must frequently sort the people in the “employees” table according to their hire date. Let’s run the following query:
hire_date > ‘2000-01-01’;
12! And it took the computer around 0.235 seconds to deliver the result. Naturally, this number could vary on different machines. But we can use the number obtained here as an example.
Writing the Code
Now, we will create an index with a name starting with “I”, standing for “index”. It will be “I_hire_date”.
So, the code will be:
CREATE INDEX i_hire_date ON employees(hire_date);
You can tell that this is true from the picture above.
What Are Composite Indexes in MySQL?
Another useful feature we have in MySQL is composite indexes. They are applied to multiple columns, not just a single one. The syntactical structure to abide by is the same. All a programmer needs to do is carefully pick the columns that would optimize his search. Then, he should type the chosen column names in the parentheses attached to the table name.
So, let’s repeat the exercise, this time using a composite index.
How to Create a Composite Index?
We will create an index referring to the same ‘employees’ table, searching data from two fields – first name and last name.
The way the SQL optimizer works is the following.
- First, it will look up rows based on the data in the first designated column; just as if you were using a single-column index.
- Then, based on the obtained intermediary output, it will proceed and search based on the second value.
Implementing the Index
Therefore, let’s SELECT all employees bearing the name “Georgi Facello”. We can achieve that by writing:\
first_name = ‘Georgi’
AND last_name = ‘Facello’;
Again, as you can see in the picture above, it took us approximately 0.172 seconds to obtain the output.
How about we create the “I_composite” index on the fields “first name” and “last name” from the “employees” table. The query should look like this:
CREATE INDEX i_composite ON employees(first_name, last_name);
Then, we can re-run the SELECT statement.
As shown in the picture above, it is quicker.
Other Types of Indexes in MySQL
Basically, these two examples showed how beneficial it is to use indexes.
However, please remember there are also other types of indexes in MySQL. Surprisingly or not, you might have worked with some!
Primary and Unique keys are indexes in MySQL. And it makes sense, when you think of it. They represent columns on which a person would typically base their search. In the “employees” database, a good example is the “emp_no” primary key from the “employees” table.
The reason is, this column represents unique values an analyst could take advantage of to extract distinct values from the data table.
How to Display the Indexes?
There are two alternative ways you can ask Workbench to display a list with the indexes in use.
In the same way, we can check the indexes related to a specific table. For instance, let’s select the “Indexes” tab for the “employees” table. What we will see is a list of three elements: the primary key column and the two objects we created in this tutorial – “I_hire_date” and “I_composite”.
Another Way to Do it
Alternatively, we can simply type and run:
- SHOW INDEX FROM
- the table name of interest
- the database the table belongs to
- and a semi-colon.
In our case, we have selected a default database, so the second “FROM employees” could be omitted from the query.
Why Use Indexes?
To conclude, we would like to add that SQL specialists are always aiming for a good balance between the improvement of speed search and the resources used for its execution. An index occupies memory space and could be redundant unless it can contribute to a quicker search.
Therefore, for small datasets, the costs of having an index might be higher than the benefits. However, for large databases, a well-optimized index can make a positive impact on the search process.
The Next Step
So, if you can’t wait to dive into another advanced SQL topic, feel free to embark on the journey where we will talk about the CASE statement in SQL.
Eager to hone your SQL skills? Enroll in our SQL course.