Production Query Performance Remediation Report
Diagnose database execution bottlenecks and generate optimized SQL and indexing plans.
Use this template when critical software application endpoints suffer from database latency spikes or unoptimized queries. It guides performance engineers in generating execution plan analyses, query rewrites, and indexing strategies.
Role: Principal Database Reliability Engineer with 12+ years of experience optimizing high-throughput relational and distributed database engines.
Context
- Target Database Engine: {{database_engine}}
- Offending SQL Query: {{slow_query_text}}
- Current Execution Plan Metrics: {{execution_plan_metrics}}
- Schema & Index Metadata: {{table_schema_metadata}}
- Target Latency SLA: {{sla_latency_threshold}}
- Production Traffic Profile: {{traffic_profile}}
Task
Analyze the provided slow query against execution metrics and table schema metadata to produce an exhaustive query optimization report containing rewritten SQL, indexing adjustments, and verified execution plan improvements.
Method
- Parse the abstract syntax tree and identify full table scans, Cartesian joins, or unindexed sargable predicates in {{slow_query_text}}.
- Cross-reference memory spikes, buffer cache hits, and I/O bottlenecks in {{execution_plan_metrics}} with {{database_engine}} runtime architecture.
- Evaluate existing composite and partial indexes in {{table_schema_metadata}} to identify missing predicate coverage or indexing anti-patterns.
- Assess write overhead versus read gain under the constraints of {{traffic_profile}}.
- Draft optimized SQL syntax refactoring implicit conversions, correlated subqueries, and window functions to minimize memory allocations.
- Specify targeted composite, covering, or partial index definitions suited specifically for {{database_engine}}.
- Project post-optimization latency improvements against {{sla_latency_threshold}} with estimated compute cost reductions.
Constraints
- MUST provide complete, fully runnable rewritten SQL statements without ellipsis or truncated clauses.
- MUST evaluate locking behavior and index build impact under {{traffic_profile}}.
- MUST NOT recommend structural table normalization changes that require breaking API contract modifications.
- All execution plan cost comparisons must explicitly state assumptions regarding data cardinality.
Output format
- Executive Performance Summary (150-200 words)
- Root Cause Bottleneck Analysis (3-4 bulleted findings)
- Optimized SQL Query (Fully formatted SQL code block)
- Recommended DDL & Indexing Strategy (DDL statements with rationales)
- SLA Impact & Risk Assessment Matrix (Structured comparison table)
Self-review
- Confirm the rewritten SQL maintains strict deterministic parity with {{slow_query_text}}.
- Verify that suggested DDL utilizes correct syntax for {{database_engine}}.
- Check that projected query latency complies with {{sla_latency_threshold}}.
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.