query-performance-safety

Detects and fixes N+1 queries, unbatched IN clauses, and unbounded recursion in backend code.

1.0k|109|Updated Jan 4, 2026
One-click install
npx skills add https://github.com/doccker/cc-use-exp --skill query-performance-safety
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: query-performance-safety
Source: https://github.com/doccker/cc-use-exp/tree/main/.cursor/skills/query-performance-safety
Command: npx skills add https://github.com/doccker/cc-use-exp --skill query-performance-safety

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Backend services often ship with hidden performance traps: N+1 queries inside loops, oversized IN clauses, unbounded BFS traversals, and duplicated heavy queries across nested service calls. These pass local testing but collapse under production data volumes. This Skill gives the AI concrete detection patterns and fixes for these query performance pitfalls.

Core Features & Use Cases

  • N+1 Detection and Repair: Identifies single-record queries inside loops, streams, and DTO converters, then rewrites them using the batch-fetch-then-map-lookup pattern.
  • IN Clause Batching: Enforces a fixed batch size (e.g., 500) for large ID lists and aggregate SUM/COUNT queries to avoid packet limits and plan-cache misses.
  • Bounded Traversal: Adds MAX_TOTAL_NODES limits with truncation logging to BFS/recursive structures like invite trees and org charts.
  • Use Case: While reviewing a Java Spring service that converts 6000 products to DTOs in a loop, the Skill flags the implicit N+1 (4 queries per item) and rewrites it with two-phase processing and preloaded maps, cutting thousands of SQL round trips to a handful.

Quick Start

Review this service method for N+1 queries, unbatched IN clauses, and unbounded recursion, then rewrite the unsafe parts following the query-performance-safety rules.

Frequently Asked Questions about query-performance-safety

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix N+1 queries in Spring Data JPA?

Collect all foreign key IDs first, run one batch query with findAllById or a findByIdIn method, build a Map keyed by ID, then look up values inside the loop instead of querying. Also check DTO converter helpers, which often hide multiple findById calls per item.

How to batch large IN clause queries in SQL?

Split the ID list into fixed-size chunks, commonly 500, execute one IN query per chunk, and merge the results. This avoids MySQL max_allowed_packet failures, Oracle's 1000-parameter limit, and query plan cache misses on very long statements.

Why does my recursive tree query cause out of memory errors?

Limiting depth alone is unsafe because each level can fan out to hundreds of thousands of nodes. Add a MAX_TOTAL_NODES cap alongside the depth limit, log a warning when truncating, and use aggregate SQL instead of loading all nodes for counts or sums.

Does this query safety guidance work with Go GORM or Python SQLAlchemy?

Yes, the references include equivalent patterns for GORM Preload and explicit IN batching, SQLAlchemy joinedload and in_ filters, and Prisma include or batched findMany. The core batch-fetch-then-map principle is identical across all four stacks.

What are the limitations of using @Cacheable for query results?

Cache keys containing Pageable or timestamps rarely hit, and keys without tenantId leak data across tenants. Use stable explicit keys including tenantId, pair writes with @CacheEvict, and skip caching entirely when strong consistency is required.