Django / Editorial solution guide

A Django list issues a query for every row

Measure and repair repeated relationship queries while preserving filtering, result correctness and bounded memory.

About 3 min readContent reviewed

Read-only knowledge base ยท No public comments

Recognize the symptoms

A list endpoint becomes slower as the number of displayed objects grows. Database tracing shows a similar relationship lookup repeated for each row. A small development fixture can hide the pattern, and a high query count alone does not reveal whether the database or another layer dominates latency.

Understand the likely causes

  • A view, serializer or template may access an unloaded related object in a loop. Filtering a related manager differently from its prefetch can also trigger another query. Large joins, expensive individual queries and external requests can cause similar symptoms and require separate evidence.

1. Reproduce and measure

Use an isolated database with several synthetic parent rows and varied related records. Capture the endpoint query count, query shapes, response time and result IDs for small and larger fixtures. Identify where the repeated relationship is accessed, including template rendering. Exclude credentials and user content from captured SQL diagnostics. Compare the timing with non-database work before concluding that query count is the only bottleneck.

2. Load the required relationships

For a single-valued foreign key or one-to-one relationship, consider select_related on the queryset. For a collection, consider prefetch_related and choose only the relationships the page actually needs. Apply ownership filters before returning data. Match the prefetch to the access pattern: a later differently filtered manager query may not use the prefetched result. Avoid loading an unbounded collection merely to reduce the query count.

3. Verify results and resource use

Repeat the same request with the same fixture and compare response content, ownership boundaries, query count, elapsed time and memory. Use assertNumQueries around a deliberately scoped operation when its expected query count is stable for the fixture. An exact count is not a substitute for checking the returned records, and may change legitimately when middleware or database behavior changes. Inspect a database plan when a remaining query is slow.

4. Retain a representative regression

Keep a test with multiple parent rows and distinct related objects, so a per-row query cannot hide behind a single-record fixture. Assert the intended result as well as the measured query budget, and document which database and request boundary the measurement covers. Review new template and serializer relationship access during changes; remeasure production-like data volume before assuming a prefetch improves the overall workload.

Verify the fix

Compare the original and repaired endpoint using identical synthetic data. Confirm correct authorized results, bounded queries and acceptable measured memory and latency; investigate any remaining slow query separately.

Keep it from recurring

  • Keep a representative multi-row fixture and a scoped query-count regression. Reassess relationship loading when the page, filtering or data volume changes.

Official references