Apps & Software
Slow App Because of the Database: Signs and How to Trace It
A slowing app is often not a server that is too weak, but a few wasteful database queries. This guide covers the signs, how to find those queries, and the order of fixes before adding hardware.
SEO and Content Specialist
5 min read
Short answer: an app that is slow because of the database usually shows certain pages that are slow while others are fast, response time that rises as data grows, and database CPU or disk busy while the application server is relaxed. Trace it by turning on the slow query log, sorting queries by total time, then checking the execution plan with EXPLAIN. The fix that most often works is the right index and tidying repeated queries, before adding servers.
The complaint “the app is slow” rarely points to a cause. It could be the network, the code, the server, or the database, and adding server specifications without knowing the cause often spends budget without changing anything. This guide helps confirm whether the database is really the culprit, and how to trace it step by step. For symptoms of 500 or 502 errors, see tracing 500 and 502 errors in an application.
What are the signs of an app slowed by the database?
Signs that point to the database:
- Slow on specific pages, for example reports or lists with many filters, while other pages are normal.
- Slower over time. Response time rises as tables grow, because a query that is fast on 1,000 rows is not necessarily fast on 5 million.
- Slow at peak hours, when many queries compete for the same resources.
- High database CPU or disk while the application server is relatively idle.
- One page running dozens of similar queries, a pattern known as N+1: one query for the list, then one extra query for each row.
If it is the application server’s CPU that is high, or timeouts happen on the network, the cause may not be the database. Measure first which part consumes the time: the application, the database, or the connection between them.
Steps to trace it
1. Turn on slow query logging
PostgreSQL has a log_min_duration_statement parameter that records every statement running longer than a limit you set. MySQL and MariaDB have a slow query log with a similar function. Start with a fairly loose limit, then tighten it, so the log does not flood.
2. Find the queries that take the most total time
One slightly slow query run thousands of times often costs more than one very slow query that rarely runs. The pg_stat_statements extension in PostgreSQL collects planning and execution statistics per kind of query, so they can be sorted by total time, not only time per run.
3. Read the execution plan
The EXPLAIN command shows how the database will run a query. The PostgreSQL documentation warns that EXPLAIN ANALYZE actually runs the query, so for statements that change data, run it inside a transaction that is then rolled back. What to look for:
| What appears in the plan | What it means | Usual action |
|---|---|---|
| Reading an entire large table | No suitable index for the filter | Add an index on the filter and sort columns |
| Estimated rows far from reality | Table statistics are out of date | Refresh the table statistics |
| A large sort spilling to disk | Sorting without an index on large data | An index aligned with the order, or limit the result |
| The same query repeated dozens of times | An N+1 pattern in application code | Fetch the data at once, not row by row |
4. Fix one at a time and measure again
Change one thing, measure again, then continue. An index speeds up reads but adds write overhead and storage, so not every column needs one. Sources such as Use The Index, Luke explain how indexes work for different kinds of queries.
Example: a monthly report that keeps getting slower
Imagine a monthly report page that used to open in two seconds and now takes tens of seconds. The trace goes like this. The slow query log shows one query that sums transactions per branch and filters by date. EXPLAIN shows the database reading the whole transactions table, because there is no index on the date column. After an index on the date is added and the query is retested, the execution plan switches to reading through the index, and the time drops close to what it was. No server was added.
The example is simplified, but the pattern often repeats: a growing table, a filter the indexes do not serve, and a complaint that appears only once the data is large. That is why monitoring response time per page, not just server status, helps catch slowdowns before users complain. For server-level monitoring, see server monitoring: metrics and thresholds.
When is adding a server worthwhile?
Adding servers or enlarging specifications makes sense after the wasteful queries have been tidied and the load is still high. How to calculate server needs is in calculating the specifications for an internal application server. The economical order: measure, tidy queries and indexes, add a cache for data that rarely changes, then enlarge the hardware.
How Legia handles it
In the Web Application Development service, the data structure and queries are designed together with the application’s needs, and applications already running can be maintained through a maintenance plan. Other discussions about building applications are on the Applications & Software page.
Frequently asked questions
Must the database be separated from the application server?
Not always. For a small load, one server is enough. Separating the database makes sense when the two start competing for CPU, memory, or disk, or when you need different backup and security arrangements.
Are as many indexes as possible always better?
No. Every index must be updated on every write. An index that no query uses only adds load, so create them based on the queries that actually run.
How do you know the fix worked?
Compare the response time and total time of the same queries before and after, at comparable hours and load. A fix that is not measured is only a guess. Also keep a note of which queries were changed and why, so the next change does not repeat the same guess and other teams can trace the history.

