SQL Query Doctor

    1

    SQL query doctor for slow queries: reads your query, schema and EXPLAIN plan, ranks the causes and proposes rewrites and indexes with a safe way to verify. PostgreSQL, MySQL, SQLite, SQL Server.

    $5

    Secure checkout via Stripe

    Money-back guarantee

    Converts to your local currency at checkout

    Security scanned

    Works with the AI Agents you already use

    Claude CodeClaude CodeCursorCursorCodex CLICodex CLIMuseMuseDotsDots+21 more

    SQL Query Doctor

    Example session with this skill installed

    This query takes 2.3 seconds on our PostgreSQL orders table (40 million rows). Here are the query, the table DDL with its indexes and the EXPLAIN (ANALYZE, BUFFERS) output.

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

    Illustrative. The query, schema, plan and timings are invented; this shows the structure and depth the skill produces.

    Diagnosis

    The query filters on status and a date range and sorts by created_at, but no index serves that filter, so the database scans the whole table and sorts the result. The estimate of matching rows (about 900) is close to the actual (about 1,100), so statistics are not the problem.

    Evidence from the plan

    • Sequential scan on orders removed 39.9 million rows by filter to return about 1,100 rows.
    • Sort of about 1,100 rows in memory: cheap. The cost is the scan before it.
    • Planning time is negligible; execution time (about 2.3 s) is dominated by the scan.

    Recommended changes (in order)

    Add a partial index that matches the filter and the sort. The query only reads open orders, a small fraction of the table:

    CREATE INDEX CONCURRENTLY idx_orders_open_created
      ON orders (created_at)
      WHERE status = 'open';
    

    Run it outside a transaction block. It adds write cost only for rows that are, or become, open.
    2.

    Keep the query as it is. It already filters on status = 'open' and a created_at range, so the partial index applies. No rewrite is needed.
    3.

    Only if the date range is often wide, consider keyset pagination on (created_at, id) for the listing page instead of OFFSET.

    Verification plan

    1. On a copy with realistic data, create the index and run the same query under EXPLAIN (ANALYZE, BUFFERS).
    2. Confirm the plan changes from a sequential scan to an index scan on the new index, and compare the time.
    3. Compare the result rows with the old run (same rows, same order).
    4. Measure insert and update time on orders before and after on the copy.
    5. Create the index on production in a quiet period with CONCURRENTLY; keep the statement to drop it if the write cost is too high.

    Risks and trade-offs

    • The index only helps queries that include status = 'open'.
    • An index built concurrently takes longer and can leave an invalid index if it fails; check for that afterwards.
    • Writes that move an order out of open update the index.

    What I could not determine

    • How often the query runs and at what concurrency.
    • Whether other queries filter on other statuses and would benefit from a different index.
    • The real distribution of status over time: if open becomes common, the partial index loses its advantage.

    Next steps

    • Run the verification plan on a copy before touching production.
    • Send the EXPLAIN output again after the index exists so the result can be checked.

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

    About this skill

    Find out why a query is slow or wrong. Paste the query, the schema (with indexes), the execution plan and the database version, and it reads the plan node by node: scans that should be index lookups, row estimates that are far from the actual rows, sorts that spill to disk, repeated subplans, functions on indexed columns, leading wildcards, deep offset pagination and the N+1 pattern from an ORM. It ranks the likely causes by expected gain and risk and proposes fixes, each with the trade-offs.

    What makes it different from a generic prompt: it works from the plan, not from guesses. A rewrite must return the same rows, and it states the conditions (for example NULL handling when replacing NOT IN with NOT EXISTS). Every index recommendation names its write cost and the safest way to build it on your engine. Every recommendation ends with a verification plan to run on a copy or staging first.

    What it reads and does: it works only from the text you paste. It never connects to a database, never runs a query and never asks for credentials. It warns that an ANALYZE option executes the statement, and how to run a data-changing statement safely.

    Honest limits: without your real data distribution and concurrency, a recommendation is a hypothesis to test. It does not design whole schemas or tune server settings from one query.

    Works only from the query, schema and plan you paste. Without your real data distribution and concurrency, a recommendation is a hypothesis to test. It does not design whole schemas or tune server settings from a single query.

    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 13 days ago

    Requires

    None

    Permissions

    Read Files

    File Scopes

    <the
    query
    schema
    and
    plan
    you
    name>

    A prompt-only skill (SKILL.md). It reads only the query, schema and plan you paste or name; it never connects to a database, runs no commands, makes no network connection and reads no environment variables.

    Listed13 days ago

    What's inside

    Frequently Asked Questions

    No. It never connects to a database, never runs a query and never asks for credentials. You paste the query, the schema and the plan as text.

    PostgreSQL, MySQL, SQLite and SQL Server, with notes for each engine on how to get the plan and how to build an index safely. For other engines it reasons from the plan text and says what it cannot confirm.

    It tells you what is likely and how to get the plan for your engine before recommending an index. It also warns that an ANALYZE option runs the statement, and how to run a data-changing statement safely inside a transaction that is rolled back.

    It states the conditions under which the rewrite is equivalent (for example NULL handling when replacing NOT IN with NOT EXISTS) and gives a check that compares the results of the old and new query on a copy.

    New in Data & Databases