Intermediate LevelPostgreSQL
How do Partial Indexes and Expression Indexes optimize targeted queries?
❓ Interview Prompt: Explain Partial Indexes (`WHERE` predicate), Expression/Functional Indexes (`LOWER(email)`), index size reduction, and query matching.
1. Expression Indexes:
Indexes the computed output of a function ('CREATE INDEX ON users (LOWER(email))'), enabling fast queries matching 'WHERE LOWER(email) = 'user@example.com''.
2. Partial Indexes:
Indexes only a subset of table rows meeting a 'WHERE' predicate ('CREATE INDEX ON orders (created_at) WHERE status = 'UNPAID''), reducing index size by 90%!
Code Implementation Example
sql
-- Expression Index supporting case-insensitive lookups
CREATE INDEX idx_users_lower_email ON users (LOWER(email));
-- Partial Index indexing ONLY active un-fulfilled orders
CREATE INDEX idx_orders_unfulfilled ON orders (created_at)
WHERE status = 'UNPAYED';💡Creating Expression and Partial indexes for query optimization.
🎯 Interview Key Takeaway
Use Partial Indexes to index active subset records and save gigabytes of index RAM.
#Partial Index#Expression Index#Optimization#Intermediate
Back to all PostgreSQL Questions→