20 of the best prompts for batching database queries efficiently, step by step across 4 stages. Works with ChatGPT, Claude, and Gemini.
20 of the best prompts for batching database queries efficiently, step by step across 4 stages. Works with ChatGPT, Claude, and Gemini.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
AI Prompts for Batch Database Query Optimization
The tool is generating database access code that runs queries inside a loop, leading to inefficient N+1 query problems.
See promptsAI Prompts for Batch Database Query Optimization
The AI is generating database access code that runs queries inside a loop, leading to inefficient N+1 query problems.
See promptsAI Prompts for Batching Database Queries Efficiently
Claude Code sometimes generates database access code that runs queries inside a loop, leading to inefficient N+1 query problems.
See prompts