SQL
AuraScore 89/100

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.

Template

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

  1. Analyze the execution plan in {{current_execution_plan}} to identify full table scans, expensive hash joins, sorting bottlenecks, and temporary disk spills.
  2. Cross-reference row cardinality and data distribution from {{table_volume_and_schema}} against filter predicates and join conditions in {{problematic_query}}.
  3. Evaluate existing primary, foreign, and secondary indexes on {{database_engine}} to detect index misses or redundant indexing overhead.
  4. Design targeted single-column, composite, or covering indexes tailored specifically to the query access patterns.
  5. Rewrite non-sargable query predicates, correlated subqueries, or inefficient joins into sargable, engine-optimized alternatives.
  6. Formulate a canary testing and benchmarking protocol to validate query cost reduction against {{p99_latency_target}}.
  7. 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?
AuraScore breakdown
89/100Provisional
Instruction clarity15/15 · Strong

Explicit role, a named task, and discrete steps the model can follow.

Context architecture12/12 · Strong

Background, inputs and variables the model needs before it starts.

Constraint engineering12/12 · Strong

Hard boundaries — what the model must and must not do.

Output specification14/14 · Strong

A named, field-level shape for the response.

Reasoning structure10/10 · Strong

Ordered work items that force analysis before an answer.

Model compatibility10/10 · Strong

Length and structure that travel across frontier models.

Token efficiency5/10 · Thin

Signal density — instruction weight without padding.

Reusability7/7 · Strong

Documented variables so the scaffold adapts to new inputs.

Robustness3/5 · Adequate

Quality bar, assumptions and behaviour when inputs are thin.

Observed performance1/5 · Thin

How much real usage the template has behind it.

data-analytics
data-sql
software-engineering-debugging
sql
performance-tuning
query-optimization