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-partial-indexes.md
1---2title: Use Partial Indexes for Filtered Queries3impact: HIGH4impactDescription: 5-20x smaller indexes, faster writes and queries5tags: indexes, partial-index, query-optimization, storage6---78## Use Partial Indexes for Filtered Queries910Partial indexes only include rows matching a WHERE condition, making them smaller and faster when queries consistently filter on the same condition.1112**Incorrect (full index includes irrelevant rows):**1314```sql15-- Index includes all rows, even soft-deleted ones16create index users_email_idx on users (email);1718-- Query always filters active users19select * from users where email = '[email protected]' and deleted_at is null;20```2122**Correct (partial index matches query filter):**2324```sql25-- Index only includes active users26create index users_active_email_idx on users (email)27where deleted_at is null;2829-- Query uses the smaller, faster index30select * from users where email = '[email protected]' and deleted_at is null;31```3233Common use cases for partial indexes:3435```sql36-- Only pending orders (status rarely changes once completed)37create index orders_pending_idx on orders (created_at)38where status = 'pending';3940-- Only non-null values41create index products_sku_idx on products (sku)42where sku is not null;43```4445Reference: [Partial Indexes](https://www.postgresql.org/docs/current/indexes-partial.html)46