In MySQL, the CREATE INDEX statement is used to create an index on one or more columns in a table. Indexes improve the speed of data retrieval operations by allowing the database engine to quickly locate rows based on the values in the indexed columns.
Here's the basic syntax for creating an index:
CREATE INDEX index_name
ON table_name (column1, column2, ...);
For example, suppose you have a table named employees with columns id, first_name, and last_name, and you frequently search for employees by their last names. You can create an index on the last_name column to improve the search performance:
CREATE INDEX idx_last_name
ON employees (last_name);
This statement creates an index named idx_last_name on the last_name column of the employees table.
You can also create composite indexes on multiple columns to improve the performance of queries that involve multiple columns. For example:
CREATE INDEX idx_full_name
ON employees (first_name, last_name);
This statement creates a composite index named idx_full_name on the first_name and last_name columns of the employees table.
It's important to note that while indexes can improve query performance, they also consume additional storage space and may impact the performance of data modification operations (such as INSERT, UPDATE, and DELETE). Therefore, it's essential to carefully consider the trade-offs when creating indexes.