Back

Indexes

An index is like the index in the back of a book: it helps the database find rows quickly without scanning the whole table. Add indexes on columns you search or join on often. They speed up reads but make writes slightly slower, so don't index everything.

-- Speed up lookups by email
CREATE INDEX idx_users_email ON users(email);

-- Unique index also prevents duplicates
CREATE UNIQUE INDEX idx_users_username ON users(username);

-- Index on two columns used together
CREATE INDEX idx_orders_user_date ON orders(user_id, created_at);

DROP INDEX idx_users_email;

Roadmap