Home
/
Database Support
/
MySQL optimization using indexes

MySQL optimization using indexes

A database index is a data structure that improves the speed of operations in a table.

Every time your web application runs a database query, the database will look through all the rows in your table to find those that match your request. As your database tables grow, an increasing number of rows need to be inspected each time which can slow the overall performance of the database and respectively your application.

MySQL indexes solve this problem, by taking data from a column in your table and storing it alphabetically in a separate location called an index. More on that can be found here.

Indexes can be created easily via phpMyAdmin in your Site Tools. Find below an example of such index:

Let’s take an example table called “sample” with only two rows – “number” and “employee“. If you run a simple SQL query such as:

SELECT * FROM sample WHERE number = 4;

MySQL will check all records and will return only the one that has its number value set to 4.

But if you have several thousand entries, for example, this will be a slow query. In this case, we have a unique field – “number“. Therefore, we can create an index for it. Indexing will create an internal register that is saved in by the MySQL service. It can be done with the following query:

ALTER TABLE sample ADD INDEX (number);

Once this index is set, next time you want to get the information for employee number 4, the service will go directly to it using the index and will return the information much faster.

This is just a very basic example. For bigger databases, the difference in the loading time can be significant. Indexing your database can drastically decrease the loading time of your web applications.

There is another query that you can use to increase the loading speed of your database:

OPTIMIZE TABLE sample;

You can also check the tutorial below which will help you to Repair/Optimize your database using phpMyAdmin:

Repair and Optimize database

Share This Article