Think Build Implement Repeat
London, UK +44 7367 067226
WhatsApp FOLLOW f in X
PHP Development

Why the Application Is Slow

Last updated:

It is the database, not PHP

When a business application is slow, PHP execution time is rarely the cause. The time is spent waiting for database queries, and usually for far more of them than anyone realised.

We have seen a single page issuing four hundred queries because a loop fetched related records one at a time. No amount of PHP optimisation fixes that.

The three usual causes

  1. Query multiplication — one query per row in a loop, instead of one query total
  2. Missing indexes on columns used for filtering or joining
  3. Over-fetching — selecting everything when three columns are needed

Find them before fixing them

  • Log query count and total time per request
  • Identify the slowest pages by real user timing, not by impression
  • Look at the query log for repeated similar queries
  • Use the database's own explain facility on the slow ones

Optimising without measuring produces a lot of work on the wrong queries, which is the most common way performance projects waste money.

Indexes are usually the quick win

A missing index on a foreign key or a frequently filtered column can turn a query from milliseconds into seconds. Adding it is a one-line change with an immediate effect.

Index the columns you filter, join and sort by. Do not index everything — each index costs on writes.

Caching comes after

Do firstThen consider
Fix query multiplicationQuery result caching
Add missing indexesObject caching
Fetch only needed columnsFull page caching
Paginate large resultsA read replica

Caching a badly-written query hides the problem rather than solving it, and the problem reappears the moment the cache misses.

Frequently asked questions

How many queries should a page make?

Usually under twenty for a typical business page. Hundreds indicates query multiplication in a loop.

Will more server resources fix it?

Temporarily and expensively. A query problem scales with data volume, so hardware buys time rather than a solution.

How do we find missing indexes?

The database's slow query log plus its explain output. Both are free and both point directly at the problem.

Is an ORM the cause?

Not inherently — lazy loading in loops is, and that is usually avoidable by loading relations explicitly.

Keep reading

Application slow and getting slower?

Count the queries on your slowest page first. It is usually the whole answer.

Book a free 30-minute call Get a project estimate WhatsApp us

Related services

What we build for problems like this one

Web DevelopmentCustom Software Development