Partial, Expression, and Functional Indexes

High-leverage index patterns for subsets of data and computed predicates across PostgreSQL, MySQL, and SQLite.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Pattern catalog

Advanced patterns that often beat naive full-table indexes.

PostgreSQL case-insensitive email lookup

Index the normalized expression you actually query.

sqlANYpostgresqlexpression-index
sql
CREATE INDEX idx_users_lower_email ON users ((lower(email)));

SELECT * FROM users WHERE lower(email) = lower('a@example.com');

If the predicate calls a function, a plain index on the raw column may not help enough.

Index only open items

Keep the hot subset small and efficient.

sqlANYpartial-indexqueues
sql
CREATE INDEX idx_tickets_open_priority_created_at
ON tickets (priority, created_at DESC)
WHERE closed_at IS NULL;

Great for dashboards or queues that mostly show open work items.

MySQL generated column from JSON

Make a JSON path searchable with an index.

sqlANYmysqljsongenerated-column
sql
ALTER TABLE events
  ADD COLUMN event_type VARCHAR(64) GENERATED ALWAYS AS (JSON_UNQUOTE(JSON_EXTRACT(payload, '$.type'))) STORED,
  ADD INDEX idx_events_event_type (event_type);

Generated columns are a common way to index computed values in MySQL.

SQLite partial index for active rows

Shrink the index to the rows that matter most.

sqlANYsqlitepartial-index
sql
CREATE INDEX idx_subscriptions_active_renewal_date
ON subscriptions (renewal_date)
WHERE canceled_at IS NULL;

This is especially attractive in lightweight local databases where space matters.

Recommended next

No recommendations yet.