Introduction
Indexes help the database find data faster, like a shortcut in a book's index that points directly to the page you want.
CREATE INDEX index_name ON table_name (column_name);
CREATE INDEX idx_lastname ON employees (last_name);
CREATE INDEX idx_name_age ON users (name, age);
CREATE TABLE books ( id INT PRIMARY KEY, title VARCHAR(100), author VARCHAR(50) ); INSERT INTO books VALUES (1, 'Learn SQL', 'Alice'), (2, 'Data Basics', 'Bob'), (3, 'SQL Speed', 'Alice'); CREATE INDEX idx_author ON books (author); EXPLAIN SELECT * FROM books WHERE author = 'Alice';
email in a table users?products(id INT, name VARCHAR(50), price DECIMAL) with an index on price, what will be the effect of this query?EXPLAIN SELECT * FROM products WHERE price = 100;username but your query SELECT * FROM users WHERE username = 'alice'; is still slow. What could be the problem?orders with columns order_id, customer_id, and order_date. You often run this query:SELECT * FROM orders WHERE customer_id = 123 AND order_date > '2023-01-01';Pass the test and this pattern moves unknown