Works with the AI Agents you already use

    Claude CodeClaude CodeCursorCursorCodex CLICodex CLIMuseMuseDotsDots+21 more

    SQL Query Clinic Skill

    1

    Optimize SQL queries, analyze EXPLAIN plans, and diagnose database errors across major SQL dialects.

    $5

    Secure checkout via Stripe

    Money-back guarantee

    Converts to your local currency at checkout

    Security scanned
    SQL Query Clinic Skill

    SQL Query Clinic Skill

    Example session with this skill installed

    My PostgreSQL query is taking 5 seconds. Here is the SQL and the EXPLAIN plan. It joins 'orders' and 'users' on user_id. The plan shows a Sequential Scan on 'orders'.

    • Read your context and instructions
    • Compiled the sql query clinic

    I have analyzed the query and execution plan. The bottleneck is a sequential scan on a large table due to a missing foreign key index and a non-sargable date filter.

    SQL Query Diagnosis

    1. Performance Findings

    The execution plan reveals a Seq Scan on the orders table (850,000 rows). While the users table is indexed, the orders.user_id column lacks an index, forcing the optimizer to scan the entire table for the join. Additionally, the filter WHERE TO_CHAR(created_at, 'YYYY') = '2023' prevents the use of any index on created_at.

    2. Index Recommendations

    CREATE INDEX idx_orders_user_id ON orders(user_id);
    CREATE INDEX idx_orders_created_at_brin ON orders(created_at) WHERE (created_at >= '2023-01-01' AND created_at < '2024-01-01');
    
    • Benefit: Converts the Seq Scan to an Index Scan.

    Trade-off: Increases storage usage by ~45MB and adds minor overhead to INSERT operations on the orders table.

    3. Rewritten Query

    SELECT 
        u.username, 
        o.order_total, 
        o.created_at
    FROM users u
    INNER JOIN orders o ON u.id = o.user_id
    WHERE o.created_at >= '2023-01-01' AND o.created_at < '2024-01-01';
    

    4. Why the Fix Helps

    Sargable Predicate: Replacing TO_CHAR with a range comparison allows PostgreSQL to use a B-tree or BRIN index on the created_at column.

    Join Optimization: The new index on orders.user_id enables a Hash Join or Merge Join to operate on indexed sets rather than scanning the heap.

    Next steps

    1. Apply the idx_orders_user_id index in a development environment.
    2. Run EXPLAIN ANALYZE with the rewritten query to confirm the move from Seq Scan to Index Scan.
    3. Verify that the result set row count matches the original query to ensure semantic parity.

    Connects securely to your tools. The creator never sees your data.

    About this skill

    Slow queries and vague database errors stall development. This skill acts as a SQL Performance Engineer to audit your SQL, diagnose bottlenecks, and provide optimized rewrites.

    What it does

    • Syntax & logic audit finds incorrect joins, NULL handling issues, and aggregation errors.
    • Error diagnosis maps database error messages to specific failing SQL segments and root causes.
    • Execution plan analysis parses EXPLAIN output to identify expensive scans, join strategies, and row-estimation mismatches.
    • Index recommendations suggests specific column ordering for predicates and joins while considering write overhead.
    • Query refactoring rewrites inefficient subqueries and non-sargable expressions into high-performance alternatives.

    How it works

    1. Input query and context providing the SQL, any error messages, and optionally the EXPLAIN plan or schema.
    2. Analysis phase where the tool checks for correctness before moving to performance bottlenecks.
    3. Execution plan review to confirm if the database is accessing data efficiently or missing indexes.
    4. Optimized output receiving a corrected query, index DDL, and a breakdown of why the changes improve performance.

    Frameworks & tools

    Supports PostgreSQL, MySQL, MariaDB, SQLite, SQL Server, and Oracle. Works with standard EXPLAIN and EXPLAIN ANALYZE outputs.

    Why this beats prompting it yourself

    Generic prompts often overlook database-specific optimizer behavior or suggest indexes that slow down writes. This skill follows a rigorous workflow that prioritizes confirmed evidence over guesses and ensures query semantics remain identical after refactoring.

    Use cases

    • Optimizing a slow dashboard query that times out on large datasets.
    • Troubleshooting a "Column not found" or "Ambiguous reference" error in a complex multi-join query.
    • Reviewing a new migration to ensure it includes the necessary indexes for production traffic.
    • Converting a heavy subquery into a more efficient JOIN or CTE structure.

    Known limitations

    Cannot execute queries directly on your database. Accuracy depends on the level of schema and execution plan detail provided by the user.

    Requires user-provided schema or EXPLAIN plans for high-accuracy optimization. Does not execute SQL. Performance gains are estimates until verified on actual hardware.

    How to use

    1. 1

      Connect your agent to the Agensi MCP

      Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup

    2. 2

      Sign in to Agensi

      Your agent shows a sign-in link. Sign in and click Allow.

    3. 3

      Ask for what you need

      Your agent loads this skill and uses it on your task. It always asks before any payment.

    Reviews

    No reviews yet

    Be one of the first to try it. Every listed skill goes through the trust checks below.

    Security scanned

    Checked by our 8-point scan before listing

    Fresh listing

    Recently published to Agensi

    Money-back guarantee

    Doesn't do what its page promises? You get your money back.

    Trust & safety

    Security scan

    Security scan passed 1 month ago

    Permissions

    No special permissions declared or detected

    Listed1 month ago
    Updated22 days ago

    What's inside

    Frequently Asked Questions

    This skill supports a wide range of major dialects including PostgreSQL, MySQL, MariaDB, SQLite, SQL Server, and Oracle, ensuring specialized optimization for each engine's specific optimizer behavior.

    While the skill cannot access your live database for security reasons, it provides the highest accuracy when you provide the SQL query alongside the output of an `EXPLAIN` or `EXPLAIN ANALYZE` command.

    Unlike generic AI prompts, this skill uses a structured diagnostic workflow to ensure query logic remains identical while improving speed, specifically accounting for index write-overhead and non-sargable expressions.

    The skill diagnoses syntax errors, incorrect join logic, and ambiguous references by mapping your database's specific error messages to the failing segments of your code.

    You will receive the full SQL Query Clinic Skill to integrate with your preferred AI agent, plus a guide on how to best provide schema context and execution plans for optimal results.

    New in Data & Databases