API performance Level 2 This level · 5 of 20
Medium java backend

Don't wrap indexed columns in functions in WHERE

The 30 second answer

If I wrap an indexed column in a function in the WHERE clause, like lowercasing an email, a normal index on that column cannot be used and the database scans the table. I either rewrite the condition so the column stays bare, for example using a date range instead of extracting the date, or I create an expression index that matches the function exactly.

Could you say this in an interview?

An index on email can't serve WHERE lower(email) = ?. Either rewrite the condition or create an index on the expression.

CREATE INDEX ON users (lower(email));
-- or rewrite: WHERE created_at >= '2026-01-01' AND created_at < '2026-01-02' instead of date(created_at) = ...
Sources (2)
  1. PostgreSQL 17: Indexes on Expressionspostgresql.org
  2. Use The Index, Luke: Functionsuse-the-index-luke.com
Previous
Next