An automated workflow to identify, analyze, and optimize slow-running database queries through iterative benchmarking and targeted improvements.
Reduce query execution time to meet performance thresholds
Run this loop in Cursor, Claude Code, Codex, OpenCode, or Gemini CLI to automatically optimize slow SQL queries
Prepare Environment
Ensure database connection and benchmarking tools are available
Execute Loop
Run the loop which will iteratively benchmark and optimize the target query
Review Results
Inspect optimized query and benchmark results after loop completes
Analyze target query to identify potential bottlenecks
Review query execution plan and structure
Run initial benchmark to establish baseline performance
Execute 'query benchmark' and record timing
Apply optimization techniques based on analysis
Implement indexing, query restructuring, or caching
Database
Automates the verification of database backup restorability by repeatedly testing restore operations until successful or maximum iterations reached.
Database
Automatically detects and resolves schema drift between your database and ORM models by iteratively applying necessary migrations and verifying alignment.
Database
This loop identifies and repairs broken foreign key relationships in a relational database. It systematically detects constraint violations, proposes corrective actions, and validates fixes until all foreign key checks pass.
Re-run benchmark to measure improvement
Execute 'query benchmark' and compare with baseline
Evaluate results against exit condition
If query under threshold, exit; else, return to step 1
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.