cc-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 cc-query-performance-safety
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: cc-query-performance-safety
Source: https://github.com/doccker/cc-use-exp/tree/main/.codex/skills/cc-query-performance-safety
Command: npx skills add https://github.com/doccker/cc-use-exp --skill cc-query-performance-safety

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Backend code often hides severe performance traps: loops that fire one SQL query per element (N+1), IN clauses with thousands of parameters, recursive traversals without node limits, and nested service calls that repeat the same heavy query. These issues pass local testing but degrade or crash production systems under real data volumes.

Core Features & Use Cases

  • N+1 Detection and Remediation: Identifies single-record queries inside loops, streams, and DTO conversion helpers, then rewrites them using the batch-fetch-to-Map pattern with paired repository methods like findByIdIn.
  • IN Clause Batching: Enforces a fixed IN_BATCH_SIZE of 500 for large ID lists, including batched SUM/COUNT aggregations, to avoid packet limits and plan-cache misses.
  • Bounded Traversal and Query Reuse: Adds MAX_TOTAL_NODES caps with truncation logging to BFS/recursive walks, and consolidates nested service calls so one BFS result feeds count, sum, and list operations.
  • Use Case: While reviewing a team-detail endpoint that calls countMembers, sumPoints, and listMembers separately, the Skill flags three duplicate BFS traversals and refactors them into a single shared descendant collection.

Quick Start

Review this service method for N+1 queries, unbatched IN clauses, and unbounded recursion, then rewrite it using batch fetching and Map lookups.

Frequently Asked Questions about cc-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 findByIdIn or findAllById, build a Map keyed by ID, then look up values inside the loop instead of calling findById. This reduces N queries to a constant number regardless of list size.

How to batch large IN clauses in SQL queries?

Split the ID list into chunks of 500 using a fixed IN_BATCH_SIZE constant, query each batch separately, and merge the results. This avoids MySQL max_allowed_packet failures, Oracle's 1000-parameter limit, and query plan cache misses.

Why is my DTO conversion causing thousands of SQL queries?

Helper methods like convertToDTO often hide multiple findById calls internally, so invoking them in a loop multiplies queries by list size. Use two-phase processing: filter with entities first, batch-preload associations, then convert using preloaded Maps.

Does limiting BFS depth prevent memory issues?

No, depth limits alone are insufficient because each level can fan out to hundreds of thousands of nodes. Add a MAX_TOTAL_NODES hard cap around 5000, log a warning on truncation, and document the maximum returned node count in the API contract.

What cache key mistakes cause multi-tenant data leaks?

Cache keys that omit tenantId allow one tenant to read another tenant's cached data, and keys containing Pageable objects rarely hit. Build keys from stable fields like tenantId plus the entity ID, and pair writes with @CacheEvict.