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-covering-indexes.md
1---2title: Use Covering Indexes to Avoid Table Lookups3impact: MEDIUM-HIGH4impactDescription: 2-5x faster queries by eliminating heap fetches5tags: indexes, covering-index, include, index-only-scan6---78## Use Covering Indexes to Avoid Table Lookups910Covering indexes include all columns needed by a query, enabling index-only scans that skip the table entirely.1112**Incorrect (index scan + heap fetch):**1314```sql15create index users_email_idx on users (email);1617-- Must fetch name and created_at from table heap18select email, name, created_at from users where email = '[email protected]';19```2021**Correct (index-only scan with INCLUDE):**2223```sql24-- Include non-searchable columns in the index25create index users_email_idx on users (email) include (name, created_at);2627-- All columns served from index, no table access needed28select email, name, created_at from users where email = '[email protected]';29```3031Use INCLUDE for columns you SELECT but don't filter on:3233```sql34-- Searching by status, but also need customer_id and total35create index orders_status_idx on orders (status) include (customer_id, total);3637select status, customer_id, total from orders where status = 'shipped';38```3940Reference: [Index-Only Scans](https://www.postgresql.org/docs/current/indexes-index-only-scans.html)41