Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Postgres performance optimization and best practices guide maintained by Supabase
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/monitor-explain-analyze.md
1---2title: Use EXPLAIN ANALYZE to Diagnose Slow Queries3impact: LOW-MEDIUM4impactDescription: Identify exact bottlenecks in query execution5tags: explain, analyze, diagnostics, query-plan6---78## Use EXPLAIN ANALYZE to Diagnose Slow Queries910EXPLAIN ANALYZE executes the query and shows actual timings, revealing the true performance bottlenecks.1112**Incorrect (guessing at performance issues):**1314```sql15-- Query is slow, but why?16select * from orders where customer_id = 123 and status = 'pending';17-- "It must be missing an index" - but which one?18```1920**Correct (use EXPLAIN ANALYZE):**2122```sql23explain (analyze, buffers, format text)24select * from orders where customer_id = 123 and status = 'pending';2526-- Output reveals the issue:27-- Seq Scan on orders (cost=0.00..25000.00 rows=50 width=100) (actual time=0.015..450.123 rows=50 loops=1)28-- Filter: ((customer_id = 123) AND (status = 'pending'::text))29-- Rows Removed by Filter: 99995030-- Buffers: shared hit=5000 read=1500031-- Planning Time: 0.150 ms32-- Execution Time: 450.500 ms33```3435Key things to look for:3637```sql38-- Seq Scan on large tables = missing index39-- Rows Removed by Filter = poor selectivity or missing index40-- Buffers: read >> hit = data not cached, needs more memory41-- Nested Loop with high loops = consider different join strategy42-- Sort Method: external merge = work_mem too low43```4445Reference: [EXPLAIN](https://supabase.com/docs/guides/database/inspect)46