// dialect comparison

Indexes on expressions like LOWER(col) in every SQL dialect

An expression index stores a function result instead of the raw column, which makes WHERE LOWER(email) = ? use an index.

Filed under 인덱스

The verdict

Three engines do it inline, MySQL does it with double parens, SQL Server makes you add a computed column first. All five can do it; the query must repeat the expression exactly in every one of them.

Where they differ

PostgreSQL, SQLite and Oracle index expressions directly. MySQL 8.0.13+ accepts parenthesized functional key parts. SQL Server needs a detour: a persisted computed column that you then index.

How each engine writes it

SQL Server

ALTER TABLE users ADD email_lower AS LOWER(email) PERSISTED;
CREATE INDEX idx_users_email_lower ON users (email_lower);

No direct expression indexes. Add a computed column (PERSISTED is optional when the expression is deterministic) and index it. The optimizer matches WHERE LOWER(email) = ... to the computed column, so queries need not name it. The session needs QUOTED_IDENTIFIER ON.

PostgreSQL

CREATE INDEX idx_users_email_lower ON users (LOWER(email));

Direct support. The expression must be immutable, and the query must repeat the exact expression to use the index.

MySQL

CREATE INDEX idx_users_email_lower ON users ((LOWER(email)));

Functional key parts since 8.0.13; note the double parentheses. Older versions need a generated column + index. Queries must repeat the expression.

SQLite

CREATE INDEX idx_users_email_lower ON users (LOWER(email));

Expression indexes since 3.9.0. The query must spell the expression identically to use it.

Oracle

CREATE INDEX idx_users_email_lower ON users (LOWER(email));

Function-based indexes, available since 8i. The function must be deterministic; gather statistics afterwards so the optimizer can cost the hidden virtual column.

Questions that come up

Why does my expression index not get used?

The WHERE clause must contain the same expression. WHERE email = 'x' will not use an index on LOWER(email); the query has to say LOWER(email) = 'x'.

Does lower() beat a case-insensitive collation?

Not necessarily. CI collations in SQL Server, MySQL and Oracle make plain indexes case-insensitive without expressions; lower() is the portable route that behaves identically everywhere.

Any restrictions on the expression?

It must be deterministic/immutable. LOWER, SUBSTR and arithmetic are fine; anything depending on session state or time is not.

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.