20 of the best prompts for batch database query optimization, step by step across 4 stages. Works with ChatGPT, Claude, and Gemini.
20 of the best prompts for batch database query optimization, step by step across 4 stages. Works with ChatGPT, Claude, and Gemini.
Published July 2, 2026
Getting Batch Database Query Optimization right takes more than a single prompt. This 4-stage guide covers Diagnose the Wall, Isolate and Fix, Verify the Resolution, and more, breaking the whole process into focused steps where each prompt builds on the last. The tool is generating database access code that runs queries inside a loop, leading to inefficient N+1 query problems. This can significantly degrade performance and increase load times if not addressed. By following this guide, developers will learn to optimize their database interactions, improving application efficiency and responsiveness. Every prompt is optimized and runs in ChatGPT, Claude, and Gemini.
Look for instances where database queries are executed within a loop in your code. This will often manifest as performance issues or excessive database load.
Quote the Query Generation Logic
Please provide the exact code output generated for the database access in the previous response. Identify the function that contains the loop and explain how it deviates from the expected batch query approach. Include a comparison of the intended batch query logic versus the current implementation, highlighting the specific areas where the approach differs.
Paste the Query Loop Code
Please analyze the following code snippet where database queries are executed in a loop: [CODE SNIPPET]. Explain why this approach is inefficient, highlighting the specific issues that arise from it. Additionally, provide at least two alternative methods to improve the performance of this code, detailing how each method addresses the inefficiencies identified.
State Your Understanding of Batching
Explain the concept of batching database queries instead of executing them in a loop. Discuss the advantages of batching, such as improved performance and reduced load on the database. Provide examples of scenarios where batching is beneficial, and outline any potential pitfalls or misunderstandings that could arise when implementing this approach.
Expected vs. Actual Query Behavior
Provide a structured comparison in a table format that details the expected behavior of batching queries versus the actual behavior of looping queries. For each behavior, include specific examples, performance metrics, and implications on system efficiency. Additionally, summarize the key differences and potential areas for optimization based on the findings.
Identify the Batching Rule Violation
Identify the specific batching rule or best practice regarding database query batching that was violated in the last output. Please quote the rule directly and explain how it was breached. Additionally, provide suggestions on how to adhere to this rule in future queries to improve performance and efficiency.
Target the specific code responsible for executing database queries in a loop. This will allow for a focused fix to implement batching.
Rewrite Loop to Batch Queries
Rewrite the function located at [FILE]:[LINE] that currently executes database queries in a loop. Transform this function to utilize a batched query approach while ensuring that the original logic for data retrieval remains intact. Please provide the updated code along with a brief explanation of how the new approach improves performance and efficiency compared to the original loop method.
Enforce Batching Constraint
Modify the database access code located at [FILE] on line [LINE] to enforce a batching constraint for query execution. Ensure that all queries are collected and executed in a single batch operation to optimize performance and reduce the number of database calls. Provide a brief explanation of how this change improves efficiency and any potential impacts on existing functionality.
Scoped Change to Query Execution
Please modify the code in [FILE] at line [LINE] where the loop is defined. Replace the current loop implementation with a batch processing method while ensuring that the rest of the function remains unchanged. After making the adjustment, provide a brief explanation of how the new method improves performance and any potential impacts on the overall functionality.
Format Query for Batching
Please adjust the database query format in the function located at [FILE] on line [LINE] to support batch execution. Provide the updated query structure that enables processing of multiple records simultaneously. Additionally, explain how this change improves performance and efficiency in handling queries.
Correct Database Dependency for Batching
Examine the database dependencies in [FILE] that could impact the batching of queries. Identify any outdated or incorrect libraries or methods being utilized for this functionality. Provide a detailed report on the necessary updates, including specific changes to be made and the reasons for these adjustments to ensure optimal performance in query batching.
Confirm that the changes made to batch the database queries have resolved the issue. This involves checking performance and output.
Run Performance Test on Batching
Please execute a performance test on the updated database access code to verify the efficiency improvements from the batching implementation. Include specific metrics such as execution time, resource usage, and any observed bottlenecks. Present the results in a structured format, detailing comparisons with previous performance benchmarks and highlighting any significant changes.
Show Diff of Query Changes
Please display the differences in the database access code changes. Highlight the modifications that were made to transition from a loop-based execution to a batch query execution. Provide a clear comparison of the old and new code, indicating which lines were added, removed, or altered, and include comments explaining the rationale behind these changes.
Replay the Original Query Scenario
Replay the original query scenario that led to the N+1 query issue. Confirm whether the new batching implementation effectively resolves the performance problems. Please document your findings in a structured format, including details on the original query, the changes made in the batching implementation, and a summary of the performance improvements observed.
Check Edge Case for Batching
Please verify the batching implementation by testing an edge case where the database returns a large number of records. Ensure that the system handles this scenario correctly without any performance degradation. Provide a detailed report on the results, including any issues encountered, performance metrics, and suggestions for improvements if necessary.
Confirm Output State of Database Queries
Verify the output state of the database queries after implementing batching. Please provide a detailed report that includes the expected results, any discrepancies found, and a summary of errors, if any. Additionally, include a comparison between the output before and after batching to highlight any changes in performance or data integrity.
Establish reusable artefacts that ensure future database query implementations adhere to batching principles.
Batch Query Best Practices Prompt
Please provide best practices for batching database queries to prevent N+1 problems. Include a detailed explanation of how to identify loops in query execution and the steps developers should take to address these issues. Additionally, outline the consequences of not following these practices and suggest methods for alerting developers when a loop is detected. Format your response as a checklist that developers can easily reference.
Database Query Optimization Template
Create a conversation-starter template for discussing database query optimization. Include the following elements: a clear statement on the importance of batching database queries, guidelines on when batching is not feasible, and a request for justification along with alternative approaches that can be taken in such cases. Format this as a structured outline with bullet points for each key topic to facilitate discussion.
Project Spec for Query Batching
Draft a project specification message that outlines the requirement for all database access code to utilize batched queries. Include the rationale behind this decision, emphasizing the benefits of performance and efficiency. Additionally, specify that any exceptions to this rule must be documented thoroughly and receive approval from the lead developer before implementation. Ensure the message is clear and concise, suitable for distribution among the development team.
Naming Convention for Batching
Please establish a naming convention for functions that handle database queries. The suggested format is 'batchQuery_[FUNCTION_NAME]'. Explain the rationale behind this convention, how it aids in identifying functions that adhere to batching principles, and provide examples of how this naming convention can be applied in different scenarios.
Self-Check for Query Batching
Before finalizing any database query code, please conduct a self-check to ensure that all queries are batched. If any queries are not batched, provide a detailed rationale explaining the reasoning behind this approach. Additionally, suggest potential improvements or alternatives that could enhance performance and reduce the risk of N+1 query issues.
The N+1 query problem occurs when database queries are executed in a loop, resulting in multiple individual queries instead of a single batch. This leads to performance issues as each query incurs overhead.
You can identify N+1 queries by monitoring the database logs for excessive query counts during operations that should be batched. Bolt's sandboxed environment may not surface these issues directly, so manual checks are necessary.
N+1 queries can significantly slow down application performance due to the overhead of multiple database connections and query executions. In Bolt, this can lead to timeouts or degraded user experiences.
Bolt executes database queries within a sandboxed container, which can sometimes mask performance issues caused by N+1 queries. Developers need to be vigilant about query patterns to avoid inefficiencies.
If your batching implementation fails, check the query structure and ensure that the database supports batch operations. Bolt may not surface errors clearly, so reviewing logs and outputs is crucial.
AI 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
Cursor generates database access code that runs queries inside loops, leading to inefficient N+1 query problems.
See promptsAI Prompts for Batching Database Queries Efficiently
Running database queries inside a loop can lead to performance issues known as the N+1 query problem.
See prompts