Introduction
A composite index helps the database find data faster when searching using multiple columns together. The order of columns in the index matters because it affects how quickly the database can use the index.
CREATE INDEX index_name ON table_name (column1, column2, ...);
CREATE INDEX idx_name ON employees (last_name, first_name);
CREATE INDEX idx_date_status ON orders (order_date, status);
CREATE TABLE orders ( order_id INT PRIMARY KEY, customer_id INT, order_date DATE, status VARCHAR(20) ); CREATE INDEX idx_date_status ON orders (order_date, status); EXPLAIN QUERY PLAN SELECT * FROM orders WHERE order_date = '2024-01-01' AND status = 'shipped';
composite index in a database?last_name and first_name in a table employees?(city, age) in a table users, which query will use the index efficiently?(department, salary) but your query filtering only on salary is slow. What is the likely cause?orders with columns customer_id, order_date, and status. You often run queries filtering by customer_id and sorting by order_date. Which composite index will optimize these queries best?Pass the test and this pattern moves unknown