Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Optimize Django ORM query loading by fixing N+1 queries, duplicate related-object queries, over-fetching, serializer/template query loops, and inappropriate select_related, prefetch_related, only, or defer usage. Use when a Django page, API, admin view, template, serializer, or queryset issues too many queries or loads more model data than needed.
.claude/skills/hashgraph-online-django-orm-query-optimization/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-02 | ✗→✓ | ▲ Improved | 0% | 0% |
| case-03 | ✓→✓ | = Same ✓ | 7% | 0% |
| case-04 | ✓→✓ | = Same ✓ | -4% | 0% |
| case-07 | ✓→✓ | = Same ✓ | -12% | 0% |
| case-08 | ✓→✓ | = Same ✓ | -11% | 0% |
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.
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.items, do not later call items.filter(...) and expect the prefetch cache to help.iterator() with prefetch_related(), provide chunk_size.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.
Other measured skills in the registry, with their headline benchmark lift.