Loading...
Loading...
Fixes N+1 queries, redundant fetches, over-fetching with keep/discard verification
npx skill4agent add levnikolaevich/claude-code-skills ln-812-query-optimizerPaths: File paths (,shared/,references/) are relative to skills repo root. If not found at CWD, locate this SKILL.md directory and go up one level for repo root.../ln-*
| Aspect | Details |
|---|---|
| Input | Audit findings from |
| Output | Optimized queries, verification report |
| Companion | ln-651-query-efficiency-auditor (finds issues) → ln-812 (fixes them) |
| Check | Required | Action if Missing |
|---|---|---|
| Audit findings OR target file | Yes | Block optimization |
| Test infrastructure | Yes | Block (need tests for verification) |
| Git clean state | Yes | Block (need clean baseline for revert) |
shared/references/ci_tool_detection.mdshared/references/git_worktree_fallback.mddocs/project/persistence_audit.md| Finding Type | Optimization |
|---|---|
| N+1 query | Batch loading / eager loading / |
| Redundant fetch | Pass object instead of ID, cache result |
| Over-fetching | Select specific fields / projection / |
| Missing index hint | Add index annotation / migration |
| Unbounded query | Add |
| Priority | Criteria |
|---|---|
| 1 (highest) | N+1 in hot path (called per request) |
| 2 | Redundant fetches (same entity loaded multiple times) |
| 3 | Over-fetching (SELECT * where few columns needed) |
| 4 | Missing pagination on user-facing endpoints |
FOR each finding (F1..FN):
1. APPLY: Edit query code (surgical change)
2. VERIFY: Run tests
IF tests FAIL → DISCARD (revert) → next finding
3. VERIFY: Tests PASS → KEEP
4. LOG: Record fix for report| Condition | Decision |
|---|---|
| No tests cover affected file/function | SKIP finding — log as "uncovered, skipped" |
| Tests pass | KEEP |
| Tests fail | DISCARD + log failure reason |
| Fix introduces new N+1 | DISCARD |
| Field | Description |
|---|---|
| source | Audit report path or target file |
| findings_total | Total findings from audit |
| fixes_applied | Successfully kept fixes |
| fixes_discarded | Failed fixes with reasons |
| fix_details[] | Per-fix: finding type, file, before/after description |
Options:
# Source
audit_report: "docs/project/persistence_audit.md"
target_file: "" # Alternative to audit report
# Verification
run_tests: true
# Scope
fix_types: # Filter which types to fix
- n_plus_one
- redundant_fetch
- over_fetching
- unbounded_query| Error | Cause | Solution |
|---|---|---|
| No audit findings | ln-651 not run or no issues | Report "no findings to optimize" |
| ORM-specific syntax | Unknown ORM | Query Context7/Ref for ORM docs |
| Migration needed | Index addition requires migration | Log as manual step, skip |
../ln-651-query-efficiency-auditor/SKILL.mdshared/references/ci_tool_detection.md