Skip to content
Preptima
hardScenarioConceptMidSeniorStaff

A list endpoint that returns fifty orders issues more than a hundred queries, and rewriting it as an async endpoint made it slower. Explain both.

5 min readUpdated 2026-08-15

Answer

Short answer

An N+1 happens when the ORM follows a relation lazily once per row; select_related fixes forward foreign keys with a SQL join, prefetch_related issues one extra query per relation and joins in Python.

Was this answer helpful?

Likely follow-ups

  • When would prefetch_related be slower than the N+1 it replaces?
  • How would you prefetch only the unshipped items of each order into a separate attribute?
  • What happens if you call the Django ORM synchronously from inside an async view?
  • How would you detect a blocking call inside an event loop in a running service?

Related questions

Further reading

Related Topics