AI Prompts for Batching Database Queries Efficiently

20 of the best prompts for batching database queries efficiently, step by step across 4 stages. Works with ChatGPT, Claude, and Gemini.

AI Prompts for Batching Database Queries Efficiently

20 of the best prompts for batching database queries efficiently, step by step across 4 stages. Works with ChatGPT, Claude, and Gemini.

Scroll to explore

Published June 28, 2026

When GitHub Copilot generates code that runs database queries inside a loop, it can lead to performance issues known as the N+1 query problem. This results in slow application performance and increased load on the database. These prompts help developers identify and correct the issue by batching queries, improving efficiency and performance. Built across 4 distinct stages covering Diagnose the Wall, Isolate and Fix, Verify the Resolution and more, this guide gives you one expert prompt per step so you never have to write from scratch or guess what the AI needs. The prompts work in ChatGPT, Claude, and Gemini and are designed to get usable output on the first try.

Diagnose the Wall

Identify if the code is executing database queries inside a loop, causing N+1 query issues.

Check Loop Queries

Please review the following code snippet for database queries inside loops: [PASTE CODE]. Identify all queries that are executed within a loop and provide a detailed analysis of their presence. Explain the potential performance implications of executing these queries in a loop, and suggest any optimizations or best practices that could be applied to improve efficiency. Additionally, summarize your findings in a clear format, highlighting the specific lines of code where these queries occur.

Diagnose the Wall

Identify Query Patterns

Please analyze the following code: [PASTE CODE] and identify any patterns of repeated database queries that are executed multiple times within loops. For each identified query, provide details on the specific loop it resides in, the frequency of its execution, and any potential performance implications. Additionally, suggest optimizations or refactoring strategies to reduce the number of repeated queries and improve overall efficiency.

Diagnose the Wall

Spot Inefficient Queries

Examine the following code for potential inefficiencies in database access patterns: [PASTE CODE]. Identify and highlight any queries that may be causing performance issues, and provide a brief explanation for each identified query regarding why it is considered inefficient. Additionally, suggest possible optimizations or alternatives for these queries to improve overall performance. Format your findings in a clear and structured manner, with each query and its corresponding analysis presented separately.

Diagnose the Wall

Locate N+1 Queries

Inspect the following code for any N+1 query patterns: [PASTE CODE]. Identify if there are any queries being executed within a loop that could potentially be optimized through batching. Provide a detailed analysis of the identified patterns, including specific examples of where the N+1 queries occur, and suggest how these queries can be refactored to improve performance. Additionally, explain the impact of these changes on overall application efficiency and database load.

Diagnose the Wall

Analyze Query Execution

Analyze the execution of database queries in the following code: [PASTE CODE]. Identify any instances where queries are being executed within loops, and explain the potential performance implications of this practice. Additionally, suggest alternative approaches to optimize the query execution, such as batching or restructuring the code to minimize database calls. Provide a summary of your findings and recommendations in a clear and concise format.

Diagnose the Wall

Isolate and Fix

Focus on refactoring the code to batch database queries instead of executing them inside loops.

Refactor Loop Queries

Please refactor the following code to optimize database queries by batching them instead of executing within loops: [PASTE CODE]. Focus on improving efficiency and reducing the number of database calls. Explain the changes made and how they enhance performance. Additionally, provide comments in the code to clarify the logic and any assumptions made during the refactoring process.

Isolate and Fix

Batch Queries Together

Please modify the following code to batch the database queries identified in the loop: [PASTE CODE]. Ensure that the batching is efficient and explain the logic behind your approach. Additionally, provide a code example that demonstrates how the batch processing works, including any necessary adjustments to the database connection or query execution methods. The final output should be clear and well-commented to help understand the changes made.

Isolate and Fix

Optimize Query Execution

Analyze the following code snippet for database access: [PASTE CODE]. Identify opportunities to optimize query execution by batching queries effectively. Explain the changes you would make to reduce the number of queries executed, and provide a revised version of the code that implements these optimizations. Additionally, describe the expected performance improvements and any potential trade-offs associated with your solution.

Isolate and Fix

Combine Loop Queries

Please optimize the following database queries executed in a loop by combining them into a single batch operation: [PASTE CODE]. Ensure that the new code maintains the same functionality and performance while reducing the number of database calls. Present the optimized code clearly, highlighting the changes made and explaining how the new approach improves efficiency.

Isolate and Fix

Improve Query Efficiency

Please analyze the following code snippet for database queries executed within loops: [PASTE CODE]. Identify areas where query efficiency can be improved by batching the queries instead of executing them individually. Refactor the code to implement these improvements and provide the updated version, along with a brief explanation of the changes made and how they enhance performance.

Isolate and Fix

Verify the Resolution

Ensure that the refactored code no longer executes queries inside loops and performs efficiently.

Confirm Query Batching

