WIPIVERSE All articles

Expression index

Expression index (also known as a function-based index) is a type of database index in computing and computer science that is built on a generic expression or the result of a function, rather than directly on one or more raw column values. This allows indexes to be defined for common query conditions that depend on data in a table but are not actually stored in that table in the exact form being queried.

Overview

When a standard database index is created on a column, queries that search using that column directly can benefit from the index. However, many queries apply functions or transformations to column values — for example, converting text to lowercase, extracting a substring, or performing arithmetic. An expression index precomputes the result of such an expression and stores it in the index structure, enabling the database to perform efficient lookups without scanning the entire table.

Common Use Case: Case-Insensitive Searching

A typical application is supporting case-insensitive searches while preserving the original case of the stored data. For example:

CREATE INDEX users__last_name_lower ON users( lower( last_name ) );

This creates an index on the lowercased representation of the last_name column. Queries that use the same expression can then utilize the index:

SELECT user_id FROM users WHERE lower( last_name ) = lower( 'Smith' );

Database Support

Major relational database management systems that support expression indexes include:

  • IBM Db2 (since version 10.5)
  • Oracle Database (since release 8i)
  • PostgreSQL (since at least version 7)
  • CockroachDB (supports expression indexes)

Key Characteristics

  • The index stores the evaluated result of the expression, not the original column values.
  • Queries must use the exact same expression as defined in the index for the database query optimizer to consider using the index.
  • Expression indexes can be created on scalar expressions, function results, or extracted values (e.g., from JSON data using operators like data->>'name').
  • They can also be used to enforce uniqueness constraints on transformed values (e.g., ensuring unique case-insensitive email addresses).
Browse

More topics to explore

    Browse all articles