N+1 Problem in Django Rest Framework
summary
Shows that N+1 appears in Django Rest Framework both in view querysets and serializer fields and that using select_related and prefetch_related actually fixes it. Uses an author/book/tag example API, detects problems with nplusone and Django Debug Toolbar, and measures dramatic improvements — e.g. authors endpoint drops from about 197 queries to 7, books from ~53 to 5 — demonstrating the change is real and effective in practice.
AI-written summary · about
chapters (13)
- 0:00Introduction
- 2:14Django Rest Framework
- 4:00N+1 problem in ORMs
- 8:18Example models
- 10:24N+1 in DRF views
- 12:58Serializer-level N+1
- 14:45Tools to detect N+1
- 18:42Solving N+1 with select_related
- 21:19Solving N+1 with prefetch_related
- 23:50DRF recommendations
- 24:54Performance impact
- 27:03References
- 28:09Q&A
related talks
27mFinding and fixing an N+1 problem in the Django docs
26mA Related Matter: Optimizing your webapp
27mDjango REST Framework: Taking your API to the next level
33mDjango REST Framework: Moving Past the Tutorial to Production
49mPrefetching for Fun and Profit
42mA Nice Problem to Have: Django Under Heavy Load
17mOff the Beaten Path: Exploring Unusual Roads to Django Performance
38mEager Loading for ActiveRecord Performance
