- Home
- Skills
- Data & Databases
- SQL Query Clinic Planner
Works with the AI Agents you already use
SQL Query Clinic Planner
Diagnose slow SQL, interpret execution plans, and rewrite queries for better performance and correctness.
$5
SQL Query Clinic Planner
Example session with this skill installed
Analyze this PostgreSQL query and EXPLAIN output. It is taking 5 seconds to run on a table with 10M rows.
SELECT * FROM orders WHERE DATE(created_at) = '2023-01-01';
Plan: Seq Scan on orders (cost=0.00..254.00 rows=50000 width=120) Filter: (date(created_at) = '2023-01-01'::date)
- Read your context and instructions
- Compiled the sql query clinic
I have analyzed the query and execution plan. The primary bottleneck is a non-sargable predicate preventing index usage.
Diagnosis
| Issue | Evidence | Severity | Confidence |
|---|---|---|---|
| Index Suppression | Function DATE() on column created_at | P1 | High |
| Full Table Scan | Seq Scan in EXPLAIN plan | P1 | High |
Correctness Issues
No correctness issues found. The query logic is sound but inefficient for large datasets.
Performance Bottlenecks
The use of the DATE() function on the created_at column makes the expression non-sargable (Search ARGumentable). Even if an index exists on created_at, the database must perform a full sequence scan to apply the function to every row before filtering.
Index Advice
- Table:
orders - Candidate Column:
created_at - Expected Benefit: Transform
Seq ScantoIndex Strategy Seek/Scan. - Risk: Minimal; standard B-tree index.
Recommended Rewrite
Original
SELECT * FROM orders WHERE DATE(created_at) = '2023-01-01';
Revised
SELECT * FROM orders
WHERE created_at >= '2023-01-01 00:00:00'
AND created_at < '2023-01-02 00:00:00';
Why
By using a range comparison on the raw column, the database can use a standard B-tree index on created_at. This avoids the O(N) cost of the function call per row.
Validation Checks
- Verify if an index exists on
created_at. If not, run:CREATE INDEX idx_orders_created_at ON orders(created_at); - Run
EXPLAIN ANALYZEwith the revised query to confirm anIndex ScanorIndex Only Scanis used. - Compare
Buffers(I/O) between the two versions.
Next steps
- Check if there are other queries using functions on
created_at. - Verify the distribution of data for Jan 1st to ensure index selectivity.
- Apply the range-based rewrite in the application code.
Connects securely to your tools. The creator never sees your data.
About this skill
The problem
Slow or broken SQL queries often result in guessing games involving trial-and-error indexing and rewrites that may change query semantics. Without an execution plan analysis, developers risk introducing production regressions or technical debt through redundant indexes.
What it does
- Analyzes EXPLAIN and EXPLAIN ANALYZE output to identify table scans, join algorithms, and cardinality misestimations.
- Performs correctness audits for NULL behavior, JOIN conditions, and three-valued logic to fix bug-prone queries.
- Audits existing indexes for redundancy, overlap, and sargability to minimize write overhead.
- Proposes semantically equivalent rewrites for correlated subqueries, non-selective predicates, and inefficient pagination.
- Classifies bottlenecks by severity (P0-P3) based on CPU, I/O, and locking contention evidence.
Frameworks & tools
Supports PostgreSQL, MySQL, MariaDB, SQL Server, Oracle, SQLite, Snowflake, BigQuery, and Redshift.
Why this beats prompting it yourself
Generic LLM prompts often hallucinate performance gains or suggest destructive changes without considering write costs. This skill follows a strict evidence-based workflow that distinguishes between confirmed bottlenecks and hypotheses, ensuring rewrites preserve original query semantics.
Use cases
- Diagnosing a sudden spike in CPU or I/O caused by a specific production query.
- Refactoring complex legacy CTEs and subqueries for better maintainability and plan stability.
- Auditing a table's index strategy to remove low-value indexes that slow down inserts.
- Debugging silent data loss or row multiplication in complex multi-way joins.
Known limitations
Requires user-supplied schema or execution plans for high-confidence advice. Cannot execute SQL or access live databases directly.
Requires manually provided schema, execution plans, or statistics for accurate diagnosis. Does not execute destructive SQL or connect to live production environments.
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