Full answer
Typical mismatches:
- Function on the column:
WHERE lower(email) = 'a@x.com'cannot use an index onemail. Create an index onlower(email)or store emails normalised. - Implicit cast: comparing a text column with a number, or a varchar with a parameter typed differently by a driver, may cast the column side.
- Leading wildcard:
LIKE '%@x.com'cannot use a B-tree; a trigram index (PostgreSQLpg_trgm) can. - LIKE prefix in PostgreSQL with a non-C collation needs a
text_pattern_opsindex. - In SQLite,
LIKEis case-insensitive by default and only uses an index under specific collation settings. - Low selectivity or a tiny table: the planner may prefer a scan legitimately.
Use EXPLAIN to see the chosen plan and test the rewritten predicate; the fix is whichever form lets the bare indexed expression appear on one side of the comparison.
Key points
- Functions on the column block plain indexes
- Expression indexes match function predicates
- Implicit casts can disable index use
- Leading wildcards need trigram indexes
- Small tables may scan legitimately
Common mistakes
- Adding a second identical index
- Ignoring driver parameter types
Follow-ups
- How would you enforce case-insensitive unique emails?
- What is a sargable predicate?
Tips
- Check the predicate against the index definition