Skip to content
Digital Security Consulting

Slow Database? Five Causes to Check Before You Rewrite

An application that crawls exactly when you're busiest is a revenue problem. The usual first proposal is a rewrite or a bigger server. Most of the time, the cause is one of five things — all fixable in place.

Published 3 min read

When an application starts struggling at peak times — month-end, a big campaign, the morning rush — the first proposals are usually a bigger server or a rewrite. Occasionally one of those is right. Far more often, the cause is one of five ordinary problems, all fixable without starting over.

Measure first. Turn on the database’s slow query log, or use its built-in statistics (PostgreSQL’s pg_stat_statements is a common choice), and rank queries by total time spent. The worst offender is rarely the one people suspect.

1. A missing or wrong index

The most common cause by far. A query that filters or sorts on a column without a suitable index has to scan the entire table. On a thousand rows, nobody notices. On a few million, it’s the whole problem.

The tell: one query dominates the slow log, and its query plan shows a full table scan.

2. N+1 queries

The page loads fifty records, then issues a separate query for each one to fetch related data — fifty-one queries where two would do. It’s invisible in development with ten records and crippling in production with ten thousand.

The tell: hundreds of nearly identical, individually fast queries per page load.

3. Unbounded result sets

A query with no limit that returns every matching row. Fine when the table was small; slow and memory-hungry once it grows.

The tell: a query whose runtime grows steadily over months with no code change.

4. Connection pool exhaustion

Every request waits for a free database connection. The database itself is barely working — the application is queuing to talk to it.

The tell: low database CPU, but high application response times and timeout errors under load.

5. No caching where it obviously belongs

The same expensive query runs thousands of times a minute and returns the same answer every time — a product catalogue, a settings table, a dashboard total.

The tell: identical queries, with identical results, at very high frequency.

Why this order matters

All five are fixable in place, usually in days or weeks, with low risk and an easy way back. A rewrite typically takes far longer than estimated, and while it’s happening you’re maintaining two systems and shipping nothing new.

A rewrite is sometimes genuinely right — when the architecture can’t support something the business now needs. But that conclusion should come from measurement, not replace it.

We walk through a typical case in the site crawls exactly when we’re busiest. Performance work is part of our infrastructure practice.

Frequently asked questions

Won't a bigger server fix it?

It buys time and permanently raises the bill. Most of these problems don't scale away with hardware: a query missing an index gets slower as the data grows, however much CPU you add. It's a reasonable emergency measure and a poor strategy.

How do we find the slow query?

Turn on the database's slow query log, or use its built-in statistics — PostgreSQL's pg_stat_statements is a common choice — to rank queries by total time spent. The worst offender is rarely the one people suspect.

When is a rewrite actually justified?

When the architecture genuinely cannot support something the business now requires — for example, real-time behaviour from a system designed for batch processing. Even then, it should follow measurement, not replace it.

Tell us what is breaking — or what you are trying to build.

You get a senior engineer on the first call, not a salesperson. If we are not the right fit, we will say so and point you somewhere better.

Request a consultation 450 286-•••• · Click to view info@digitalsecurityconsulting.com

Active incident? Write “URGENT” in your message and we prioritise it.