An index is a data structure (usually a B-tree) that lets the database find rows without scanning the whole table – like the index at the back of a book. The right index can turn a slow query into an instant one.
What You Will Learn
- What an index is and how it works
- Create indexes and see the effect with EXPLAIN
- Composite and unique indexes
- Which columns to index
- The cost of too many indexes
Prerequisites
Measure the Difference
CREATE TABLE orders (id INTEGER PRIMARY KEY, customer_id INTEGER, amount INTEGER);
-- 500,000 random orders inserted
EXPLAIN QUERY PLAN
SELECT COUNT(*), SUM(amount) FROM orders WHERE customer_id = 4242;
SCAN orders
CREATE INDEX idx_orders_customer ON orders(customer_id);
EXPLAIN QUERY PLAN
SELECT COUNT(*), SUM(amount) FROM orders WHERE customer_id = 4242;
SEARCH orders USING INDEX idx_orders_customer (customer_id=?)
without index: 17.7 ms, with index: 0.006 ms
Explanation: Without an index the database scans all 500,000 rows. With the index it jumps straight to the matching rows – thousands of times faster in our SQLite test (exact times depend on your machine). Use EXPLAIN (MySQL) or EXPLAIN ANALYZE (PostgreSQL) to see whether your query uses an index.
Types of Indexes
CREATE UNIQUE INDEX idx_students_email ON students(email); -- no duplicates allowed
CREATE INDEX idx_orders_cust_date ON orders(customer_id, order_date); -- composite
DROP INDEX idx_orders_customer;
| Index | Use |
|---|---|
| Primary key | Created automatically; unique row id |
| Unique | Prevent duplicates (email, phone) |
| Composite | Queries filtering on several columns; column order matters |
| Full-text | Searching words in long text |
| Vector (pgvector HNSW) | Similarity search on embeddings – see Vector Databases |
What to Index
- Columns in WHERE filters that return few rows.
- Columns used in JOIN conditions (foreign keys).
- Columns used in ORDER BY with LIMIT.
- Do not index tiny tables or columns with very few distinct values (e.g. gender) – a scan may be faster.
The Cost
Every INSERT, UPDATE and DELETE must also update each index, and indexes use disk space. Add indexes for real slow queries, not “just in case”.
Series
SQL series: SQL Basics · Advanced SQL · Views · Indexing · Stored Procedures · Database Design · SQL for Analytics · MySQL · PostgreSQL · MongoDB · NoSQL vs SQL · Interview Questions
Interview Questions
Q: How does an index speed up queries?
It keeps column values sorted in a tree so the database can search instead of scanning every row.
Q: Why not index every column?
Indexes slow down writes and use storage; unused indexes are pure cost.
Q: What is the leftmost prefix rule?
A composite index can be used only when the query filters on its first column(s).
Practice Exercises
- Run EXPLAIN on a slow query in your database, add an index and compare.
- Decide which indexes a “students + enrollments” app needs.
Summary
Indexes let databases search instead of scan. Index filter and join columns, check with EXPLAIN, mind composite-index column order and avoid unnecessary indexes.
Next: Stored Procedures
Machine Learning Tutorials, Courses and Certifications