Skip to main content

Claude/ChatGPT Prompt to Detect N+1 Queries and Fix Eager Loading

Audit a query log for N+1 patterns and get an ORM-specific eager-loading fix for each, ranked by real per-request impact instead of generic tuning tips.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt

                                

What this prompt does

This prompt casts the AI as a senior backend engineer and makes it hunt for N+1 query patterns in a real request log, returning ORM-specific fixes instead of generic performance tips. You provide [orm], [framework], and the request log via [query_log]. It identifies each parent query and its repeated child query, counts repetitions per request and per minute of traffic, gives the exact eager-loading fix, shows before/after code, estimates the query-count reduction, and ranks every finding by impact.

The structure works because it ranks fixes by real per-request impact rather than treating every N+1 equally. By multiplying requests per minute against the extra queries each pattern fires, the prompt surfaces where the traffic actually hurts. [orm] determines the exact fix syntax — eager-load clauses, joins, or prefetch differ across ORMs — and [framework] provides the surrounding context for the call-site code. Feeding a real [query_log] rather than a hunch means the fixes land where the load really is.

When to use it

  • A list page silently fires hundreds of queries because a relationship loads in a loop.
  • You inherited an app and want to find its worst N+1 offenders from real traffic.
  • You want ORM-specific eager-loading code, not a generic "use eager loading" reminder.
  • You need to prioritise which N+1 to fix first by actual traffic impact.
  • You want before/after code for the call site, ready to drop in.
  • You are wary of over-eager-loading and want only the relationships the view renders.

Example output

You get a ranked table of findings — each row naming the parent query, the repeated child query, the repetition count, and the impact score — followed by the fixed code for the top offenders, with before/after snippets in your ORM's syntax and an estimate of the query-count reduction per request.

Pro tips

  • Feed a real [query_log] from one slow request, not a synthetic example — the ranking is only as good as the traffic it sees.
  • Set [orm] precisely, since the fix syntax for eager loading differs sharply between ORMs and a near-match will not paste cleanly.
  • Use the impact ranking to fix the top offenders first instead of chasing every N+1 at once.
  • Avoid eager-loading everything blindly; pull only the relationships the view renders, or you trade N+1 for one giant over-fetching join.
  • Compare the estimated query-count reduction against a fresh log after the fix to confirm it actually landed, since the estimate is a projection, not a measurement.
  • Watch for relationships loading inside a loop in the call-site code; that pattern is the classic source of N+1, and the before/after snippet should make it obvious.
  • If a finding's repetition count looks low, deprioritise it — a rare N+1 on a low-traffic page rarely justifies the change, and your attention is better spent on the top of the ranked table.

Frequently Asked Questions

What kind of log should I paste into the prompt?
Paste the full query log for one slow request into `[query_log]`. The model needs to see the actual parent and repeated child queries to identify N+1 patterns, so a real log from a slow page works far better than a hand-written summary.
Does it give fixes for my specific ORM?
Yes, set `[orm]` to your ORM and it returns the matching eager-loading syntax, whether that is eager-load clauses, joins, or prefetch. The fixes are written for that ORM, so naming it exactly is what makes the before/after code paste-ready.
Why does it rank the findings instead of just listing them?
Ranking by impact, requests per minute times extra queries, tells you which N+1 actually hurts. A pattern that fires on a rarely-visited page matters less than one on a hot list view, so the ranking helps you fix the high-traffic offenders first.
Can eager loading make things worse?
Yes, if you over-do it. Eager-loading relationships the view never renders trades N+1 for one bloated query that over-fetches data. The prompt's advice is to load only what the view shows, so review each fix against what the page actually displays.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Data & SQL Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support