Before continuing: This entry is part of the indexes guide. If you are new to the topic, start with INDEX.
Example
On the teaching database, the following index and query were tested inside a transaction and rolled back after the test:
CREATE INDEX idx_customers_email_lower_demo
ON customers(lower(email));
EXPLAIN QUERY PLAN
SELECT id, name, email
FROM customers
WHERE lower(email) = 'amy@example.test';The plan confirms that SQLite recognises the index for the expression being queried. The query returns Amy Wilson with amy@example.test.
How it is used
An ordinary index on email holds the column's value. If your condition always applies lower(email), the expression being queried is no longer simply email. A functional index lets you index that derived form.
It is not an automatic improvement. Every index takes up space and has to be maintained on inserts and updates. Create one because a real, frequent query needs it, not because there is a function you could index.
Differences and trade-offs
The query's expression must match the indexed one
SQLite documents a particularly strict rule: the planner does not do algebra to prove that two equivalent expressions are the same. If you index x + y and query y + x, it may not use the index even though the sum is mathematically equivalent.
In practice, keep indexed expressions in one stable form and check the real plan with EXPLAIN or your engine's equivalent tool.
Creating the index and assuming every variant will use it
An available index does not force the optimiser to choose it. Besides the expression, selectivity, table size, statistics and estimated cost all play a part. Check the query that matters.
Differences between engines
- PostgreSQL:
CREATE INDEXaccepts expressions and requires the functions and operators in the definition to be immutable. - SQLite: allows indexes on deterministic expressions and requires a very close match between the index expression and the query expression.
- MySQL 8.4: supports functional key parts; the expression goes inside an extra pair of parentheses, for example
CREATE INDEX idx ON table_name ((LOWER(column_name))), and it inherits the restrictions of generated columns. - SQL Server: the usual pattern is to define a computed column that meets the determinism and precision requirements, and to index that column.
So «functional index» describes a shared intent, but the exact syntax is not portable across the four engines.
When it is worth it
- Frequent filters on
LOWER(column_name)or another stable normalisation. - Repeated sorting by a computed expression.
- Uniqueness rules on a transformation, when the engine allows it and the meaning is well defined.
Do not confuse it with a composite index, which combines several key columns, or with a partial/filtered index, which includes only the rows that satisfy a predicate. An index can combine several of these ideas, but each one solves a different problem.