Please review the following refactored code: [PASTE CODE]. I need you to verify that the code correctly batches database queries rather than executing them within loops. Additionally, check for the presence of N+1 queries and confirm that they are absent. Provide a detailed explanation of your findings, including any specific lines of code that demonstrate the batching approach and any potential improvements if necessary.

Verify the Resolution

Check Performance Improvement

Please assess the performance of the refactored code provided below to ensure it no longer suffers from N+1 query issues. The code is: [PASTE CODE]. Analyze the execution time, number of queries executed, and any other relevant performance metrics. Additionally, compare these metrics to the previous version of the code to highlight improvements or regressions. Summarize your findings in a clear report format.

Verify the Resolution

Validate Query Optimization

Please validate the optimization of the following code: [PASTE CODE]. Analyze whether the queries have been successfully batched and assess the overall efficiency improvements. Provide a detailed report on the optimization results, including any metrics or benchmarks that indicate the level of improvement achieved. If there are areas for further enhancement, please outline those as well.

Verify the Resolution

Ensure Efficient Execution

Please review the following code snippet: [PASTE CODE]. Ensure that the refactored code executes database queries efficiently without using loops. Specifically, confirm that the N+1 problem has been resolved by analyzing the query execution plan and identifying any remaining performance issues. Provide a summary of your findings, including any recommendations for further optimization if necessary.

Verify the Resolution

Verify Code Refactoring

Please review the following refactored code for proper batching of database queries: [PASTE CODE]. Ensure that the code effectively eliminates any inefficient query execution patterns. Provide feedback on whether the refactoring improves performance and adheres to best practices for database interactions. Additionally, highlight any areas that may still require optimization or further improvement.

Verify the Resolution

Prevent Recurrence

Create reusable artefacts to prevent future occurrences of N+1 query problems.

Create Batching Rule

Draft a CLAUDE.md rule to enforce query batching based on the recent issue identified. The rule should state: 'Always batch database queries instead of executing them inside loops.' Include a brief explanation of why this rule is important for performance optimization and provide an example of how to implement this rule in code. Ensure the final output is formatted clearly for easy integration into the existing documentation.

Prevent Recurrence

Add Code Comment

Please add a comment in the following code snippet to remind developers about the importance of batching queries to prevent N+1 problems. The comment should read: 'Ensure queries are batched to avoid N+1 problems.' Indicate the exact line where this comment should be placed within the code. Additionally, provide a brief explanation of why batching queries is crucial for performance optimization.

Prevent Recurrence

Develop Checklist Item

Please create a detailed checklist item for code reviews focusing on database query optimization. The item should state: 'Verify that no database queries are executed inside loops.' Additionally, provide a brief explanation of why this practice is important for performance and scalability. Include examples of potential issues that can arise from executing queries in loops, and suggest best practices for avoiding this problem in future code. Format the response as a concise checklist entry that can be easily integrated into a larger code review checklist.

Prevent Recurrence

Establish Naming Convention

Propose a comprehensive naming convention for functions that handle batch queries in a programming context. The convention should include the use of a specific prefix, such as 'batch', to clearly indicate the function's purpose. Additionally, provide a detailed example of a function name that follows this convention, explaining how it reflects the function's functionality and enhances code readability. Include any additional guidelines that should be considered when naming these functions to ensure consistency and clarity across the codebase.

Prevent Recurrence

Design Project Guard

Help me design a project guard that detects N+1 query patterns in code. The guard should implement a linter rule that flags any queries that are executed inside loops, as this can lead to performance issues. Please provide a detailed description of how the guard will function, including the types of queries it will flag, the criteria for detection, and any recommended best practices for developers to follow to avoid these patterns. Additionally, outline how the guard can be integrated into a development workflow and any potential limitations it may have.

Prevent Recurrence

Frequently asked questions

How do I stop GitHub Copilot from generating N+1 queries?+

GitHub Copilot generates code based on patterns it has learned. To avoid N+1 queries, guide it by providing context or examples of batched queries, and use prompts to correct any inefficient patterns.

Why does GitHub Copilot suggest queries inside loops?+

GitHub Copilot suggests code based on common patterns it has seen. If it suggests queries inside loops, it might be due to similar examples in its training data. Use prompts to guide it towards more efficient patterns.

Can I train GitHub Copilot to avoid N+1 problems?+

You cannot directly train GitHub Copilot, but you can influence its suggestions by providing context and examples of efficient code. Use prompts to correct and guide its output.

What should I do if Copilot keeps suggesting inefficient code?+

If GitHub Copilot suggests inefficient code, provide feedback through prompts to correct it. You can also adjust the context or provide examples to guide its suggestions.

How can I ensure Copilot generates efficient database code?+

To ensure efficient database code, provide GitHub Copilot with examples of optimized patterns and use prompts to correct any inefficient suggestions. Regularly review and refactor the generated code.