Monday , November 30 2026
SQL indexes explained with a tested 500,000-row example: EXPLAIN query plans, unique and composite indexes, what to index and costs.

SQL Indexing: How Indexes Speed Up Queries

Updated: 29 November 2026Level: IntermediateReading time: about 9 minutesSQL · tested on SQLite

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

SQL Basics.

Measure the Difference

Example
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;
Output
SCAN orders
Example
CREATE INDEX idx_orders_customer ON orders(customer_id);

EXPLAIN QUERY PLAN
SELECT COUNT(*), SUM(amount) FROM orders WHERE customer_id = 4242;
Output
SEARCH orders USING INDEX idx_orders_customer (customer_id=?)
Output
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

Example
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
Note: A composite index on (customer_id, order_date) helps queries that filter on customer_id, or on customer_id and order_date – but not queries that filter only on order_date (the “leftmost prefix” rule).

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

  1. Run EXPLAIN on a slow query in your database, add an index and compare.
  2. 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

About Machine Learning

Check Also

SQL Views: Create, Use and Drop with Examples

SQL views explained: create and query views, security and consistency benefits, limitations and materialized views, with tested examples.