Design and best practice · Designing databases

Partial or filtered indexes in SQL: indexing only some rows

A partial or filtered index contains only the rows that meet a condition, such as the pending tasks in a table.

Before continuing: This entry is part of the indexes guide. If you are new to the topic, start with INDEX.

Example

Runnable demo in SQLite
CREATE TEMP TABLE tasks_demo (
  id INTEGER PRIMARY KEY,
  status TEXT NOT NULL,
  priority INTEGER NOT NULL
);

INSERT INTO tasks_demo VALUES
  (1, 'done', 1),
  (2, 'pending', 2),
  (3, 'pending', 4),
  (4, 'done', 5),
  (5, 'pending', 3),
  (6, 'done', 2);

CREATE INDEX idx_tasks_pending_priority
ON tasks_demo (priority)
WHERE status = 'pending';

SELECT id, status, priority
FROM tasks_demo
WHERE status = 'pending'
  AND priority >= 2
ORDER BY priority DESC, id;

In this demo, EXPLAIN QUERY PLAN recognises the partial index for the condition status = 'pending' and the priority range. On another table, with different statistics or a different query, the plan may change.

How it is used

An index normally keeps entries for the whole table. A partial index keeps entries only for the subset where its predicate is true. If almost every task is finished and your application keeps querying the pending ones, the active subset can be much smaller than the full table.

It is not an automatic recipe: the benefit depends on the real data distribution, the queries and the optimiser. Design the predicate around a specific workload and check the plan with EXPLAIN.

Differences and trade-offs

The query must be compatible with the predicate

To use a partial index, the optimiser has to prove that the query stays within the indexed subset. PostgreSQL documents that the query's predicate must imply the index predicate and that this check happens at planning time. SQLite also applies rules to decide when a query's WHERE allows a partial index to be used.

How queries fit an index with WHERE status = 'pending'
QueryFit
WHERE status = 'pending' AND priority >= 2It works within the indexed subset.
WHERE status = 'done'It looks for exactly the rows the index does not contain.
No filter on statusIt needs rows in both states; the partial index on its own does not represent the whole table.

It can also enforce uniqueness on just a subset

PostgreSQL and SQLite support a partial UNIQUEindex. That pattern is useful, for example, to require a single “active” row per entity without applying the same rule to historical rows that are no longer active.

Conditional uniqueness
CREATE UNIQUE INDEX uq_active_subscription
ON subscriptions (customer_id)
WHERE active = 1;

Before using this design, check that the condition expresses a stable business rule and that your engine supports the specific syntax.

Key differences between engines
Names and syntax
EngineDirect support
PostgreSQLPartial indexes with CREATE INDEX ... WHERE ....
SQLitePartial indexes with CREATE INDEX ... WHERE ..., including unique partial indexes.
SQL ServerCalls them filtered indexes and also uses a WHERE predicate on a non-clustered index.
MySQL 8.4The documented grammar of CREATE INDEX does not include a clause WHERE; do not copy this pattern as though it were portable.