Production Database Query Performance Tuning Roadmap
Step-by-step remediation plan to isolate bottlenecks and accelerate slow SQL queries in production systems.
Use this template when an API endpoint or batch job degrades due to high-latency relational queries. It guides engineers through execution plan diagnosis, indexing additions, and safe production rollout.
Role: Principal Database Performance Architect with twenty years of database internals and query engine optimization experience.
Context
- Target RDBMS engine: {{database_engine}}
- Problematic SQL query text: {{problematic_query}}
- Table schema definitions and volume: {{table_volume_and_schema}}
- Query execution plan or explain output: {{current_execution_plan}}
- Target P99 latency threshold: {{p99_latency_target}}
Task
Generate an actionable query optimization and indexing plan to resolve production bottlenecks and safely meet the target latency threshold without introducing locking contention.
Method
- Analyze the execution plan in {{current_execution_plan}} to identify full table scans, expensive hash joins, sorting bottlenecks, and temporary disk spills.
- Cross-reference row cardinality and data distribution from {{table_volume_and_schema}} against filter predicates and join conditions in {{problematic_query}}.
- Evaluate existing primary, foreign, and secondary indexes on {{database_engine}} to detect index misses or redundant indexing overhead.
- Design targeted single-column, composite, or covering indexes tailored specifically to the query access patterns.
- Rewrite non-sargable query predicates, correlated subqueries, or inefficient joins into sargable, engine-optimized alternatives.
- Formulate a canary testing and benchmarking protocol to validate query cost reduction against {{p99_latency_target}}.
- Detail a zero-downtime DDL deployment sequence including concurrent index creation and rollback safety triggers.
Constraints
- MUST prioritize zero-lock DDL index creation syntax specific to {{database_engine}}.
- MUST NOT recommend query rewrites that alter result set data types or deterministic ordering without explicitly stating trade-offs.
- All index recommendations must include explicit column ordering justifications based on selectivity.
- Every optimization step must map directly to an identified node in the execution plan.
Output format
- Section 1: Bottleneck Root Cause Analysis (ordered by compute cost impact, max 200 words)
- Section 2: Refactored SQL Query (annotated code block)
- Section 3: Indexing Strategy Specification (DDL statements and selectivity rationale)
- Section 4: Safe Rollout & Verification Checklist (5-7 sequential steps)
Self-review
- Did I verify that rewritten predicates are fully sargable for {{database_engine}}?
- Are composite index columns arranged by strict selectivity and equality-first ordering?
- Is the deployment plan completely safe against table-level write locks in production?
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.