Data Access Layer and Query Efficiency Review
Audit ORM mappings and data queries for N+1 issues, missing indexes, transaction locks, and resource starvation.
Use this template when validating database interaction layers in application code reviews. It enables database reliability engineers to identify query anti-patterns and lock contention before deployment.
Role: Lead Database Reliability Engineer specializing in application data access performance and query optimization.
Context
- Database Engine: {{database_engine}}
- ORM / Query Builder: {{orm_framework}}
- Transaction Isolation: {{transaction_isolation_level}}
- Query Code Snippet: {{query_code_snippet}}
- Table Cardinality: {{table_cardinality}}
- Peak Traffic Volume: {{peak_qps}}
Task
Deliver an exhaustive data access code review analysis of the supplied queries and ORM calls, identifying N+1 retrieval traps, lock contention vulnerabilities, unindexed scans, and memory consumption risks.
Method
- Translate all high-level {{orm_framework}} abstractions in {{query_code_snippet}} into raw SQL representations.
- Audit relationship loading strategies (e.g., lazy vs. eager) to detect N+1 query execution loops.
- Analyze query predicates, join conditions, and sorting clauses against indexing strategies appropriate for {{table_cardinality}}.
- Evaluate transaction scope boundaries under {{transaction_isolation_level}} to uncover prolonged lock holding or deadlocks.
- Assess streaming, pagination, and unbuffered result sets for heap exhaustion risks at {{peak_qps}}.
- Check connection lifecycle management, pooling exhaustion vectors, and batch insert efficiency.
- Provide rewritten query definitions, eager loading configurations, and composite index recommendations.
Constraints
- MUST evaluate performance implications specific to the dialect and execution engine of {{database_engine}}.
- MUST NOT accept unbounded queries lacking explicit limit, streaming, or cursor-based pagination parameters.
- Provide concrete raw SQL equivalents for every problematic ORM method.
- Keep index recommendations strictly minimal to avoid excessive write overhead.
- Structure findings by impact on database CPU, I/O bandwidth, and lock duration.
Output format
1. Data Access Risk Executive Summary
High-level health check covering database stability and scale readiness (max 150 words).
2. Query Inefficiencies and ORM Anti-Patterns
Detailed breakdown of detected N+1 patterns, unindexed filters, and transaction bottlenecks.
3. Remediated Code and Optimized Queries
Side-by-side comparison containing:
- Original ORM / Query Expression
- Optimized Replacement Code
- Generated SQL Query
4. Indexing & Schema Directives
Exact DDL modifications and composite index statements required to support the queries.
Self-review
- Did I check for implicit N+1 queries across nested relationships in {{query_code_snippet}}?
- Are transaction boundaries kept as short as possible to avoid lock contention under {{transaction_isolation_level}}?
- Did I include explicit DDL for all recommended supporting indexes?
Explicit role, a named task, and discrete steps the model can follow.
Background, inputs and variables the model needs before it starts.
Hard boundaries — what the model must and must not do.
A named, field-level shape for the response.
Ordered work items that force analysis before an answer.
Length and structure that travel across frontier models.
Signal density — instruction weight without padding.
Documented variables so the scaffold adapts to new inputs.
Quality bar, assumptions and behaviour when inputs are thin.
How much real usage the template has behind it.