Quick revision
The short answer
The index is on lower(email), but Spring Data JPA builds IgnoreCase queries with upper() on both sides, and Hibernate renders that as upper(u1_0.email) = upper(?). An expression index is used only when the query contains the exact same expression, so PostgreSQL scans the table. We fix it by indexing upper(email), writing an explicit @Query with lower(), storing a normalised email column, or using citext or a case-insensitive collation, and we pin the choice with a test.
What to remember
In this answer, we saw that findByEmailIgnoreCase produces upper(email) = upper(?), so an index on lower(email) never matches and PostgreSQL scans the table. Expression indexes must match the exact expression in the SQL. We fixed it by indexing upper(email), by writing an explicit lower() query or storing a normalised column, or by using citext or a case-insensitive collation so plain equality works. A plan test keeps the index and the generated SQL in agreement. The general lesson is to review the SQL the ORM emits, not the repository method name.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The index is on lower(email), but Spring Data JPA builds IgnoreCase queries with upper() on both sides, and Hibernate renders that as upper(u1_0.email) = upper(?). An expression index is used only when the query contains the exact same expression, so PostgreSQL scans the table. We fix it by indexing upper(email), writing an explicit @Query with lower(), storing a normalised email column, or using citext or a case-insensitive collation, and we pin the choice with a test.
Why this question is asked
This is one of the most common gaps between how we think an ORM behaves and what it actually sends. The repository method name reads as 'find by email, ignore case', we created an index for case-insensitive search, and yet the endpoint is slow. Interviewers use it to see whether we debug from the generated SQL rather than from the Java code. In this answer, we will recap how expression indexes are matched, reproduce the problem, look at why the SQL uses upper(), walk through three fixes and the test that keeps the fix in place.
1A Quick Recap of Expression Index Matching
An expression index stores the result of an expression, such as lower(email), for every row. When a query arrives, the planner, the part of the database that picks a plan, looks for indexes whose expression appears in the WHERE clause. The match is structural: lower(email) matches lower(email), but upper(email) is a different function, even though, for most text, both give a case-insensitive comparison. PostgreSQL does not reason that the two are equivalent, and for some characters they are not, so it never substitutes one for the other.
2Reproducing the Problem
Here is the repository method and the SQL it produces, as shown with spring.jpa.show-sql or the org.hibernate.SQL logger.
5 messages in order. Play them, or step through them one at a time.
So the index on lower(email) exists, but the plan is a sequential scan of 10 million rows. The reason is the expression: Spring Data's query derivation implements IgnoreCase by wrapping both the property and the parameter in upper() through the JPA criteria or JPQL it builds, and Hibernate translates that into the database's upper() function. Nothing in the method name hints at this choice. The lesson is to look at the generated SQL and the plan, never only at the method name.
3Fix 1: Index the Expression the ORM Emits
The smallest change is to match the SQL instead of fighting it: build the index on upper(email). If uniqueness must be case-insensitive, make this index the unique one and drop the lower() index, so we do not maintain two.
The risk with this fix is that it depends on an implementation detail of the framework. A future version, a different dialect, or a developer who switches the method to a hand-written query with lower() could silently bring the sequential scan back. That is why we pin it with a test, shown below.
4Fix 2: Write the Query Ourselves or Normalise the Data
If we prefer lower(), we write the query explicitly so the SQL is under our control. Even better, we store the email already normalised in its own column and compare plainly, which needs no expression index and no function call on every row.
Notice Locale.ROOT in the normalisation. Calling toLowerCase() with the default locale gives different results on a server with a Turkish locale, where uppercase I becomes a dotless i. Normalising in Java once, in one place, also makes the rule easy to test.
5Fix 3: Let PostgreSQL Compare Without Functions
In PostgreSQL we can make the column itself case-insensitive. The citext extension gives a type whose equality ignores case, and a nondeterministic ICU collation does the same for text. Then the repository method can be a plain findByEmail with ordinary equality and an ordinary unique index, so the ORM has no function to add. Check your version for how LIKE and pattern matching behave with nondeterministic collations before relying on them.
6Pinning the Fix With a Test
Whichever fix we choose, we add a test against a real PostgreSQL, for example with Testcontainers, that runs EXPLAIN for the query the repository sends and fails if the plan contains a sequential scan. A cheaper variant asserts the logged SQL string. Either way, a future change to the framework or to the query surfaces in the build, not in production.
7Follow-up questions
Why does Spring Data use upper() rather than lower()?
It is simply the implementation choice in its query derivation, and both are standard JPA functions. Upper-casing is a long-standing convention for case-insensitive comparison. Since the choice is not part of any contract we control, the safe approach is to verify the generated SQL in a test rather than assume it.
Could we keep both indexes, on lower() and upper()?
We could, but each index costs writes, storage and memory, and two unique indexes that mean almost the same thing invite confusion. It is better to pick one expression, make every query use it, and drop the other.
8Conclusion
In this answer, we saw that findByEmailIgnoreCase produces upper(email) = upper(?), so an index on lower(email) never matches and PostgreSQL scans the table. Expression indexes must match the exact expression in the SQL. We fixed it by indexing upper(email), by writing an explicit lower() query or storing a normalised column, or by using citext or a case-insensitive collation so plain equality works. A plan test keeps the index and the generated SQL in agreement. The general lesson is to review the SQL the ORM emits, not the repository method name.
Open with
The index is on lower of email, but the SQL is upper of email equals upper of the parameter. Spring Data implements IgnoreCase with upper on both sides and Hibernate renders that, and PostgreSQL only uses an expression index when the query contains exactly the same expression, so it falls back to a sequential scan.
Then, if they want more
I would confirm it by taking the logged SQL and running EXPLAIN ANALYZE on it. For the fix there are three options. The quickest is to create the index on upper of email, concurrently, and drop the lower one so we do not keep two.
The more explicit option is a hand-written @Query with lower on both sides, or better, a normalised email column that we fill with strip and toLowerCase with Locale.ROOT on save, and then a plain unique index and plain equality. In PostgreSQL, a third option is citext or a nondeterministic collation, so equality is case-insensitive without functions at all.
Whichever I pick, I would add a Testcontainers test that runs EXPLAIN for the repository's query and fails on a sequential scan, because the expression depends on framework behaviour. The lesson I take from it is to review the generated SQL, not the method name.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A Spring Data JPA query findByEmailIgnoreCase(String email) is slow even though you...": https://scrivio-interview-library.netlify.app/databases/indexing/3e0cd953d8/