Something takes too long. Unlike an error, there is nothing to point at, and unlike most debugging, intuition about the cause is reliably wrong even among experienced people. The method is measurement rather than reasoning.
Why is guessing so unproductive?
Because the slow part is rarely the complicated part. People optimise the algorithm they were proud of while the actual cost is a query running inside a loop, and the difference between the two is a factor of hundreds.
What should you measure?
Time at a few points before anything else. Where does the request spend its time, roughly, at the level of stages rather than lines. That single measurement usually identifies the area immediately.
Do not suggest optimisations yet. Where would you add timing to find out which part of this is slow, and what would each result tell me?
What are the usual causes?
- Queries in a loop. One per row instead of one for everything.
- A missing index. Which is invisible until the table grows.
- Loading everything. Reading a whole file or table to use part of it.
- Repeated identical work. The same calculation many times.
- Network calls in sequence. Where they could run together.
- Doing it in code. Rather than in the database.
The first two account for a substantial share of everything slow in small applications.
Why is the query loop so common?
Because it is the natural way to write it. Fetch a list, then for each item fetch its details, which is readable and produces a hundred queries where one would do.
Generated code produces this pattern constantly, since it is the clearest expression of the intent.
How do you find it?
Count the queries. Most frameworks can log them, and seeing four hundred queries for one page answers the question without any further investigation.
Why do missing indexes appear suddenly?
Because they are irrelevant at small sizes. A query scanning a thousand rows is instant; the same query on a million is not, and nothing changed except the amount of data.
This is why performance problems appear months after deployment with no code change.
What should you not do?
Optimise before measuring, which produces effort spent on the wrong thing and code that is harder to read. It also frequently makes things slower, since intuition about what is expensive is poor.
How slow is too slow?
Worth deciding before optimising anything. A page taking two seconds may be entirely acceptable for an internal tool and unacceptable for something public, and without a target the work has no stopping point.
Setting a threshold also prevents the common failure of optimising something already fast enough while something genuinely slow goes untouched. The useful question is which operations users actually wait for, since a report that takes thirty seconds once a month matters considerably less than a page that takes two seconds four hundred times a day.
What is the order?
Measure, find the slowest stage, understand why, fix that one thing, measure again. Repeating that a few times resolves most performance problems, and it prevents the common outcome of optimising something that was never the issue.