⚙ Performance Optimization
The CREATE INDEX statement in MySQL is used to create an index on one or more columns of a table. Indexes act like a fast lookup for rows and can dramatically speed up SELECT queries, especially on large tables. However, they also add some overhead to INSERT, UPDATE, and DELETE operations.
idx_employee_name).The basic syntax to create an index on a single column is:
-- Create a simple index on one column
CREATE INDEX index_name
ON table_name (column_name);
You can also create an index on multiple columns (composite index):
-- Create a composite index on multiple columns
CREATE INDEX index_name
ON table_name (column1, column2);
Example: creating an index on the Name column of an Employees table.
-- Create an index on the Name column in Employees table
CREATE INDEX idx_employee_name
ON Employees (Name);
idx_employee_name on the Name column.SELECT * FROM Employees WHERE Name = 'John'; MySQL can use the index to quickly find matching rows instead of scanning the entire table.Name changes or a new row is inserted/deleted.WHERE, JOIN, ORDER BY, or GROUP BY clauses.Employees table and insert sample data. Then:
SELECT query with a WHERE condition on Name.Name.EXPLAIN.Department and JoiningDate) and run queries that use both columns in the WHERE clause.DROP INDEX index_name ON table_name; and see how it affects query plans.