Fix N+1 Database Query Patterns in ORM Code
This prompt identifies N+1 query issues in your code, provides the optimized eager-loading syntax, and calculates the reduction in database round-trips.
Prompt
Act as a Senior Backend Engineer. Your task is to audit code for N+1 query patterns. Wait for me to paste my ORM code snippet and the associated model relationships. Once I provide the code, follow these rules: 1. Identify the exact line causing the N+1 issue. 2. Provide the corrected code using eager loading or batching syntax for my specific framework. 3. Calculate the number of queries executed in the current version versus the optimized version, assuming a list of 50 items. 4. Explain why the change improves performance without using jargon. Output format: ## Analysis of N+1 Pattern. ## Optimized Code Implementation. ## Query Count Comparison (Before vs After). ## Performance Impact Summary. This analysis is for architectural guidance and does not replace load testing in your staging environment.Example output
Tips and common edits
Why this works
- Forces the model to explicitly calculate query counts to prove the benefit.
- Requires specific framework-based syntax for immediate application.
- Includes a clear disclaimer about the need for actual load testing.
Use this prompt when you notice high latency in API endpoints that fetch related data. Paste your controller or service layer code along with the model definitions. Supported in ChatGPT, Claude, and Gemini.
Common edits
- Add your specific database engine if you need dialect-specific optimizations.
- Request the output as a Pull Request description if you are documenting the change for a team.
Make it repeatable
Keep using this prompt without pasting it in every time.
Prompt details
- By
- Prompts-GPT editors
- Published
Did this prompt work?
Keep exploring
More like this
Design Cursor-Based Pagination Logic for High-Volume APIs
Get a complete implementation plan for cursor-based pagination including SQL queries, cursor encoding logic, and strategies for handling record deletions.
Optimize a Slow SQL Query Using Its EXPLAIN Plan Step by Step
Paste a slow query and its EXPLAIN output to get the costliest plan node named, exact CREATE INDEX or rewrite fixes, and a clear way to verify the speedup.
Debug SQL query errors and incorrect row counts
Find syntax errors, incorrect joins, and silent null filter bugs in database queries. Use it when a query returns syntax errors or unexpected row counts.
Find and Fix Data Quality Issues with Ready-to-Run SQL Checks
Paste your schema and get SQL queries that catch duplicates, NULLs, orphaned foreign keys and out-of-range values, each with what a bad result means and a fix.
Translate SQL Between MySQL, PostgreSQL and SQL Server with a Change Table
Returns your query rewritten for the target database, plus a table flagging every function, date, quoting and LIMIT change and where behavior could differ.
Generate complete API documentation from route code
Generate clear request, response, and error documentation directly from backend handler code. Use it to prepare API docs for frontend developers.
- Design a Normalized Database Schema from a Plain Feature Description
- Explain a Complex SQL Query in Plain English, Step by Step
- Write SQL Window Function Queries for Running Totals, Ranks and Period Changes
- Speed Up a Slow Function with Big-O Analysis and a Benchmark
- Turn a Feature Description into a REST API Endpoint Spec
- SQL Query from Schema and Question