What are indexes?
Nobody likes slow systems.
High system performance is of prime importance in almost all database systems .
Most businesses invest heavily in hardware so that data retrievals and manipulations can be faster.
But there is limit to hardware investments a business can make.
Optimizing your database is a cheaper and better solution.
Towards this end we can use INDEXES.
The slowness in the response time is usually due to the records being stored randomly in database tables.
Search queries have to loop through the entire randomly stored records one after the other to locate the desired data.
This results in poor performance databases when it comes to retrieving data from large tables
Indexes come in handy in such situations. Indexes sort data in an organized sequential way.Think of an index as an alphabetically sorted list. It is easier to lookup names that have been sorted in alphabetical order than ones that are not sorted.
INDEX’s are created on the column(s) that will be used to filter the data.
Using indexes on tables that are frequently updated can result in poor performance. This is because MySQL creates a new index block every time that data is added or updated in the table. Generally, indexes should be used on tables whose data does not change frequently but is used a lot in select search queries.
Create index basic syntax
Indexes can be defined in 2 ways
1. At the time of table creation
2. After table has been created
For our myflixdb we expect lots of searches to the database on full name.
We will add the “full_names” column to Index in a new table “members_indexed”.
The script shown below helps us to achieve that.
Execute the above SQL script in MySQL workbench against the “myflixdb”.
Refreshing the myflixdb shows the newly created table named members_indexed.
“Note” members_indexed table has “full_names” in the indexes node.
As the members base expand and the number of records increases , search queries on the members_indexed table that use the WHERE and ORDER BY clauses will be much faster compared to the ones performed the members table without the index defined.
Add index basic syntax
The above example created the index when defining the database table. Suppose we already have a table defined and search queries on it are very slow. They take too long to return the results. After investigating the problem, we discover that we can greatly improve the system performance by creating INDEX on the most commonly used column in the WHERE clause.
We can use following query to add index
Let’s suppose that search queries on the movies table are very slow and we want to use an index on the “movie title” to speed up the queries, we can use the following script to achieve that.
Executing the above query creates an index on the title field in the movies table.
This means all the search queries on the movies table using the “title” will be faster.
Search queries on other fields in the movies table will however still are slower compared to the ones based on the indexed field.
Note you can create indexes on multiple columns if necessary depending on the fields that you intend to use for your database search engine.
If you want to view the indexes defined on a particular table, you can use the following script to do that.
Let’s now take a look at all the indexes defined on the movies table in the myflixdb.
Executing the above script in MySQL workbench against the myflixdb gives us the following results shown below.
Note the primary and foreign keys on the table have already been indexed by MySQL. Each index has its own unique name and the column on which it is defined is shown as well.
Drop index basic syntax
The drop command is used to remove already defined indexes on a table.
There may be times when you have already defined an index on a table that is frequently updated. You may want to remove the indexes on such a table to improve the UPDATE and INSERT queries performance. The basic syntax used to drop an index on a table is as follows.
Let’s now look at a practical example.
Executing the above command drops the index with id ` full_names ` from the members_indexed table.
Indexes are very powerful when it comes to greatly improving the performance of MySQL search queries.
Indexes can be defined when creating a table or added later on after the table has already been created.
You can define indexes on more than one column on a table.
The SHOW INDEX FROM table_name is used to display the defined indexes on a table.
The DROP command is used to remove a defined index on a given table.
Back to SQL Tutorials Index Page