Query Performance Fixer
An automated workflow to identify, analyze, and optimize slow-running database queries through iterative benchmarking and targeted improvements.
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
- 01
Prepare Environment
Ensure database connection and benchmarking tools are available
- 02
Execute Loop
Run the loop which will iteratively benchmark and optimize the target query
- 03
Review Results
Inspect optimized query and benchmark results after loop completes
Workflow Steps
- 01
Analyze target query to identify potential bottlenecks
Review query execution plan and structure
- 02
Run initial benchmark to establish baseline performance
Execute 'query benchmark' and record timing
- 03
Apply optimization techniques based on analysis
Implement indexing, query restructuring, or caching
- 04
Re-run benchmark to measure improvement
Execute 'query benchmark' and compare with baseline
- 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
Related loops — Database
Database
Backup Verification
Automates the verification of database backup restorability by repeatedly testing restore operations until successful or maximum iterations reached.
Database
Database Schema Sync Loop
Automatically detects and resolves schema drift between your database and ORM models by iteratively applying necessary migrations and verifying alignment.
Database
Foreign Key Repair Loop
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.