Prefetching for Fun and Profit
summary
Shows that Django prefetching is extensible: implement a manager/descriptor with get_prefetch_query_set returning a six-tuple and Django will batch related lookups via prefetch_related_objects. Demonstrates concrete fixes: prefetch across separate databases and non‑FK joins, conditional language‑filtered translations, and batching HTTP calls to eliminate an N+1 (example: three queries instead of 100). Provides code patterns, tests them on models and an external API, and packages reusable helpers as Django prefetch utils.
AI-written summary · about
chapters (14)
- 0:00Start
- 1:24Overview
- 3:37What do I want you to get out of this talk?
- 5:25What is the N+1 query problem?
- 7:38An example from Django REST Framework
- 10:33Specifying Prefetches
- 11:38QuerySet evaluation
- 14:14What is a prefetcher?
- 18:08prefetch one level
- 30:01Accessing the prefetched values
- 32:34Multiple Databases
- 38:27Equal Fields
- 40:50Database String Translations
- 44:14API Requests
related talks
26mA Related Matter: Optimizing your webapp
29mMaking smarter queries with advanced ORM resources
27mFinding and fixing an N+1 problem in the Django docs
43mOptimize Python & Django apps with Postgres superpowers
47mHigh Performance Django at Ten: Old Tricks & New Picks
17mOff the Beaten Path: Exploring Unusual Roads to Django Performance
37mPerformant Django
23mDjango with PostgreSQL superpowers
