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
PostgreSQL case-insensitive email lookup
CREATE INDEX idx_users_lower_email ON users ((lower(email)));

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

# Index the normalized expression you actually query.

Index only open items
CREATE INDEX idx_tickets_open_priority_created_at
ON tickets (priority, created_at DESC)
WHERE closed_at IS NULL;

# Keep the hot subset small and efficient.

MySQL generated column from JSON
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);

# Make a JSON path searchable with an index.

SQLite partial index for active rows
CREATE INDEX idx_subscriptions_active_renewal_date
ON subscriptions (renewal_date)
WHERE canceled_at IS NULL;

# Shrink the index to the rows that matter most.

Recommended next

No recommendations yet.