Think Build Implement Repeat
London, UK +44 7367 067226
WhatsApp FOLLOW f in X
  1. Home
  2. Blog
  3. Why the Application Slows Down
Python & Django

Why the Application Slows Down

Fixing slow Django queries: lazy loading in loops, loading relations explicitly, counting queries per page, indexing filtered fields and pagination.

Updated 2 min readBy SpiderHunts Technologies

Free estimateNo obligation

Get a free estimate

Tell us what you need. A senior engineer reads every enquiry.

Takes under a minute. We never share your details.

  • Free consultation
  • No commitment
  • NDA on request

Prefer to talk? Book a free 30-minute call →

Quick answer — TL;DR

Lazy loading in loops is the cause in the large majority of cases. Load relations explicitly, count queries per page, and index what you filter on.

Lazy loading is the usual culprit

Fetching a list of records and then accessing a related object for each one issues a query per record. On twenty rows nobody notices; on a thousand the page takes twenty seconds.

We have seen a single Django page issuing over four hundred queries because a template accessed a related object inside a loop. No amount of caching fixes that properly.

Load relations explicitly

  • Select related objects in the same query for one-to-one and foreign keys
  • Prefetch for many-to-many and reverse relations
  • Only fetch the fields you need on large models
  • Watch out for relations accessed in templates, which are easy to miss

Count queries as a test

  1. Log query count per request during development
  2. Set a threshold that fails a test if exceeded
  3. Check every page that renders a list
  4. Re-check after any template change

A test that asserts a page issues no more than a set number of queries catches the regression that would otherwise appear months later as unexplained slowness.

Index what you filter on

IndexWhen
Foreign keysUsually created, but check
Filtered fieldsAny field in a common filter
Ordering fieldsAnything you sort large lists by
Composite indexesCommon multi-field filters
Nothing elseEach index costs on writes

Paginate everything

A view that loads every record works fine in development with two hundred rows and fails in production with two hundred thousand. Paginate lists from the start.

The same applies to exports — stream them or process in batches rather than building the whole result in memory.

FAQ

Frequently asked questions

The questions readers ask us after this guide.

Still have a question?

Ask us directly — a senior engineer will get back to you.

Ask about your project

How do we find the slow pages?

Real user timing plus per-request query logging. Both point directly at the problem.

Is the ORM the cause?

Not inherently. Lazy loading in loops is, and it is avoidable by loading relations explicitly.

When should we drop to raw SQL?

For complex reporting queries the ORM expresses awkwardly. For ordinary work the ORM is fine and more maintainable.

Does caching help?

After fixing the queries. Caching a query problem defers it rather than solving it.

Keep reading

More on Python & Django

Python & Django

An API Other Systems Can Depend On

Designing a Python API service others can depend on: validation at the boundary, consistent errors and status codes, early versioning and documentation.

Python & Django

Moving and Transforming Data Reliably

Building data pipelines in Python that cope with malformed input: restartable stages, quarantining failures, reconciling counts and alerting on absence.

Start here

Pages that take ten seconds to load?

Count the queries first. It is nearly always the whole answer.

  1. You tell us what you needTwo minutes on the form, or a message on WhatsApp.
  2. A senior engineer reviews itAnd comes back with questions, a realistic range and an honest view on fit.
  3. Free 30-minute scoping callWe talk through scope, options and a realistic estimate — with no obligation.
Free estimateNo obligation

Talk to someone who builds this

Send a short brief and we will come back with an honest view and a realistic range.

Takes under a minute. We never share your details.

  • Free consultation
  • No commitment
  • NDA on request

Prefer to talk? Book a free 30-minute call →