Database indexing is one of the most impactful performance optimizations available. A well-placed index can turn a 30-second query into a 30-millisecond one. But indexes aren't magic - understanding how they work helps you use them effectively.
How B-Tree Indexes Work
The most common index type. Think of it like a phone book - instead of scanning every page, you jump to the right section.
-- Without index: Full table scan (slow)
SELECT * FROM orders WHERE customer_id = 12345;
-- Scans all 10 million rows!
-- Create an index
CREATE INDEX idx_orders_customer ON orders(customer_id);
-- With index: Index seek (fast)
SELECT * FROM orders WHERE customer_id = 12345;
-- Jumps directly to matching rows!Composite Indexes
When queries filter on multiple columns, composite indexes are crucial:
-- This query needs a composite index
SELECT * FROM orders
WHERE customer_id = 12345
AND status = 'shipped'
AND created_at > '2024-01-01';
-- The index order matters! (left-to-right rule)
CREATE INDEX idx_orders_composite
ON orders(customer_id, status, created_at);The Leftmost Prefix Rule
A composite index on (A, B, C) can satisfy queries on:
AaloneAandBA,B, andC- But NOT
Balone orCalone!
When NOT to Index
- Low-cardinality columns (e.g., boolean, status with few values)
- Frequently updated columns - indexes slow down writes
- Small tables - Full scan may be faster than index lookup
- Columns rarely used in WHERE/JOIN/ORDER BY
Analyzing Query Performance
-- PostgreSQL
EXPLAIN ANALYZE SELECT * FROM orders WHERE customer_id = 12345;
-- MySQL
EXPLAIN SELECT * FROM orders WHERE customer_id = 12345;
-- Look for:
-- "Seq Scan" = Full table scan (bad for large tables)
-- "Index Scan" = Using index (good)
-- "Bitmap Index Scan" = Using index for multiple conditions (good)Index Types Comparison
| Type | Best For | Example |
|---|---|---|
| B-Tree | Equality, range queries | WHERE price > 100 |
| Hash | Exact equality only | WHERE id = 5 |
| GiST | Geometric/spatial data | ST_Contains(area, point) |
| GIN | Full-text search, arrays | WHERE tags @> ARRAY['js'] |
| BRIN | Large, naturally ordered | Time-series data |
Golden Rules of Indexing
- Always check the query plan with EXPLAIN
- Index columns used in WHERE, JOIN, and ORDER BY
- Put high-cardinality columns first in composite indexes
- Monitor index usage and remove unused indexes
- Remember: every index slows down INSERT/UPDATE/DELETE
No comments yet. Be the first!