ln-812-query-optimizer
Fixes N+1 queries, redundant fetches, over-fetching with keep/discard verification
- License 许可
- MIT
- 最近更新
- 2026-05-02
- Stars
- ★ 3
- Forks
- 1
01 前言 Front Matter
- name
- ln-812-query-optimizer
- license
- MIT
- description
- Fixes N+1 queries, redundant fetches, over-fetching with keep/discard verification
03 SKILL.md 正文
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
| Aspect | Details |
|---|---|
| Input | Audit findings from docs/project/persistence_audit.md (ln-651 section) OR target file |
| Output | Optimized queries, verification report |
| Companion | ln-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
| 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) |
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 Type | Optimization |
|---|---|
| N+1 query | Batch loading / eager loading / .Include() / prefetch_related |
| Redundant fetch | Pass object instead of ID, cache result |
| Over-fetching | Select specific fields / projection / .Select() |
| Missing index hint | Add index annotation / migration |
| Unbounded query | Add .Take() / LIMIT / pagination |
From Target File
If no audit report: scan target file for query patterns matching the table above.
Phase 2: Prioritize Fixes
| 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 |
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
| 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 |
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
| 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 |
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
| 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 |
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
04 同仓库更多 Skills
zarr-python
Chunked N-D arrays for cloud storage. Compressed arrays, parallel I/O, S3/GCS integration, NumPy/Dask/Xarray compatible, for large-scale scientific computing pipelines.
zabbix
Zabbix monitoring system automation via API and Python. Use when: (1) Managing hosts, templates, items, triggers, or host groups, (2) Automating monitoring configuration, (3) Sending data via Zabbix trapper/sender, (4) Querying historical data or events, (5) Bulk operations on Zabbix objects, (6) Maintenance window management, (7) User/permission management
x-twitter-growth
X/Twitter growth engine for building audience, crafting viral content, and analyzing engagement. Use when the user wants to grow on X/Twitter, write tweets or threads, analyze their X profile, research competitors on X, plan a posting strategy, or optimize engagement. Complements social-content (generic multi-platform) with X-specific depth: algorithm mechanics, thread engineering, reply strategy, profile optimization, and competitive intelligence via web search.
what-if-oracle
Run structured What-If scenario analysis with multi-branch possibility exploration. Use this skill when the user asks speculative questions like "what if...", "what would happen if...", "what are the possibilities", "explore scenarios", "scenario analysis", "possibility space", "what could go wrong", "best case / worst case", "risk analysis", "contingency planning", "strategic options", or any question about uncertain futures. Also trigger when the user faces a fork-in-the-road decision, wants to stress-test an idea, or needs to think through consequences before committing.
vercel-react-best-practices
React and Next.js performance optimization guidelines from Vercel Engineering. This skill should be used when writing, reviewing, or refactoring React/Next.js code to ensure optimal performance patterns. Triggers on tasks involving React components, Next.js pages, data fetching, bundle optimization, or performance improvements.
venue-templates
Access comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.