Pcornel
Databases · Indexes & Query Plans

Partial and Expression Indexes

Cheat sheet · https://pcornel.dk/topic.php?topic=6198

Back to the Topic

Answer

Partial indexes cover only rows matching a condition; expression indexes index the result of an expression such as lower(email). (PostgreSQL; MySQL has functional indexes in 8.0.)

Remember

The query must use the same condition or expression for the index to be used. Partial indexes save space for skewed data (e.g. WHERE active).

First checks

  • CREATE INDEX ON users (lower(email)); — first check for this topic
  • psql -c "SELECT now(), version();" — PostgreSQL reachable and version
  • mysql -e "SELECT NOW(), VERSION();" — MariaDB/MySQL reachable and version
  • sqlcmd -S <server> -Q "SELECT @@VERSION" — SQL Server reachable and version

Verify in this order

index on subset/expression → matching query predicates → index scan

Where it usually breaks

A query written as LOWER(email) with different casing rules or a cast does not match.

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=6198

Confirmation

Confirm action

Continue?