mcpField
L
ln-812-query-optimizer

ln-812-query-optimizer

github.com/levnikolaevichData & databases58435 installsUpdated 2d ago
Repository

About ln-812-query-optimizer

A query-optimization skill for code maintenance, using audit findings or a target file to identify inefficient queries. It tests each change before keeping or reverting it, then produces a fix report.

Key features

  1. 01Load audit findings or scan a target file
  2. 02Prioritize work by impact
  3. 03Fix N+1, redundant fetches, and over-fetching
  4. 04Keep or revert changes based on tests
  5. 05Report fixes and failure reasons

Use cases

  1. 01Address findings from a query-efficiency audit
  2. 02Optimize N+1 queries on request hot paths
  3. 03Reduce redundant fetches or unnecessary fields

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/levnikolaevich/claude-code-skills --skill ln-812-query-optimizer

After installing, type/ln-812-query-optimizerin Claude Code to confirm it loaded.

Details

From the project README

Paths: File paths (shared/, references/, ../ln-*) 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-812-query-optimizer

Type: L3 Worker Category: 8XX Optimization Parent: ln-810-performance-optimization-coordinator

Fixes query efficiency issues found by ln-651-query-efficiency-auditor. Each fix verified via tests with keep/discard pattern. Metric: query count reduction (not runtime benchmark).


Overview

AspectDetails
InputAudit findings from docs/project/persistence_audit.md (ln-651 section) OR target file
OutputOptimized queries, verification report
Companionln-651-query-efficiency-auditor (finds issues) → ln-812 (fixes them)

Workflow

Phases: Pre-flight → Load Findings → Prioritize → Fix Loop → Report


Phase 0: Pre-flight Checks

CheckRequiredAction if Missing
Audit findings OR target fileYesBlock optimization
Test infrastructureYesBlock (need tests for verification)
Git clean stateYesBlock (need clean baseline for revert)

MANDATORY READ: Load shared/references/ci_tool_detection.md — use Test Frameworks section for test detection.

Worktree & Branch Isolation

MANDATORY READ: Load shared/references/git_worktree_fallback.md — use ln-812 row.


Phase 1: Load Findings

From Audit Report

Read docs/project/persistence_audit.md, extract ln-651 findings:

Finding TypeOptimization
N+1 queryBatch loading / eager loading / .Include() / prefetch_related
Redundant fetchPass object instead of ID, cache result
Over-fetchingSelect specific fields / projection / .Select()
Missing index hintAdd index annotation / migration
Unbounded queryAdd .Take() / LIMIT / pagination
From Target File

If no audit report: scan target file for query patterns matching the table above.


Phase 2: Prioritize Fixes

PriorityCriteria
1 (highest)N+1 in hot path (called per request)
2Redundant fetches (same entity loaded multiple times)
3Over-fetching (SELECT * where few columns needed)
4Missing pagination on user-facing endpoints

Phase 3: Fix Loop (Keep/Discard)

Per-Fix Cycle
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
Keep/Discard Decision
ConditionDecision
No tests cover affected file/functionSKIP finding — log as "uncovered, skipped"
Tests passKEEP
Tests failDISCARD + log failure reason
Fix introduces new N+1DISCARD

Note: No benchmark needed — query optimization metric is correctness (tests pass) + structural improvement (fewer queries). The audit already identified the inefficiency.


Phase 4: Report Results

Report Schema
FieldDescription
sourceAudit report path or target file
findings_totalTotal findings from audit
fixes_appliedSuccessfully kept fixes
fixes_discardedFailed fixes with reasons
fix_details[]Per-fix: finding type, file, before/after description

Configuration

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 Handling

ErrorCauseSolution
No audit findingsln-651 not run or no issuesReport "no findings to optimize"
ORM-specific syntaxUnknown ORMQuery Context7/Ref for ORM docs
Migration neededIndex addition requires migrationLog as manual step, skip

References

  • ../ln-651-query-efficiency-auditor/SKILL.md (companion: finds issues)
  • shared/references/ci_tool_detection.md (test detection)

Definition of Done

  • Findings loaded from audit report or target file scan
  • Fixes prioritized (N+1 first, then redundant, over-fetch, unbounded)
  • Each fix applied with keep/discard: tests pass → keep, tests fail → discard
  • No new query inefficiencies introduced by fixes
  • Report returned with findings total, fixes applied, fixes discarded

Version: 1.0.0 Last Updated: 2026-03-08

Comments

Markdown supported