Skip to main content

Performance for Djangos

Django performance work focuses on reducing database queries and repeated computation. Query optimisation uses select_related and prefetch_related to avoid the N+1 problem, indexes and only() or values() for lighter reads, while caching stores expensive results using backends such as Redis or Memcached at the view, template fragment or low-level API level.

Stage: Data · Part of the Django roadmap · Last reviewed 19 November 2026

What to learn in Performance

Performance interview questions

What is the N+1 query problem and how do you fix it in Django?

It happens when you load a list of objects and then access a related object on each, causing one extra query per row. Use select_related for foreign keys and one-to-one, or prefetch_related for many-to-many and reverse relations, to load them together.

When should you use select_related versus prefetch_related?

select_related performs a SQL join and suits single-valued relations such as ForeignKey. prefetch_related runs a separate query and joins in Python, suiting many-valued relations such as ManyToMany or reverse foreign keys.

Where Performance fits in the Django roadmap

Performance belongs to the Data stage (intermediate level), which takes about 20 hours. ORM and migrations.

Back to the full Django roadmap