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/query-missing-indexes.md
1---2title: Add Indexes on WHERE and JOIN Columns3impact: CRITICAL4impactDescription: 100-1000x faster queries on large tables5tags: indexes, performance, sequential-scan, query-optimization6---78## Add Indexes on WHERE and JOIN Columns910Queries filtering or joining on unindexed columns cause full table scans, which become exponentially slower as tables grow.1112**Incorrect (sequential scan on large table):**1314```sql15-- No index on customer_id causes full table scan16select * from orders where customer_id = 123;1718-- EXPLAIN shows: Seq Scan on orders (cost=0.00..25000.00 rows=100 width=85)19```2021**Correct (index scan):**2223```sql24-- Create index on frequently filtered column25create index orders_customer_id_idx on orders (customer_id);2627select * from orders where customer_id = 123;2829-- EXPLAIN shows: Index Scan using orders_customer_id_idx (cost=0.42..8.44 rows=100 width=85)30```3132For JOIN columns, always index the foreign key side:3334```sql35-- Index the referencing column36create index orders_customer_id_idx on orders (customer_id);3738select c.name, o.total39from customers c40join orders o on o.customer_id = c.id;41```4243Reference: [Query Optimization](https://supabase.com/docs/guides/database/query-optimization)44