Pcornel
Browse topics
Options
Recent
Favorites
Databases · Technical reference

Partial and Expression Indexes

Direct answer

Partial indexes cover only rows matching a condition; expression indexes index the result of an expression such as lower(email). (PostgreSQL; MySQL has functional indexes in 8.0.)

When to use this

Use it when queries filter on a subset or on an expression.

First 30 seconds

1. Query filter/expression.

2. Create: CREATE INDEX ON users (lower(email)); / … WHERE active;.

3. EXPLAIN uses it?

4. Query text matches the index expression.

5. Monitor usage.

Operational verification

index on subset/expression → matching query predicates → index scan

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
MariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformMariaDB / MySQL / PostgreSQL / Microsoft SQL Server concepts
Scope / prerequisiteUse it when queries filter on a subset or on an expression.
What next?2 suggested next steps
Evidence0
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

Capture the concrete output, symptom, packet, log or counter before changing anything.

Step 1 How to verify Verify boundary

Test with the real query parameters. Confirm writes are not slowed noticeably.

Step 2 Common failures Narrow the fault

A query written as LOWER(email) with different casing rules or a cast does not match. Parameterised queries may not match a partial index condition the planner cannot show. Unused specialised indexes still cost writes.

Key points

The query must use the same condition or expression for the index to be used. Partial indexes save space for skewed data (e.g. WHERE active).

Key checks
  • CREATE INDEX ON users (lower(email)); — first check for this topic
  • psql -c "SELECT now(), version();" — PostgreSQL reachable and version
  • mysql -e "SELECT NOW(), VERSION();" — MariaDB/MySQL reachable and version
  • sqlcmd -S <server> -Q "SELECT @@VERSION" — SQL Server reachable and version
ReferenceWhat it is1 section · depth on demand

How it works

PostgreSQL supports both directly (CREATE INDEX ... ON t (lower(email)) and ... WHERE status = 'open'). MySQL 8 supports functional key parts; MariaDB uses indexed virtual columns. SQL Server uses filtered indexes and computed columns.

ReferenceImportant terms & boundaries3 sections · depth on demand

Examples

-- partial: only index active orders
CREATE INDEX orders_open_idx ON orders (customer_id) WHERE status = 'open';
-- expression: case-insensitive email lookups
CREATE INDEX customers_email_lower_idx ON customers (lower(email));
SELECT * FROM customers WHERE lower(email) = lower($1);

Documented example, not captured output. Queries must use the same predicate/expression to use the index. MySQL 8 supports functional indexes; SQL Server uses filtered indexes and computed columns.

In depth

When a partial index wins

A queue table with 50 million done rows and 2,000 pending rows:

CREATE INDEX jobs_pending_idx ON jobs (priority, created_at) WHERE status = 'pending';
SELECT id FROM jobs WHERE status = 'pending' ORDER BY priority, created_at LIMIT 100;

The index holds only the 2,000 pending rows: tiny, always cached, and updated only when rows enter or leave the pending state.

The planner must be able to prove the predicate

QueryUses WHERE status = 'pending' index?
WHERE status = 'pending'Yes
WHERE status = $1 (generic prepared plan)No — the value is unknown at plan time
WHERE status IN ('pending','retry')No — wider than the index predicate
WHERE status = 'pending' AND priority > 5Yes

Write the literal in the query (or use custom plans) when relying on a partial index.

Expression indexes

CREATE INDEX users_email_lower ON users (lower(email));
SELECT * FROM users WHERE lower(email) = lower($1);           -- matches the indexed expression
CREATE UNIQUE INDEX users_email_lower_uq ON users (lower(email)); -- case-insensitive uniqueness

The query expression must match the index expression exactly (same function, same casts). ANALYZE collects statistics on indexed expressions, which also improves estimates.

MySQL and MariaDB

MySQL 8.0.13+ supports functional key parts: CREATE INDEX users_email_lower ON users ((lower(email))); (note the double parentheses). MySQL has no partial indexes; MariaDB uses indexed virtual (generated) columns for expressions. A common workaround for partial indexes is a generated column that is NULL for uninteresting rows, indexed normally.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

Common mistakes

  • Expression indexes not matching the query expression exactly.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terminology layer

Terms & aliases

Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.

Aliases that resolve here
partial and expression indexesexpression indexpartial filtered index
Confirmation

Confirm action

Continue?