Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Optimizes Django ORM querysets by fixing N+1 queries, over-fetching, and misused select_related, prefetch_related, only, or defer.
82% 86%+5 ptsof test cases passed
Passed 19 of 22 test cases with the skill, and 18 of 22 without it.
3 of 3 safety checks passed
Not used yet
Use this skill for query-count and object-loading problems. The usual target is an N+1 pattern where each rendered row or serialized object triggers more database work.
Prove the query pattern.
Choose the loading strategy.
select_related() for single-valued relationships: ForeignKey and OneToOneField.prefetch_related() for many-valued relationships: reverse FK and many-to-many.Prefetch() when the related queryset needs filters, ordering, annotations, or nested select_related().values() or values_list() when model instances are unnecessary.only() or defer() only when large columns are truly unused.Keep access consistent.
items, do not later call items.filter(...) and expect the prefetch cache to help.iterator() with prefetch_related(), provide chunk_size.Add a focused regression check when useful.
See relationship-and-column-loading.md for examples and review cues.
prefetch_related() to every queryset without checking memory.only() and then touching deferred fields in a loop.__str__(), model properties, template includes, or admin list methods.Show the query count and duplicate-query pattern before and after, plus any wall-time or memory change.
Skills that do similar work, with their benchmark result.
Next to this one in the catalogue, plus more from the same author.