- Home
- Skills
- Data & Databases
- SQL Query Clinic Skill
Works with the AI Agents you already use
SQL Query Clinic Skill
Optimize SQL queries, analyze EXPLAIN plans, and diagnose database errors across major SQL dialects.
$5
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 Scanto anIndex 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
- Apply the
idx_orders_user_idindex in a development environment. - Run
EXPLAIN ANALYZEwith the rewritten query to confirm the move fromSeq ScantoIndex Scan. - 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
EXPLAINoutput 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
- Input query and context providing the SQL, any error messages, and optionally the
EXPLAINplan or schema. - Analysis phase where the tool checks for correctness before moving to performance bottlenecks.
- Execution plan review to confirm if the database is accessing data efficiently or missing indexes.
- 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
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 1 month ago
Permissions
No special permissions declared or detected