Creating and Dropping Indexes
Part of the SQL Roadmap
In short
One line of SQL can turn a 20-second search into an instant one. Knowing how to add an index, and when to remove one, is the first real performance skill you learn.
CREATE INDEX adds an index on one or more columns and DROP INDEX removes it. Primary keys and unique constraints usually create indexes automatically.

Think of it like this
Sticky tabs on the kirana khata
Sharma ji's credit register has hundreds of pages. To find Mrs Patel's account he used to flip through all of them. Now he sticks coloured tabs along the edge, one per letter, and jumps straight to the right page. The tabs are an index. Peeling one off doesn't erase a single entry, it just makes that search slow again.
Watch it happen
What CREATE INDEX actually does
1/5The database reads every row of orders and pulls out the customer_id of each one, plus a pointer to where that row lives.
The big ideas
What you need to know
CREATE INDEX
CREATE INDEX name ON table (column). Name it after the table and column so you know what it is later.
DROP INDEX
Removes only the index. Rows stay untouched, but queries that relied on it may slow down.
Some indexes are automatic
PRIMARY KEY and UNIQUE create an index for you. In PostgreSQL, foreign key columns do not, so you add those yourself.
Building takes time
On a big table, CREATE INDEX reads every row and blocks writes. CREATE INDEX CONCURRENTLY avoids the block in PostgreSQL.
See it in code
Walk through the code
1-- 1. See which indexes already exist2SELECT indexname, indexdef3FROM pg_indexes4WHERE tablename = 'orders';5 6-- 2. Add an index on a column you filter or join on often7CREATE INDEX idx_orders_customer_id ON orders (customer_id);8 9-- 3. On a busy live table, build it without blocking writes10CREATE INDEX CONCURRENTLY idx_orders_order_date ON orders (order_date);11 12-- 4. Remove an index nobody needs any more13DROP INDEX IF EXISTS idx_orders_old_status;1/4Always check first. The primary key index is probably already there, and you don't want a duplicate.
PostgreSQL syntax. CONCURRENTLY can't run inside a transaction block. In MySQL, list indexes with SHOW INDEX FROM orders and drop one with DROP INDEX idx_name ON orders.
Save yourself the pain
Common mistakes
Oops
Assuming PostgreSQL indexes foreign key columns automatically.
Do this instead
Only primary keys and unique constraints get one. Add an index on orders.customer_id yourself. MySQL InnoDB does create one for foreign keys.
Oops
Running a plain CREATE INDEX on a huge production table at lunchtime.
Do this instead
Use CREATE INDEX CONCURRENTLY, or schedule it for a quiet window, because a normal build blocks inserts and updates.
Oops
Naming indexes idx1, idx2, idx3.
Do this instead
Use names like idx_orders_customer_id. Six months later you will know exactly what to keep or drop.
Your mission
Before and after
Quick check
Which of these gets an index automatically in PostgreSQL?
Go deeper
- 1Create an index on a column you often filter by.
- 2Time the query before and after on a table with many rows.
- 3Remove an index that no query uses.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.