- Home
- Skills
- Data & Databases
- SQL Query Doctor
SQL Query Doctor
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
Works with the AI Agents you already use
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
ordersremoved 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
- On a copy with realistic data, create the index and run the same query under EXPLAIN (ANALYZE, BUFFERS).
- Confirm the plan changes from a sequential scan to an index scan on the new index, and compare the time.
- Compare the result rows with the old run (same rows, same order).
- Measure insert and update time on
ordersbefore and after on the copy. - 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
openupdate 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
statusover time: ifopenbecomes 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
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
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 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
Permissions
File Scopes
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.