API Performance Bottleneck & N+1 Query Optimization
Diagnose slow database queries, serialization bottlenecks, and memory churn in sluggish API endpoints.
Eliminate N+1 database queries, batch data loading, optimize SQL joins, and streamline serialization in high-latency API endpoints.
Execution Workflow Procedure
Ordered steps the coding agent executes when performing this skill.
Decompose Latency Budget
Analyze APM traces to split latency between DB queries, network I/O, serialization, and CPU computation.
Eliminate N+1 Queries with Batching
Replace iterative query loops with aggregated SQL JOINs, DataLoader batching, or IN clauses.
Trim Heavy Payload Serialization
Select only required columns in queries to minimize memory footprint during JSON serialization.
Agent Safety Guardrails & Anti-Hallucination Rules
- DO NOT add caching over a broken O(N) query without fixing the underlying query efficiency first.
- DO NOT pull large BLOB or JSONB columns into application memory if only metadata is needed.
Expected Output Format
Batch query refactoring and benchmark comparison.
Domain Verification Checks
Example Agent Invocations
Install / Configure This Skill
# Place in .cursorrules or create .cursor/rules/api-performance-optimization.mdc # Trigger: Whenever you perform api performance bottleneck & n+1 query optimization --- name: api-performance-optimization description: Eliminate N+1 query loops, optimize SQL joins, and streamline JSON serialization in slow API endpoints. version: 1.1.0 compatibility: [Cursor, Claude Code, Cline, Roo Code, Antigravity] --- # API Performance Bottleneck & N+1 Query Optimization ## Objective Convert O(N) iterative database queries into high-performance O(1) batch loads and minimize serialization overhead. ## Workflow 1. **Trace Analysis**: Identify N+1 query patterns and slow I/O calls from APM logs. 2. **Query Batching**: Replace loops with single aggregated SQL queries or DataLoader patterns. 3. **Payload Optimization**: Project only required columns and apply caching strategies.
Need a quick one-time prompt for chat?
Try the API Performance Regression & N+1 Query Debugger — a lightweight version designed for 1-click copying directly into Claude, ChatGPT, or Cursor chat.
SprintKit Workflow Integrations
Complementary tools and checklists to pair with this agent workflow.
Related Agent Skills
Explore related procedural workflows in this discipline.
Relational Database Schema Normalization & Indexing Strategy
Design production PostgreSQL schemas with composite indexes, foreign key strategies, and partitioning.
Production Exception & Stack Trace Root-Cause Analysis
Systematic investigation workflow for diagnosing production exceptions and constructing verified patches.
Spring Boot Service & JPA Code Review
Rigorous code review workflow for Spring Boot services examining transactions, JPA queries, concurrency, and validation.