
AI can generate endpoints quickly, but telemetry reveals what your system is actually doing. A real-world look at the N+1 query problem.
Originally published at: https://www.aquilabdullah.com/your-post-url
I recently noticed something strange in the backend telemetry of a code base that I was working on.
A single API request was triggering more than twenty database calls.
The code looked perfectly reasonable, but the telemetry told a very different story.
Imagine you're building a simple profile endpoint.
You ask your AI assistant to create something that returns:
A reasonable implementation might look like this:
user = get_user(user_id)
sports = get_user_sports(user_id)
posts = get_user_posts(user_id)
events = get_user_events(user_id)
return {
"user": user,
"sports": sports,
"posts": posts,
"events": events
}
At first glance, this looks great.
Each function is small.
Each responsibility is clear.
The code is readable and easy to test.
From the perspective of local code correctness, this is good code.
But from the perspective of system behavior, something subtle may have just happened.
If each of those helper functions hits the database, this endpoint just turned into multiple queries.
Instead of one database call, we now have several.
This pattern is known as the N+1 query problem.
It usually appears when you:
For example:
get_users()
for each user:
get_posts(user)
If you load 10 users, that becomes 11 queries.
If you load 100 users, that becomes 101 queries.
Each individual query is fast.
But together they create unnecessary load and extra round trips.
What started as clean, modular code quietly turns into a query fan-out pattern.
It took me a minute to realize what I was looking at.
The endpoint didn’t look suspicious, but the telemetry did.
During a single request, I saw repeated database calls like this:
21:15:40 GET /sports
21:15:40 GET /users
21:15:40 GET /event_rsvps
21:15:41 GET /sports
21:15:41 GET /users
21:15:41 GET /event_rsvps
The same resources being requested over and over again.
The code looked clean.
But the system was doing far more work than I expected.
AI coding tools are very good at generating locally correct code.
They optimize for:
But they don’t automatically reason about:
So you end up with code that looks right, but behaves differently than you expect at runtime.
Once you notice an N+1 pattern, the solution is usually to move more work into the database.
Common approaches include:
In this case, I used a database RPC function.
Instead of making multiple application-level calls, the database assembles the full result in a single operation.
Conceptually:
Before:
API → many database calls
After:
API → single RPC → database assembles result
This reduces round trips and makes the endpoint behavior predictable.
What struck me most about this bug was that the code itself looked perfectly reasonable.
Nothing obviously inefficient.
But telemetry told a different story.
That’s the shift that comes with AI-assisted development.
We can generate systems faster than ever.
But speed makes it easier to miss how those systems behave under the hood.
Telemetry gives you visibility into:
Without it, you're relying on what the code suggests.
With it, you can see what the system is actually doing.
Before the fix:
Request → ~20 database queries
After moving the logic into an RPC function:
Request → 1 database call
Same endpoint.
Very different behavior.
AI can generate endpoints quickly.
Telemetry tells you what those endpoints are actually doing.
gemmaI ported the whole Gemma-4 family — E2B, E4B, 12B, 31B, and the 26B-A4B MoE — to run on...
communityHey DEV, I'm Tobore. Let's actually connect. I've been on here for a while now, mostly writing and...
ai(yep, kinda clickbait, just for the funsies 😊) At the beginning of the year, I relaunched my...
aiMy laptop was sitting idle with the fan at full tilt. Nothing was running that I knew of. The culprit...
githubactionsI Built a Thing! TL;DR — Google Gemini-based Pull Request reviews and Issue Triaging for...
aiI've been hearing the word "harness" thrown around a lot lately. I assumed it just meant "the IDE" or...
Workflows from the Neura Market marketplace related to this DeepSeek resource