SQL Query Optimizer — independently scanned and version-tracked by SaferSkills.
SaferSkills independently audited SQL Query Optimizer (Agent Skill) and scored it 100/100 (green). The audit ran 55 deterministic rules across Security, Supply Chain, Maintenance, Transparency, and Community; it found 0 high-severity and 0 lower-severity findings. The full rule-by-rule trace and per-finding evidence are below. Free, methodology-open.
Findings & checks · 0 flagged
Every scanned point with the score it earned and what moved between them.
First recorded scan — no prior version to compare against.
The primary manifest — the file an agent reads to learn what this artifact does.
This skill directs the agent to analyze a SQL query for common performance anti-patterns and rewrite it to execute more efficiently. It checks indexes, join strategies, subquery usage, N+1 patterns, unnecessary full table scans, and more — then provides a side-by-side comparison of the original and optimized query with a plain-English explanation of each change.
Use this when a query is running slowly in production, when you're writing a complex query for the first time and want a second opinion, or when you're reviewing a pull request that touches database queries.
Copy this file to .agents/skills/sql-query-optimizer/SKILL.md in your project root (for Claude Code), or add the instructions below to your .cursorrules (for Cursor).
Then ask:
Provide the query, and optionally:
EXPLAIN or EXPLAIN ANALYZE output if availableWhen asked to optimize a SQL query, follow these steps:
WHERE, JOIN ON, ORDER BY, or GROUP BY clauses indexed? Flag unindexed columns on large tables.WHERE clause contain a subquery that references the outer query? These execute once per row. Rewrite as a JOIN or CTE.WHERE (e.g., WHERE YEAR(created_at) = 2024)? This prevents index use.INT vs VARCHAR)? This forces a full scan.DISTINCT used where it's not needed? Is ORDER BY applied to a subquery that is later filtered?LIMIT x OFFSET y with a large offset? Suggest keyset pagination instead.WITH clauses) to improve readability when the logic is complex.CREATE INDEX statements that can be run directly.-- ORIGINAL QUERY
[paste original here]
-- OPTIMIZED QUERY
[rewritten query here]### What changed and why
- **Change 1:** [Explanation]
- **Change 2:** [Explanation]
### Recommended indexes
\```sql
CREATE INDEX idx_orders_user_id ON orders(user_id);
\```
### Estimated impact
[Brief note on expected improvement — e.g., "This should reduce the query from a full table scan to an index seek, likely cutting execution time by 80-95% on large tables."]Input to Agent:
"Optimize this PostgreSQL query. Theorderstable has 2M rows.user_idis not indexed.
>
``sql SELECT * FROM orders WHERE user_id = 42 AND status = 'pending' AND EXTRACT(YEAR FROM created_at) = 2025 ORDER BY created_at DESC; ``"Output from Agent:
-- ORIGINAL QUERY
SELECT *
FROM orders
WHERE user_id = 42
AND status = 'pending'
AND EXTRACT(YEAR FROM created_at) = 2025
ORDER BY created_at DESC;
-- OPTIMIZED QUERY
SELECT id, user_id, status, amount, created_at
FROM orders
WHERE user_id = 42
AND status = 'pending'
AND created_at >= '2025-01-01'
AND created_at < '2026-01-01'
ORDER BY created_at DESC;What changed and why
created_at could not be used). Replacing it with a range predicate allows an index seek.user_id to narrow rows before applying the date range filter.Recommended indexes
CREATE INDEX idx_orders_user_id_status_created ON orders(user_id, status, created_at DESC);Estimated impact
This changes a full sequential scan of 2M rows to an index scan targeting only rows for user_id = 42. On typical data distributions, this should reduce query time from seconds to milliseconds.
EXPLAIN ANALYZE, paste it — it gives the agent exact scan types and row estimates to work with.~30 seconds. Free. No account. Every finding cites a rule and a line of evidence.