Claude Code performance

N+1 Query Problem Causing Performance Degradation

Application performance degrades significantly after adding a feature that loads related data. Profiling shows massive number of database queries (1 main query + N queries for each result). Adding 50 users slows down response by seconds instead of milliseconds.

The feature works correctly but is fundamentally inefficient, querying the database hundreds or thousands of times for what should be a few queries.

Error Messages You Might See

Application timeout after adding feature Database connection pool exhausted 50+ SELECT queries logged for single request
Application timeout after adding featureDatabase connection pool exhausted50+ SELECT queries logged for single request

Common Causes

  1. Lazy loading relationship in loop: for (user in users) { user.profile.name } triggers query per user
  2. Separate query for each item instead of batch loading
  3. Eager loading not configured, falling back to lazy loading
  4. No database indexes on foreign keys causing full table scans
  5. JOINs not used despite being available, falling back to multiple queries

How to Fix It

Enable SQL query logging to see all queries. Count them: if 51+ for 50 items, you have N+1. Use eager loading (JOIN FETCH) or explicit batch loading. Add database indexes on foreign keys. Use query analysis tools (EXPLAIN PLAN) to see join strategies. Consider denormalization or caching for expensive relationships.

Real developers can help you.

Richard McSorley Richard McSorley Full-Stack Software Engineer with 8+ years building high-performance applications for enterprise clients. Shipped production systems at Walmart (4,000+ stores), Cigna (20M+ users), and Arkansas Blue Cross. 5 patents in retail/supply chain tech. Currently focused on AI integrations, automation tools, and TypeScript-first architectures. Kingsley Omage Kingsley Omage Fullstack software engineer passionate about AI Agents, blockchain, LLMs. Jared Hasson Jared Hasson Full time lead founding dev at a cyber security saas startup, with 10 yoe and a bachelor's in CS. Building & debugging software products is what I've spent my time on for forever Tejas Chokhawala Tejas Chokhawala Full-stack engineer with 5 years experience building production web apps using React, Next.js and TypeScript. Focused on performance, clean architecture and shipping fast. Experienced with Supabase/Postgres backends, Stripe billing, and building AI-assisted developer tools. Matthew Jordan Matthew Jordan I've been working at a large software company named Kainos for 2 years, and mainly specialise in Platform Engineering. I regularly enjoy working on software products outside of work, and I'm a huge fan of game development using Unity. I personally enjoy Python & C# in my spare time, but I also specialise in multiple different platform-related technologies from my day job. Yovel Cohen Yovel Cohen I got a lot of experience in building Long-horizon AI Agents in production, Backend apps that scale to millions of users and frontend knowledge as well. Bastien Labelle Bastien Labelle Full stack dev w/ 20+ years of experience Rudra Bhikadiya Rudra Bhikadiya I build and fix web apps across Next.js, Node.js, and DBs. Comfortable jumping into messy code, broken APIs, and mysterious bugs. If your project works in theory but not in reality, I help close that gap. Franck Plazanet Franck Plazanet I am a Strategic Engineering Leader with over 8 years of experience building high-availability enterprise systems and scaling high-performing technical teams. My focus is on bridging the gap between complex technology and business growth. Core Expertise: 🚀 Leadership: Managing and coaching teams of 15+ engineers, fostering a culture of accountability and continuous improvement. 🏗️ Architecture: Enterprise Core Systems, Multi-system Integration (ERP/API/ETL), and Core Database Structure. ☁️ Cloud & Scale: AWS Expert; architected systems handling 10B+ monthly requests and managing 100k+ SKUs. 📈 Business Impact: Aligning tech strategy with P&L goals to drive $70k+ in monthly recurring revenue. I thrive on "out-of-the-box" thinking to solve complex technical bottlenecks and am always looking for ways to use automation to improve business productivity. Omar Faruk Omar Faruk As a Product Engineer at Klasio, I contributed to end-to-end product development, focusing on scalability, performance, and user experience. My work spanned building and refining core features, developing dynamic website templates, integrating secure and reliable payment gateways, and optimizing the overall system architecture. I played a key role in creating a scalable and maintainable platform to support educators and learners globally. I'm enthusiastic about embracing new challenges and making meaningful contributions.

You don't need to be technical. Just describe what's wrong and a verified developer will handle the rest.

Get Help

Frequently Asked Questions

How to detect N+1 problems?

Enable SQL query logging. For N items, you should see ~O(1) or O(log N) queries, not O(N). If query count = 1 + N, that's N+1 problem.

How to fix with JOINs?

Use JOIN FETCH in query: SELECT user FROM User user JOIN FETCH user.profile WHERE ... (fetches user and related profile in single query).

Should all relationships be eager loaded?

No. Load eagerly only if always used. For conditional access, use explicit loading or batch loading when needed.

Related Claude Code Issues

Can't fix it yourself?
Real developers can help.

You don't need to be technical. Just describe what's wrong and a verified developer will handle the rest.

Get Help