The Deska blog
Hunting N+1 Queries With an Agent
Learn how to optimize ORM performance and use an AI agent for hunting N+1 queries in your codebase to eliminate redundant database roundtrips.
· 11 min read
The N+1 query problem remains one of the most common performance bottlenecks in modern web applications. It occurs when an application makes one initial database query to fetch a list of objects and then executes additional queries for each individual object to retrieve related data. This pattern effectively turns a single operation into dozens or hundreds of sequential network roundtrips. In this guide, we will explore the mechanics of this issue and look at how specialized workflows, including hunting N+1 queries with an agent, can automate the detection and remediation of these leaks.
Anatomy of an N+1 Query
Most Object Relational Mappers (ORMs) provide a high level of abstraction that makes it easy to forget you are interacting with a relational database. Consider a scenario where you are building a dashboard to display a list of projects and the name of the owner for each. In many ORMs, the default behavior is "lazy loading," where the related owner object is only fetched when the specific property is accessed.
If you have 50 projects, your code might fetch the list once. When the loop starts, it performs a separate query for each owner. You end up with 1 + 50 queries. While this works perfectly in development with a local database and three records, it becomes a major latency source in production. The overhead is rarely the data itself. The cost lies in the cumulative network latency of multiple trips between the application server and the database engine.
Traditional Identification Methods
Before automating the process with AI, developers typically rely on several manual or semi-automated strategies to find these bottlenecks.
Database Logging and Profiling
The most direct way to see N+1 problems is to watch the raw SQL logs. If you see a sequence of nearly identical SELECT statements differing only by an ID, you have found the issue. Tools like the Django Debug Toolbar or Laravel Telescope provide a visual representation of these queries directly in the browser.
Static Analysis and Linters
Some ecosystem specific tools can flag potential N+1 issues by analyzing the source code. For example, Bullet in the Ruby on Rails community or various specialized linters for SQLAlchemy. These tools are excellent because they catch issues during the development cycle, but they can sometimes produce false positives or miss complex interactions where the data access happens deep within nested utility functions.
APM Monitoring
Application Performance Monitoring (APM) tools help identify these problems in staging or production environments. They look for patterns of repetitive queries within a single request trace. While useful for discovery, they often alert you only after the code has been written and deployed.
Hunting N+1 Queries With an Agent
The introduction of AI coding agents has changed the remediation workflow. Instead of manually tracing every relationship through your models and controllers, you can deploy an agent to analyze the execution flow. Hunting N+1 queries with an agent allows for a cross-referencing of codebase structure and actual query output.
In a traditional setup, you might have a terminal open to tail logs and a code editor to find the offending line. Within Deska, this process becomes more integrated. You can use the infinite canvas to place your code editor, a terminal running your server, and an AI agent panel side by side.
The agent can observe the logs coming through the terminal panel and match the SQL patterns to the source code it sees in the editor. By running coding agents such as Claude Code or Codex CLI within Deska, you can ask the agent to specifically monitor for N+1 patterns during a specific user action. The agent can then suggest the exact "eager loading" syntax required for your specific ORM, whether that is include in Prisma, with in Eloquent, or select_related in Django.
Strategies for Remediation
Once the N+1 issue is identified, the solution usually falls into three categories.
- Eager Loading: Explicitly telling the ORM to join the related tables or fetch all related IDs in a second, single query.
- Batching: Collecting IDs throughout the request and executing a single bulk fetch at the end.
- Database Views: If the relationship logic is complex, move it into a database view to keep the application code simple.
Selecting the right strategy depends on the volume of data. Eager loading a relationship with millions of rows can be just as dangerous as an N+1 query. An agent can help simulate these scales by generating test data or looking at the schema to suggest if a JOIN or a subquery is more appropriate.
The Local Development Advantage
Privacy and speed are paramount when dealing with database schemas and representative data. Using a local-first approach ensures that your code and query logs do not leave your machine. When you use tools like Deska, your files and sessions remain local.
You can manage your own API keys for the AI models, which means the intelligence is applied directly to your local context without a third party hosting your source code. Keeping the agent close to the runtime environment allows it to see the terminals where the database is running, making the feedback loop almost instantaneous.
Comparison of Detection Approaches
| Method | Speed | Accuracy | Setup Overhead |
|---|---|---|---|
| SQL Log Tailng | Fast | High | Low |
| APM Tools | Slow | High | High |
| Static Linters | Instant | Medium | Low |
| AI Coding Agents | Medium | High | Medium |
While agents are not always the fastest for simple one line fixes, they excel at architectural changes. They can refactor complex services to pass pre-loaded data down the call stack, which is often where manual fixing becomes tedious and error prone.
Monitoring on the Go
Sometimes a performance regression is reported when you are away from your desk. Using the mobile app allows you to check in on long running diagnostic sessions. If you have left an agent running a suite of performance tests to find N+1 leaks, you can monitor the progress and even run simple voice commands to stop a process if it begins to consume too many resources.
FAQ
How to detect N+1 queries in production?
Detection in production is best handled by APM tools that group similar queries. You should look for traces where the number of database calls scales linearly with the number of items returned in the main response. If the query count fluctuates based on user data, it is a sign of a missing eager load.
Can AI agents fix ORM performance issues automatically?
Agents are very effective at identifying the patterns and suggesting the specific ORM methods needed to fix them. However, developers should always review the suggested change because eager loading too much data can lead to memory exhaustion. Using an agent as a pair programmer to find the location and provide the fix is the safest workflow.
Is eager loading always better than lazy loading?
No, it is a trade-off. Lazy loading is useful when you only need a related object in a small percentage of cases. Eager loading is the correct choice when you know you will access the relationship for every item in a list. The goal is to avoid the loop-based query pattern while minimizing unnecessary data transfer.
Optimize Your Workflow
Finding and fixing database bottlenecks should not be a manual chore. By combining traditional SQL profiling with modern AI tools, you can maintain high performance standards without sacrificing development speed. If you want to try hunting N+1 queries with an agent in a flexible, local environment, you can download the app and get started today.