con un clic
db-explain
Run detailed query plan analysis to assess performance
Instalar con Codex o Claude Copia este prompt, pégalo en Codex, Claude u otro asistente, y deja que revise la página de la skill y la instale por ti.
Menú
Run detailed query plan analysis to assess performance
Instalar con Codex o Claude Copia este prompt, pégalo en Codex, Claude u otro asistente, y deja que revise la página de la skill y la instale por ti.
Basado en la clasificación ocupacional SOC
Execute story development with selectable automation modes to accommodate different developer preferences, skill levels, and story complexity.
Set up a Kord-aligned documentation baseline (no legacy frameworks)
Create Next Story Task methodology and workflow
Validate Next Story Task methodology and workflow
advanced-elicitation methodology and workflow
No checklists needed - this task facilitates brainstorming sessions, validation is through user interaction methodology and workflow
| name | db-explain |
| description | Run detailed query plan analysis to assess performance |
| agent | architect |
| subtask | false |
Run detailed query plan analysis to assess performance
Parameter:* mode (optional, default: interactive)
Purpose:* Definitive pass/fail criteria for task completion
Checklist:*
acceptance-criteria:
- [ ] Data persisted correctly; constraints respected; no orphaned data
type: acceptance-criterion
blocker: true
validation: |
Assert data persisted correctly; constraints respected; no orphaned data
error_message: "Acceptance criterion not met: Data persisted correctly; constraints respected; no orphaned data"
Strategy:* retry
Common Errors:*
Error:* Connection Failed
Error:* Query Syntax Error
Error:* Transaction Rollback
sql (string): SQL query to analyzeAsk user:
Execute with full analysis options:
psql "$SUPABASE_DB_URL" -v ON_ERROR_STOP=1 <<SQL
EXPLAIN (ANALYZE, BUFFERS, VERBOSE, FORMAT TEXT)
{sql};
SQL
Present key metrics:
=== Query Performance Analysis ===
Execution Time: X.XX ms
Planning Time: Y.YY ms
Total Time: Z.ZZ ms
Buffers:
- Shared Hit: XXX (cache hits)
- Shared Read: YYY (disk reads)
- Temp Read/Written: ZZZ (temp files)
Cost: XXX.XX..YYY.YY
Rows: Estimated XXX, Actual YYY
Planning Time*
Execution Time*
Total Cost*
Seq Scan* (Sequential Scan)
Index Scan*
Index Only Scan*
Bitmap Heap Scan*
Nested Loop*
Hash Join*
Merge Join*
Shared Hits* (Good)
Shared Reads* (Bad if high)
Temp Read/Written* (Bad)
Seq Scan on fragments (cost=0.00..10000 rows=1000000)
Filter: (user_id = '...')
Problem*: Scanning entire table
Impact*: Slow for large tables
Fix*: Create index
CREATE INDEX idx_fragments_user_id ON fragments(user_id);
Nested Loop (cost=0.00..50000)
-> Seq Scan on users
-> Seq Scan on fragments
Filter: (fragments.user_id = users.id)
Problem*: No index for join condition
Impact*: Quadratic complexity
Fix*: Index foreign key
CREATE INDEX idx_fragments_user_id ON fragments(user_id);
Sort (cost=10000..12000)
Sort Key: created_at DESC
Sort Method: external merge Disk: 5000kB
Problem*: Sorting spills to disk
Impact*: Much slower than in-memory
Fix*: Increase work_mem or add index
-- Option 1: Increase memory (session)
SET work_mem = '64MB';
-- Option 2: Add index to avoid sort
CREATE INDEX idx_fragments_created_at ON fragments(created_at DESC);
Seq Scan on users (cost=0.00..100 rows=10 actual rows=10000)
Problem*: Estimated 10 rows, actually 10,000
Impact*: Wrong join strategy chosen
Fix*: Update statistics
ANALYZE users;
-- Or more aggressive:
VACUUM ANALYZE users;
Seq Scan on fragments (cost=0.00..10000 rows=500000)
Filter: ((user_id = auth.uid()) AND (deleted_at IS NULL))
Rows Removed by Filter: 499990
Problem*: RLS policy not using index
Impact*: Scans all rows to apply policy
Fix*: Index RLS policy columns
CREATE INDEX idx_fragments_user_id_not_deleted
ON fragments(user_id)
WHERE deleted_at IS NULL;
Run current query:
explain "SELECT * FROM table WHERE ..."
Note execution time and plan.
What might be slow?
Apply potential fix:
CREATE INDEX ...;
-- or
VACUUM ANALYZE table;
-- or
SET work_mem = '...';
Run explain again:
explain "SELECT * FROM table WHERE ..."
Compare:
Repeat until performance acceptable.
# Option A
explain "SELECT * FROM users WHERE status = 'active'"
# Option B (rewritten)
explain "SELECT * FROM users WHERE deleted_at IS NULL AND status = 'active'"
Pick query with better plan.
For critical queries, analyze under load:
-- Run multiple times to warm cache
EXPLAIN (ANALYZE, BUFFERS) SELECT ...;
EXPLAIN (ANALYZE, BUFFERS) SELECT ...;
EXPLAIN (ANALYZE, BUFFERS) SELECT ...;
-- Check consistency of execution time
psql "$SUPABASE_DB_URL" -qAt -c \
"EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) SELECT ..." \
> query_plan.json
Upload to: https://explain.depesz.com or https://explain.dalibo.com
Interactive queries*: < 100ms
Reports*: < 1s
Batch/Background*: < 5s
If slower:*
Goal*: > 95% shared hits
-- Check overall cache hit ratio
SELECT
sum(heap_blks_hit) / (sum(heap_blks_hit) + sum(heap_blks_read)) AS cache_hit_ratio
FROM pg_statio_user_tables;
If low:*
Always:*
Reactive:*
Never:*
⚠️ Warning*: ANALYZE actually executes query
Safe:*
Dangerous:*
-- Safe way to EXPLAIN write queries
BEGIN;
EXPLAIN ANALYZE DELETE FROM ...;
ROLLBACK; -- Undo changes
Plans based on table statistics:
Plans vary based on:
Query optimization workflow:
explain "SELECT ..." - Baselineexplain "SELECT ..." - Verify improvementVisualization Tools:*
Documentation:*
Related Commands:*
analyze-hotpaths - Check common query patternsdesign-indexes - Plan index strategyrls-audit - Check RLS policy performance