Thinking in SQL with Django
summary
Profiling uses Django's Explain, queryset .query, django-debug-toolbar and django-extensions shell_plus --print-sql to spot an N+1 on pizzas and toppings; fix one adds prefetch_related and assertNumQueries tests to keep gains. Instead of Python loops, model toppings with numeric ratings and compute vegan/vegetarian via annotate + Case/When and a Subquery with OuterRef against the through table to force inner joins; result removes a costly hash aggregate, drops CPU load, and fixes responsiveness with a one-line change and no raw SQL.
AI-written summary · about
