Debugging & Incident Diagnosis
Compatible with Cursor, Claude Code, Cline, Roo Code, Antigravity

API Performance Bottleneck & N+1 Query Optimization

Diagnose slow database queries, serialization bottlenecks, and memory churn in sluggish API endpoints.

PerformanceSQLNode.jsJavaBackendAPM
Skill Objective

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.

1

Decompose Latency Budget

Analyze APM traces to split latency between DB queries, network I/O, serialization, and CPU computation.

2

Eliminate N+1 Queries with Batching

Replace iterative query loops with aggregated SQL JOINs, DataLoader batching, or IN clauses.

3

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.

📊 Bottleneck Breakdown: Query count and latency analysis.
🛠️ Optimized Query & Code: Refactored batch-loading endpoint code.

Domain Verification Checks

Query Count O(1): Reduces total queries executed per request to a constant count.
Selective Column Projections: Avoids SELECT * on wide production tables.
Connection Pool Sizing: Prevents DB pool starvation under high concurrent load.

Example Agent Invocations

"Optimize this organization project listing endpoint which is firing 120 SQL queries per request."
"Refactor this slow GraphQL resolver using DataLoader batching."

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.
Complementary Agent Prompt

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.

View Prompt

SprintKit Workflow Integrations

Complementary tools and checklists to pair with this agent workflow.

Related Agent Skills

Explore related procedural workflows in this discipline.

All Skills

Relational Database Schema Normalization & Indexing Strategy

Design production PostgreSQL schemas with composite indexes, foreign key strategies, and partitioning.

PostgreSQLSQLDatabase+2
3 workflow stepsView skill

Production Exception & Stack Trace Root-Cause Analysis

Systematic investigation workflow for diagnosing production exceptions and constructing verified patches.

DebuggingObservabilityLogs+2
4 workflow stepsView skill

Spring Boot Service & JPA Code Review

Rigorous code review workflow for Spring Boot services examining transactions, JPA queries, concurrency, and validation.

JavaSpring BootJPA+3
5 workflow stepsView skill