Django ORM Query Optimization
Side-by-side comparison of unoptimized and optimized Django ORM queries. Each panel shows exactly how many database queries ran and how long they took.
The unoptimized versions suffer from the N+1 query problem.
The optimized versions use select_related, prefetch_related,
and annotate to slash query counts.
Books
Publisher, authors, genres. Demonstrates select_related, prefetch_related, and annotate together — the most dramatic optimization in the app.
Reviews
ForeignKey traversal on User and Book. Compare raw queries against select_related.
Reading Lists
Nested prefetch across three models. The deepest N+1 cascade in the app.
Borrow Records
FK traversal with a computed is_overdue property. See select_related shine.
Users
Counting reviews per user. The simplest N+1 pattern, fixed with one annotate.