When an endpoint is slow, the database is the first place to look, and the same handful of causes come up again and again. This is the order I work through them. Examples use PostgreSQL syntax; the ideas apply to MySQL and SQL Server too.
1. Find the slow query before guessing
Do not optimize from intuition. Turn on slow-query logging, or in PostgreSQL enable pg_stat_statements, and sort by total time. The query that runs 10,000 times at 30 ms costs more than the one that runs twice at 2 seconds.
2. Read the plan
EXPLAIN ANALYZE runs the query and shows what the database actually did.
EXPLAIN ANALYZE
SELECT * FROM orders
WHERE customer_id = 42 AND status = 'paid'
ORDER BY created_at DESC
LIMIT 20;
Three things to look for:
- Seq Scan on a large table. The database read every row. Usually a missing index.
- Estimated rows far from actual rows. The planner's statistics are stale; run
ANALYZE. - A sort or hash that spills to disk. The query is handling more rows than it needs to.
3. Add the index the query needs
An index helps when it matches how the query filters and sorts. For the query above, a composite index covers the filter and the sort in one structure:
CREATE INDEX orders_customer_status_created
ON orders (customer_id, status, created_at DESC);
Column order matters. Put the columns compared with = first and the range or sort column last. An index on (status, customer_id) works too, but one on (created_at, customer_id) would not help this filter.
Things that silently stop an index from being used:
- wrapping the column in a function:
WHERE LOWER(email) = ...needs an index onLOWER(email) - a leading wildcard:
LIKE '%smith' - comparing a column to a value of a different type
Indexes are not free. Each one slows down writes and takes space, so add them for queries that exist, not ones you imagine.
4. Remove N+1 queries
The classic ORM problem: one query for a list, then one more per row.
SELECT * FROM orders LIMIT 50;
SELECT * FROM customers WHERE id = 1;
SELECT * FROM customers WHERE id = 2;
... 48 more
Each query is fast, so nothing shows up in the slow log, but the page makes 51 round trips. Fix it with a join or by loading the related rows in one query with WHERE id IN (...). Every ORM has a way to ask for this (eager loading, include, joinedload). Log the query count per request in development and N+1 becomes obvious.
5. Fetch less
- Select the columns you use.
SELECT *drags large text and JSON columns across the network for nothing. - Filter in the database, not in application code.
- Paginate. No screen needs 10,000 rows.
6. Paginate with keys, not offsets
OFFSET 100000 LIMIT 20 makes the database produce 100,020 rows and throw away the first 100,000. Keyset pagination jumps straight to the right place using the index:
SELECT * FROM orders
WHERE customer_id = 42
AND created_at < '2026-10-01T12:00:00Z'
ORDER BY created_at DESC
LIMIT 20;
The client sends the created_at of the last row it saw. Page 5,000 is as fast as page 1.
7. Then consider the bigger tools
Only after the steps above: caching results that are read often and change rarely, a materialized view for a heavy report, a read replica for analytics, or partitioning a table that has grown past what one index handles well.
Measure after every change with the same EXPLAIN ANALYZE. Most slow endpoints I have fixed needed step 3 or step 4 and nothing more.