Open In App

MySQL CREATE INDEX Statement

Last Updated : 18 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

MySQL is an open-source relational database management system that uses Structured Query Language (SQL) to manipulate databases. CREATE INDEX statement is used to retrieve data from the database more quickly.

CREATE INDEX statement is used to create an Index that allows MySQL to quickly locate and retrieve relevant data by reducing the need for full table scans and minimizing response times. In this article, we will learn about how to use the CREATE INDEX statement with examples.

MySQL CREATE INDEX Statement.

The MySQL CREATE INDEX statement is a DDL (Data Definition Language) statement and it is used to create indexes on tables. It allows developers to optimize query performance by specifying which columns should be indexed. The index is a data structure that provides a quick lookup mechanism for faster data retrieval.

Need for Indexing in MySQL:

  • Indexing allows the database engine to quickly locate and retrieve the data.
  • Indexed columns speed up the execution of SELECT, JOIN, and WHERE clauses.
  • Unique indexes ensure the uniqueness of values in specified columns by preventing duplicate entries.
  • Indexing improves the efficiency of sorting and grouping operations by providing an ordered structure for the data.

How to Create an Index in MySQL

To create an index we will use CREATE INDEX statement. This statement will allow you to define an index on one or more columns of a table.

Syntax:

CREATE [UNIQUE] INDEX index_name

ON table_name (column1, column2, …);

Example of MySQL CREATE INDEX

By this example, we will try to understand, how CREATE INDEX helps in quickly retrieve the data.

we will create index on this products table:

products_table

products table

Before creating a Index, we will analyze the query execution plan for a query filtering by the category column. This will show a full table scan, that indicate that MySQL is examining all rows in the table.

EXPLAIN SELECT * FROM products WHERE category = 'Electronics';
before_index

Analyzing Query Before Creating of Index

Now, we will create a index on a category column by using this query:

CREATE INDEX idx_category ON products (category);
creating_index

Creating Index

After creating a Index, we will analyze the query execution plan for a query filtering by the category column. The output showing that the query is using the idx_category index, resulting in a more efficient query execution plan.

EXPLAIN SELECT * FROM products WHERE category = 'Electronics';
after_index

Analyzing Query After Creating Index

To Drop index, we will use this query:

DROP INDEX idx_category ON products;
drop_index

Drop Index

Conclusion

In conclusion, MySQL CREATE INDEX is tool for optimizing database performance by improving query speed and efficiency. By selecting and defining indexes on the appropriate column you can improve the efficiency of data retrieval operations. Indexing allows MySQL to quickly locate and retrieve relevant data by reducing the need for full table scans and minimizing response times CREATE INDEX is a DDL (Data Definition Language) statement that is used to create indexes on tables.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads