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

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.

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. Costea Adrian Costea Adrian Embedded Engineer specilizing in perception systems. Latest project was a adas camera calibration system. Matt Butler Matt Butler Software Engineer @ AWS Victor Denisov Victor Denisov Developer 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. BurnHavoc BurnHavoc Been around fixing other peoples code for 20 years. Milan Surelia Milan Surelia Milan Surelia is a Mobile App Developer with 5+ years of experience crafting scalable, cross-platform apps at 7Span and Meticha. At 7Span, he engineers feature-rich Flutter apps with smooth performance and modern UI. As the Co-Founder of Meticha, he builds open-source tools and developer-focused products that solve real-world problems. Expertise: πŸ’‘ Developing cross-platform apps using Flutter, Dart, and Jetpack Compose for Android, iOS, and Web. πŸ–‹οΈ Sharing insights through technical writing, blogging, and open-source contributions. 🀝 Collaborating closely with designers, PMs, and developers to build seamless mobile experiences. Notable Achievements: 🎯 Revamped the Vepaar app into Vepaar Store & CRM with a 2x performance boost and smoother UX. πŸš€ Launched Compose101 β€” a Jetpack Compose starter kit to speed up Android development. 🌟 Open source contributions on Github & StackOverflow for Flutter & Dart πŸŽ–οΈ Worked on improving app performance and user experience with smart solutions. Milan is always happy to connect, work on new ideas, and explore the latest in technology. Jen Jacobsen Jen Jacobsen I’m a Full-Stack Developer with over 10 years of experience building modern web and mobile applications. I enjoy working across the full product lifecycle β€” turning ideas into real, well-built products that are intuitive for users and scalable for businesses. I particularly enjoy building mobile apps, modern web platforms, and solving complex technical problems in a way that keeps systems clean, reliable, and easy to maintain. Sage Fulcher Sage Fulcher Hey I'm Sage! Im a Boston area software engineer who grew up in South Florida. Ive worked at a ton of cool places like a telehealth kidney care startup that took part in a billion dollar merger (Cricket health/Interwell health), a boutique design agency where I got to work on a ton of exciting startups including a photography education app, a collegiate Esports league and more (Philosophie), a data analytics as a service startup in Cambridge (MA) as well as at Phillips and MIT Lincoln Lab where I designed and developed novel network security visualizations and analytics. I've been writing code and furiously devoted to using computers to make people’s lives easier for about 17 years. My degree is in making computers make pretty lights and sounds. Outside of work I love hip hop, the Celtics, professional wrestling, magic the gathering, photography, drumming, and guitars (both making and playing them) MFox MFox Full-stack professional senior engineer (15+years). Extensive experience in software development, qa, and IP networking.

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