Databaseprompt onlyMedium

Query Performance Fixer

An automated workflow to identify, analyze, and optimize slow-running database queries through iterative benchmarking and targeted improvements.

← all loops
databaseperformancesqloptimizationbenchmarking

Goal

Reduce query execution time to meet performance thresholds

How to Run

Run this loop in Cursor, Claude Code, Codex, OpenCode, or Gemini CLI to automatically optimize slow SQL queries

  1. 01

    Prepare Environment

    Ensure database connection and benchmarking tools are available

  2. 02

    Execute Loop

    Run the loop which will iteratively benchmark and optimize the target query

  3. 03

    Review Results

    Inspect optimized query and benchmark results after loop completes

Workflow Steps

  1. 01

    Analyze target query to identify potential bottlenecks

    Review query execution plan and structure

  2. 02

    Run initial benchmark to establish baseline performance

    Execute 'query benchmark' and record timing

  3. 03

    Apply optimization techniques based on analysis

    Implement indexing, query restructuring, or caching

  4. 04

    Re-run benchmark to measure improvement

    Execute 'query benchmark' and compare with baseline

  5. 05

    Evaluate results against exit condition

    If query under threshold, exit; else, return to step 1

Kickoff Prompt

Start the "Query Performance Fixer" loop.

Goal: Reduce query execution time to meet performance thresholds
Max iterations: 10
Between iterations run: query benchmark
Exit when: Query execution time under 100ms


Please provide the SQL query that needs optimization and specify your performance threshold. I will analyze it, run benchmarks, and iteratively apply optimizations until we meet the target performance or reach the maximum iterations.

Self-pace this loop. After each iteration, run `query benchmark` and evaluate the output, and only continue if the exit condition is not met (Query execution time under 100ms). Stop when the exit condition passes or 10 iterations are reached. Give a short status update each pass.

Guardrails

hardcoded
  • ·Prevent destructive schema changes during optimization
  • ·Ensure all query changes are tested in isolated environment
  • ·Limit iterations to prevent infinite loops
  • ·Preserve original query for rollback capability
  • ·Validate query correctness after each optimization

Flow Diagram

rendering…

Related loops — Database