// dialect comparison

Partial (filtered) indexes in every SQL dialect

A partial index covers only rows matching a WHERE clause, keeping it small and fast when you always filter the same way.

Filed under インデックス

The verdict

PostgreSQL, SQLite and SQL Server index a row subset directly; MySQL cannot, and Oracle fakes it with a CASE-based function index. If hot-queue queries dominate your workload, this is one of the most effective size reducers available.

Where they differ

Three engines take the WHERE clause directly on CREATE INDEX. MySQL has no partial indexes; Oracle gets close with a function-based index that indexes NULL for the rows you do not care about.

How each engine writes it

SQL Server

CREATE INDEX idx_orders_pending ON orders (created_at) WHERE status = 'pending';

Called a filtered index. The WHERE clause allows only simple comparisons (=, <>, <, >, IN, IS NULL) joined by AND: no OR, LIKE or functions. A parameterized query (WHERE status = @s) usually cannot use it, because the plan must work for any value. Creating it requires SET QUOTED_IDENTIFIER ON, which sqlcmd leaves off unless you pass -I.

PostgreSQL

CREATE INDEX idx_orders_pending ON orders (created_at) WHERE status = 'pending';

Any immutable predicate is allowed, including expressions over several columns. Typical uses: an open-orders queue, or WHERE deleted_at IS NULL on soft-deleted tables.

MySQL

CREATE INDEX idx_orders_status ON orders (status, created_at);

No partial indexes. The nearest alternative is a composite index led by the filter column; it still holds an entry for every row, whatever its status.

SQLite

CREATE INDEX idx_orders_pending ON orders (created_at) WHERE status = 'pending';

Partial indexes supported since SQLite 3.8.0, with a documented set of restrictions on the WHERE clause.

Oracle

CREATE INDEX idx_orders_pending ON orders (CASE WHEN status = 'pending' THEN created_at END);

No partial indexes. A function-based index stores NULL for non-matching rows (NULLs are not indexed), so it behaves like a partial index; queries must repeat the CASE expression.

Questions that come up

Why use a partial index instead of a normal one?

It is much smaller, since it only stores the rows you query, and writes to other rows do not touch it at all.

Why is my partial index not being used?

The query predicate has to imply the index predicate. WHERE status = 'pending' uses it; WHERE status IN ('pending') may or may not depending on the engine, and mismatched literals never will.

Can the WHERE clause reference other columns?

Yes, any column of the table. PostgreSQL and SQLite also allow deterministic expressions; SQL Server allows only simple comparisons joined by AND. MySQL has no partial indexes, and Oracle's CASE trick can reference any column inside the CASE.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.