Indexes & Performance: Making Queries Fast
EXPLAIN QUERY PLAN; EXPLAIN ANALYZE is PostgreSQL.After this lesson, you will be able to:
- Explain what an index is and how B-tree indexes speed up lookups
- Create single-column and composite (multi-column) indexes
- Use EXPLAIN ANALYZE (Postgres) / EXPLAIN QUERY PLAN (SQLite) to see how the database executes a query
- Identify when indexes help and when they hurt performance
- Understand covering indexes and how they eliminate table lookups
- Apply indexing best practices for common query patterns
- Know when to reach for B-tree vs Hash vs GIN vs BRIN (Postgres index types)
Dialect note. The playgrounds run SQLite, so useEXPLAIN QUERY PLANthere.EXPLAIN ANALYZE,CREATE INDEX CONCURRENTLYand the plan output shown below are PostgreSQL; read them here and run them in a PostgreSQL database.
If you have written queries that "just work" on small tables, this is where you learn why the same query might crawl on real-world data, and how to fix it. Indexing is the bridge between toy databases and production systems.
#What Is an Index?
#Without an Index: Full Table Scan
-- Without an index on email, the database reads EVERY row
SELECT * FROM users WHERE email = 'alice@example.com';
-- On 10 million rows, this means:
-- 10,000,000 row comparisons
-- ~5 seconds on a typical disk
#With an Index: B-Tree Lookup
-- Create an index on the email column
CREATE INDEX idx_users_email ON users(email);
-- Now the same query uses the index
SELECT * FROM users WHERE email = 'alice@example.com';
-- On 10 million rows:
-- ~23 B-tree comparisons (log2 of 10M)
-- ~0.001 seconds
#How B-Trees Work (Simplified)
A B-tree is a balanced tree structure where:
- Values are stored in sorted order
- Each node can have multiple children (typically hundreds)
- Looking up a value requires traversing from root to leaf: O(log n) steps
- The tree stays balanced as data is inserted and deleted
[Johnson, Smith] -- Root node
/ | \
[Adams, Chen] [Kim, Lee] [Taylor, Wong] -- Internal nodes
/ | \ / | \ / | \
[rows] [rows] [rows] ... [rows] [rows] -- Leaf nodes (point to actual rows)
For 10 million rows with a branching factor of 100:
- Level 0 (root): 1 node
- Level 1: ~100 nodes
- Level 2: ~10,000 nodes
- Level 3: ~1,000,000 leaf nodes
Only 3-4 levels to traverse — that is why index lookups are so fast.
#Creating Indexes
#Single-Column Index
-- Index on a single column
CREATE INDEX idx_employees_name ON employees(name);
CREATE INDEX idx_orders_date ON orders(order_date);
CREATE INDEX idx_products_category ON products(category);
-- Unique index (enforces uniqueness like a UNIQUE constraint)
CREATE UNIQUE INDEX idx_users_email ON users(email);
#Composite (Multi-Column) Index
-- Index on multiple columns (order matters!)
CREATE INDEX idx_orders_customer_date
ON orders(customer_id, order_date);
-- This index helps with:
-- WHERE customer_id = 42 (uses first column)
-- WHERE customer_id = 42 AND order_date > '2024' (uses both columns)
-- ORDER BY customer_id, order_date (matches index order)
-- This index does NOT help with:
-- WHERE order_date > '2024' (skips the first column -- can't use index)
#Dropping Indexes
-- Remove an index
DROP INDEX idx_employees_name;
-- Safe version
DROP INDEX IF EXISTS idx_employees_name;
You create a composite index: CREATE INDEX idx ON orders(customer_id, order_date). Which query can use this index?
#EXPLAIN QUERY PLAN
EXPLAIN QUERY PLAN shows you how the database will execute your query BEFORE it runs. It is your most important performance debugging tool.#Reading EXPLAIN Output
EXPLAIN QUERY PLAN
SELECT * FROM employees WHERE department_id = 2;
Possible outputs:
-- WITHOUT index: full table scan
SCAN employees
-- WITH index on department_id:
SEARCH employees USING INDEX idx_emp_dept (department_id=?)
Key terms in EXPLAIN output:
- SCAN: Full table scan (reads every row). Slow on large tables.
- SEARCH USING INDEX: Uses an index to find matching rows. Fast.
- USING COVERING INDEX: The index contains all needed columns — no table lookup needed. Fastest.
- USING TEMPORARY B-TREE: Creating a temporary sort structure (for ORDER BY or DISTINCT).
#Examples
-- Check if a JOIN uses indexes
EXPLAIN QUERY PLAN
SELECT e.name, d.name
FROM employees e
JOIN departments d ON e.department_id = d.id
WHERE d.name = 'Engineering';
-- Output might show:
-- SEARCH departments USING INDEX sqlite_autoindex_departments_1 (name=?)
-- SEARCH employees USING INDEX idx_emp_dept (department_id=?)
-- Both tables use indexes -- good!
-- Check if an ORDER BY needs a temporary sort
EXPLAIN QUERY PLAN
SELECT * FROM employees ORDER BY salary DESC;
-- Output might show:
-- SCAN employees
-- USE TEMP B-TREE FOR ORDER BY
-- No index on salary, so full scan + sort. Consider adding an index.
Try it! Run EXPLAIN QUERY PLAN on different queries and see how the database uses (or does not use) your indexes.
#When to Index (and When NOT to)
#When Indexes Help
-- 1. WHERE clause filters (the most common use case)
SELECT * FROM users WHERE email = 'alice@example.com';
-- Index on: users(email)
-- 2. JOIN conditions
SELECT * FROM orders o JOIN customers c ON o.customer_id = c.id;
-- Index on: orders(customer_id) -- the foreign key column
-- 3. ORDER BY (avoids temp B-tree sort)
SELECT * FROM products ORDER BY price DESC;
-- Index on: products(price)
-- 4. GROUP BY
SELECT department_id, COUNT(*) FROM employees GROUP BY department_id;
-- Index on: employees(department_id)
-- 5. Unique lookups (already indexed if UNIQUE or PRIMARY KEY)
SELECT * FROM users WHERE id = 42;
-- PRIMARY KEY already has an index
#When Indexes Hurt
-- 1. Tables with very few rows (< 1000)
-- Index overhead is not worth it. Full scan is fast enough.
-- 2. Columns with very low cardinality
CREATE INDEX idx_gender ON users(gender);
-- Only 2-3 distinct values. The index barely narrows the search.
-- 3. Columns that are rarely used in WHERE/JOIN/ORDER BY
CREATE INDEX idx_bio ON users(bio);
-- If you never filter or sort by bio, this index wastes space and slows writes.
-- 4. Tables with heavy write traffic and light read traffic
-- Every INSERT/UPDATE/DELETE must also update every index.
-- On a logging table with 10,000 inserts/second and rare reads, indexes are costly.
#The Write Penalty
Every index must be maintained on every write:
| Operation | Without Index | With 1 Index | With 5 Indexes |
|---|---|---|---|
| INSERT | 1 write | 2 writes | 6 writes |
| UPDATE (indexed col) | 1 write | 2 writes | varies |
| DELETE | 1 write | 2 writes | 6 writes |
A table has 50 rows. Should you create an index on it?
#Covering Indexes
-- Query: find names and salaries for a department
SELECT name, salary FROM employees WHERE department_id = 2;
-- Regular index: find rows via index, then look up name and salary from the table
CREATE INDEX idx_dept ON employees(department_id);
-- Two steps: index lookup + table lookup
-- Covering index: includes ALL columns the query needs
CREATE INDEX idx_dept_covering ON employees(department_id, name, salary);
-- One step: everything is in the index. No table lookup needed.
In EXPLAIN output, a covering index shows as:
SEARCH employees USING COVERING INDEX idx_dept_covering (department_id=?)
The word "COVERING" means zero table lookups — maximum speed.
#When to Use Covering Indexes
- Queries that run thousands of times per second (API hot paths)
- Queries that scan many rows but need only a few columns
- Dashboard queries that aggregate data (COUNT, SUM, AVG)
#The Trade-Off
Covering indexes are wider (more columns = more storage). They slow down writes more than narrow indexes. Use them for your most critical read queries, not for everything.
-- This covering index helps this specific query pattern:
CREATE INDEX idx_orders_covering
ON orders(customer_id, order_date, total);
-- Fast query (covered):
SELECT order_date, total FROM orders WHERE customer_id = 42;
-- This query is NOT covered (needs 'status' column, which is not in the index):
SELECT order_date, status FROM orders WHERE customer_id = 42;
#Practical Indexing Strategy
#Step 1: Identify Slow Queries
-- In development: use EXPLAIN QUERY PLAN on every important query
EXPLAIN QUERY PLAN
SELECT * FROM orders WHERE customer_id = 42 ORDER BY order_date DESC;
-- Look for: SCAN (full table scan) and USE TEMP B-TREE (sort without index)
#Step 2: Index Foreign Keys
-- ALWAYS index foreign key columns (used in JOINs)
CREATE INDEX idx_orders_customer ON orders(customer_id);
CREATE INDEX idx_order_items_order ON order_items(order_id);
CREATE INDEX idx_order_items_product ON order_items(product_id);
CREATE INDEX idx_employees_dept ON employees(department_id);
#Step 3: Index Common WHERE Patterns
-- Index columns used in frequent WHERE clauses
CREATE INDEX idx_users_email ON users(email); -- login lookup
CREATE INDEX idx_orders_status ON orders(status); -- filter by status
CREATE INDEX idx_products_category ON products(category); -- category pages
#Step 4: Index for ORDER BY / GROUP BY
-- Composite index that helps both WHERE and ORDER BY
CREATE INDEX idx_orders_customer_date
ON orders(customer_id, order_date DESC);
-- This query benefits from the index for both filtering AND sorting:
SELECT * FROM orders
WHERE customer_id = 42
ORDER BY order_date DESC
LIMIT 10;
#Step 5: Measure and Iterate
-- Check which indexes exist on a table
PRAGMA index_list('orders');
-- Check which columns an index covers
PRAGMA index_info('idx_orders_customer_date');
-- Check the size of your database (indexes included)
PRAGMA page_count;
PRAGMA page_size;
-- Total size = page_count * page_size bytes
#Index Pitfalls
#Functions Disable Indexes
-- BAD: wrapping a column in a function prevents index use
SELECT * FROM users WHERE LOWER(email) = 'alice@example.com';
-- The index on email is NOT used because LOWER() transforms the column
-- GOOD: store data normalized, query without functions
SELECT * FROM users WHERE email = 'alice@example.com';
-- Index on email IS used
#OR Can Disable Indexes
-- May not use indexes efficiently
SELECT * FROM users WHERE email = 'alice@example.com' OR name = 'Alice';
-- Better: use UNION (each sub-query can use its own index)
SELECT * FROM users WHERE email = 'alice@example.com'
UNION
SELECT * FROM users WHERE name = 'Alice';
#Wildcards at the Start Disable Indexes
-- BAD: leading wildcard -- full scan
SELECT * FROM users WHERE name LIKE '%alice%';
-- GOOD: prefix match -- can use index
SELECT * FROM users WHERE name LIKE 'Alice%';
Indexes are one way to avoid reading rows. Partitioning is another: Postgres can split a big table into pieces and skip the pieces a query cannot match. The lab counts how many rows a query for 2024 reads under three layouts.
- Do: Start on None, then pick Range by date, then Hash by customer_id, and read the Rows Scanned number each time.
- Notice: None reads all 1,000,000 rows. Range by date reads only the 340,000 rows of the 2024 partition. Hash by customer_id has 8 small partitions yet still reads all 1,000,000, because the filter is on order_date.
- Why: The planner can skip a partition only when the query filters on the partition key. Drawn stand-in, not a database: the row counts are fixed made-up numbers and cost is simply rows read. Partitioning is a Postgres feature that the SQLite playground does not run.
Indexing is one of those skills where a little knowledge goes a long way. Most applications need only a handful of well-chosen indexes to keep their queries fast. Start with foreign keys, add indexes for your slowest queries, use EXPLAIN QUERY PLAN to verify, and resist the urge to index everything. You now have the tools to diagnose and fix many common SQL performance problems.
What type of data structure do most SQL indexes use?