Loading...
Loading...
Diagnoses and optimises slow SQL queries using EXPLAIN ANALYZE. Covers identifying bottlenecks (sequential scans, bad estimates, heap fetches), index strategy, query rewrites, and verification. Invoked when the user asks to optimize a query, fix a slow database query, or improve database performance.
npx skill4agent add soulcodex/agentic sql-query-optimizationEXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
SELECT ...; -- paste the full query hereEXPLAIN FORMAT=JSON
SELECT ...;| Signal | What it means |
|---|---|
| No index — or the planner chose not to use one |
| Rows estimate far from actual | Stale statistics — run |
| May need a nested-loop + index for small inputs |
High | Heavily cached — latency is CPU-bound, not I/O |
High | I/O bound — consider indexes or read replicas |
| Covering index may eliminate heap fetches |
-- B-tree for equality and range
CREATE INDEX CONCURRENTLY ON orders(user_id);
-- Partial index for common filter
CREATE INDEX CONCURRENTLY ON orders(created_at)
WHERE status = 'pending';
-- Covering index to eliminate heap fetch
CREATE INDEX CONCURRENTLY ON orders(user_id)
INCLUDE (status, total_amount);
-- Expression index for function-wrapped column
CREATE INDEX CONCURRENTLY ON users(lower(email));CONCURRENTLYJOINEXISTSSELECT *OFFSET-- Instead of: LIMIT 20 OFFSET 10000
WHERE id > :last_seen_id ORDER BY id LIMIT 20IN (SELECT ...)EXISTS (SELECT 1 FROM ...)ANALYZE orders; -- single table
ANALYZE; -- all tables (run as superuser)EXPLAIN (ANALYZE, BUFFERS)-- Supports: orders list by user, filtered on status (POST /api/users/:id/orders)
CREATE INDEX CONCURRENTLY ON orders(user_id, status);