🎯 Index Only What You Query
Full indexes waste space. Partial indexes index only rows matching a WHERE clause. Smaller, faster, more efficient.
📝 Basic Partial Index
-- Index only active users (not soft-deleted) CREATE INDEX idx_active_users_email ON users(email) WHERE deleted_at IS NULL; -- Query uses this index automatically SELECT * FROM users WHERE email = 'alice@example.com' AND deleted_at IS NULL; -- Index only orders with high value CREATE INDEX idx_high_value_orders ON orders(customer_id, order_date) WHERE total_amount > 1000; -- Only large orders indexed (saves space)
🎯 Real-World Examples
-- Index only pending orders (most frequently queried) CREATE INDEX idx_pending_orders ON orders(created_at) WHERE status = 'pending'; -- Index only premium customers CREATE INDEX idx_premium_users ON users(last_login) WHERE subscription_tier = 'premium' AND active = true; -- Index only current year invoices CREATE INDEX idx_current_year_invoices ON invoices(invoice_date) WHERE invoice_date >= '2024-01-01'; -- Exclude test data CREATE INDEX idx_prod_products ON products(sku, name) WHERE is_test = false;
✅ Benefits
- Smaller index size: 70-90% smaller than full index
- Faster queries: Scan fewer index pages
- Less maintenance: Updates on excluded rows skip index
- Less disk I/O: Smaller index fits in memory
💡 When to Use
- 95% of queries filter on a specific status value
- Soft-deleted rows rarely queried
- Historical data vs active data
- Test data that shouldn’t be indexed
- Only recent data is queried frequently
“Full index on 50M rows was 8GB. Partial index on active users (WHERE active=true) was 500MB. Query speed improved 10x. Always consider partial indexes.”



