Introduction
Composite indexes help speed up searches when you look for data using more than one column together.
CREATE INDEX index_name ON table_name (column1, column2, ...);
CREATE INDEX idx_name_age ON users (name, age);
CREATE INDEX idx_city_state ON customers (city, state);
CREATE TABLE employees ( id INT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), department VARCHAR(50) ); CREATE INDEX idx_name_dept ON employees (last_name, department); INSERT INTO employees (id, first_name, last_name, department) VALUES (1, 'Alice', 'Smith', 'Sales'), (2, 'Bob', 'Johnson', 'HR'), (3, 'Carol', 'Smith', 'HR'); EXPLAIN SELECT * FROM employees WHERE last_name = 'Smith' AND department = 'HR';
composite index in MySQL?first_name and last_name in a table users?(city, age) in a table people, which query will use the index efficiently?(department, salary) but your query SELECT * FROM employees WHERE salary > 50000; is slow. What is the likely problem?orders with columns customer_id, order_date, and status. You often run this query:SELECT * FROM orders WHERE customer_id = 123 AND status = 'shipped' ORDER BY order_date DESC;Pass the test and this pattern moves unknown