SQL · Module 11 · Lesson 21 min read

Composite, Unique, Partial & Expression Indexes

Composite (multi-column) index

CREATE INDEX ON expenses (user_id, spent_on);

  • Sorted by user_id, then by spent_on within each user
  • ✅ Helps: WHERE user_id = 1, WHERE user_id = 1 AND spent_on >= …, WHERE user_id = 1 ORDER BY spent_on DESC
  • ❌ Doesn't help much: WHERE spent_on >= … alone (the leftmost prefix rule)

Column order rules of thumb

  1. Columns compared with = first
  2. Then range (>, <, BETWEEN) or ORDER BY columns
  3. Put the most-used filter first

Unique index

CREATE UNIQUE INDEX ON users (lower(email)); – enforces uniqueness (here case-insensitively!)

Partial index – only some rows

CREATE INDEX ON bills (due_date) WHERE NOT paid;

  • Smaller and faster: indexes only the rows you actually query
  • The query's WHERE must imply the index condition
  • Great for: unpaid bills, active users, non-deleted rows (WHERE deleted_at IS NULL)
  • Partial unique index: "only one active subscription per user": CREATE UNIQUE INDEX … (user_id) WHERE status = 'active'

Expression index

Index the result of an expression:

  • CREATE INDEX ON users (lower(email)); → fast WHERE lower(email) = …
  • CREATE INDEX ON expenses (date_trunc('month', spent_on)); The query must use exactly the same expression.

Covering index (INCLUDE)

CREATE INDEX ON expenses (user_id) INCLUDE (amount); Extra columns are stored in the index, so the query can be answered from the index alone (an index-only scan).

Sort direction

CREATE INDEX ON expenses (user_id, spent_on DESC); – matches "newest first" queries

Don't over-index

Check which indexes are never used: pg_stat_user_indexes.idx_scan = 0

Example

SQL
-- Test data: 100,000 expenses
CREATE TABLE tx AS
SELECT g AS id, (g % 1000) + 1 AS user_id,
       DATE '2026-01-01' + (g % 330) AS spent_on,
       (g % 5000)::numeric AS amount,
       (g % 7 = 0) AS flagged
FROM generate_series(1, 100000) g;
ANALYZE tx;

-- Composite index: equality column first, then range/sort column
CREATE INDEX idx_tx_user_date ON tx (user_id, spent_on DESC);

EXPLAIN SELECT * FROM tx WHERE user_id = 7 AND spent_on >= '2026-11-01' ORDER BY spent_on DESC;
-- uses idx_tx_user_date ✅ (Index Scan or Bitmap Index Scan, depending on how many rows match)

EXPLAIN SELECT * FROM tx WHERE spent_on = '2026-11-01';
-- Seq Scan — spent_on isn't the leftmost column of the index

-- Partial index: only the rows we care about
CREATE INDEX idx_tx_flagged ON tx (spent_on) WHERE flagged;
EXPLAIN SELECT * FROM tx WHERE flagged AND spent_on = '2026-06-01';   -- uses idx_tx_flagged

-- Expression index for case-insensitive email lookups
CREATE UNIQUE INDEX idx_users_email_lower ON users (lower(email));
EXPLAIN SELECT * FROM users WHERE lower(email) = 'asha@example.com';
-- (on a 6-row table the planner may still choose a Seq Scan — it's cheaper!)

-- ❌ The unique expression index blocks case variants
-- INSERT INTO users (name, email) VALUES ('Copy', 'ASHA@example.com');  -- duplicate key

-- Partial UNIQUE: only one unpaid bill with the same title per user
CREATE UNIQUE INDEX one_unpaid_bill ON bills (user_id, title) WHERE NOT paid;

-- Covering index → index-only scan
CREATE INDEX idx_tx_user_cover ON tx (user_id) INCLUDE (amount);
VACUUM ANALYZE tx;
EXPLAIN SELECT sum(amount) FROM tx WHERE user_id = 42;
-- Index Only Scan using idx_tx_user_cover

DROP TABLE tx;