5 concepts · 10 practice questions
A B-tree index keeps a column's values sorted so the database can seek instead of scanning. A composite index on (a, b) serves filters on a, or on a and b, but helps little with b alone, because b is only sorted within each a (a skip scan can help a little when a has few distinct values). Wrapping the column in a function, like lower(email), stops a plain index from matching; index the expression instead. Every index speeds some reads and slows every write. EXPLAIN (ANALYZE) shows what the planner actually chose.
CREATE INDEX idx_orders_customer_created
ON orders (customer_id, created_at);
-- Seeks on the leading column, then reads created_at in order
EXPLAIN ANALYZE
SELECT * FROM orders
WHERE customer_id = 42
ORDER BY created_at DESC
LIMIT 20;
-- For case-insensitive lookups, index the expression you filter on
CREATE INDEX idx_users_email_lower ON users (lower(email));
Sign in to read the rest and take the 10-question test.
Sign in to continue