skills/queryset-optimizer/SKILL.md
Optimize Django ORM performance by detecting N+1 query patterns, missing `select_related`/`prefetch_related`, and likely index gaps. Run targeted static scans, optional runtime query capture, and produce a prioritized remediation plan with expected query-count impact. Use when users ask to speed up Django endpoints, reduce database hits, investigate slow views/serializers, or audit QuerySet efficiency before release.
npx skillsauth add ragnarok22/agent-skills queryset-optimizerInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Audit Django query performance with deterministic checks and evidence-backed recommendations.
manage.py).Collect at least one measurable baseline for the target before changing code.
Open Django shell:
uv run manage.py shell 2>/dev/null || python manage.py shell
Then capture query count for one representative code path:
from django.db import connection
from django.test.utils import CaptureQueriesContext
with CaptureQueriesContext(connection) as ctx:
# Execute the target path, for example evaluating the target queryset.
list(qs)
print(f"queries={len(ctx.captured_queries)}")
If runtime capture is not feasible, continue with static analysis and mark runtime validation as not evaluated.
Read references/antipatterns.md for rule IDs, severity, search commands, and fix patterns.
For each rule:
Start from 100 and deduct points per confirmed finding:
| Severity | Deduction | | -------- | --------- | | High | -8 | | Medium | -5 | | Low | -2 |
Rules:
0.+5 bonus (max 100) when before/after query counts are measured for at least one hotspot.Output a markdown report with this structure:
## QuerySet Optimizer Report
**Efficiency Score: XX / 100** [GRADE]
Grade: A (90-100), B (80-89), C (70-79), D (60-69), F (<60)
Audit root: `<path>`
Target: `<endpoint/view/task or full-scan>`
### Baseline Evidence
- Runtime query capture: [AVAILABLE/NOT AVAILABLE]
- Representative query count(s): [before values or N/A]
### Findings
#### High
| ID | Location | Issue | Fix | Expected Impact |
|----|----------|-------|-----|-----------------|
| ... | ... | ... | ... | ... |
#### Medium
...
#### Low
...
### Not Evaluated
- [RULE_ID] Reason not evaluated.
### Top Actions
1. ...
2. ...
3. ...
### Verification Plan
- [ ] Re-run representative path and compare query count.
- [ ] Confirm no behavior regressions (ordering, permissions, pagination).
- [ ] Add regression test when hotspot is business-critical.
If a severity bucket has no findings, omit that section.
If the user asks for fixes:
development
Create Git commit messages that conform to Conventional Commits 1.0.0, including type/scope/description format, optional body, trailer-style footers, and explicit BREAKING CHANGE signaling. Use when users ask to draft commit messages, commit current changes, rewrite a commit message into conventional format, or enforce conventional commit standards in a repo.
development
Audit Python codebases for security, performance, correctness, and architecture antipatterns. Run optional trusted runtime checks (syntax, tests, lint, typing) plus static rule scans, then output a 0-100 health score with actionable fixes. Use when users ask to inspect a Python project, run a Python health check, review backend code quality, or perform a pre-release audit.
development
Verify Dockerfiles and Docker Compose manifests for security issues, reliability risks, optimization opportunities, syntax errors, and misconfiguration before builds or deploys. Run deterministic checks (`scripts/verify-docker.sh`, `docker compose config -q`, optional `hadolint`) and produce a 0-100 health score with prioritized fixes. Use when users ask to validate Dockerfile(s), docker-compose/compose YAML files, harden container configuration, optimize image/runtime setup, debug configuration failures, or run a pre-deploy Docker audit.
development
Audit Django codebases for security, performance, correctness, and architecture antipatterns. Run system checks, migration drift checks, and static rule scans, then output a 0-100 health score with actionable fixes. Use when users ask to scan a Django backend, run a Django health check, review backend code quality, or perform a pre-deploy audit.