---
name: queryset-optimizer-97028a365568
description: 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.
---

# QuerySet Optimizer

Audit Django query performance with deterministic checks and evidence-backed recommendations.

## Workflow

### Step 1: Set scope and baseline target

1. Locate the Django backend root (directory containing `manage.py`).
2. Define the optimization target:
   - endpoint or view
   - serializer
   - background task
   - repeated ORM hotspot from profiling data
3. If target is unknown, scan all app directories and prioritize read-heavy paths first (list endpoints, feed pages, reports).

### Step 2: Capture runtime query evidence (preferred)

Collect at least one measurable baseline for the target before changing code.

Open Django shell:

```bash
uv run manage.py shell 2>/dev/null || python manage.py shell
```

Then capture query count for one representative code path:

```python
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.

### Step 3: Run static scan

Read [references/antipatterns.md](references/antipatterns.md) for rule IDs, severity, search commands, and fix patterns.

For each rule:

1. Run the suggested search command.
2. Manually validate each candidate.
3. Exclude false positives (tests, migrations, fixtures, one-off scripts) unless explicitly relevant.
4. Record confirmed findings with:
   - ID
   - Severity
   - File and line number
   - Evidence (1-2 lines)
   - Fix recommendation
   - Expected impact (query count, memory, latency, or lock time)

### Step 4: Score

Start from 100 and deduct points per confirmed finding:

| Severity | Deduction |
| -------- | --------- |
| High     | -8        |
| Medium   | -5        |
| Low      | -2        |

Rules:

- Floor score at `0`.
- Cap duplicate deductions for each rule ID to 3 findings.
- Deduct only for confirmed findings.
- Add `+5` bonus (max `100`) when before/after query counts are measured for at least one hotspot.

### Step 5: Report

Output a markdown report with this structure:

```text
## 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.

### Step 6: Optional remediation loop

If the user asks for fixes:

1. Fix highest-impact findings first.
2. Re-measure query counts or latency for the same path.
3. Re-run Steps 3-5.
4. Report score delta and residual risks.


---
Source attribution: ragnarok22
Original project: https://github.com/ragnarok22/agent-skills/blob/a7a679d509a3a36a9c174aedbe4a459403a729a0/skills/queryset-optimizer/SKILL.md
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

MIT License

Copyright (c) 2026 Reinier Hernández

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Mega AI Hub resource metadata

Creator / publisher: ragnarok22
Original project: https://github.com/ragnarok22/agent-skills/blob/a7a679d509a3a36a9c174aedbe4a459403a729a0/skills/queryset-optimizer/SKILL.md
Attribution basis: Source repository record

Fields of work: Data & Analytics, Development & Engineering, Product Management
Roles: Backend Developer, Data Analyst, Product Manager
Specialties: Backend
Tools: Django
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: d87bc2f460c3aa105e256cd7dbd5a372f9eed58331d231a81366329b36842f9d
