Quick revision
The short answer
IgnoreCase makes Spring Data JPA compare upper(email) with upper(?), and a plain B-tree index on email stores the original values, so it cannot be used for that expression and PostgreSQL scans all 10 million rows. We confirm it by logging the SQL and running EXPLAIN ANALYZE, which shows a sequential scan, and fix it with an index on the same expression, a case-insensitive column type such as citext, or by storing a normalised email and querying it directly.
What to remember
In this answer, we saw that findByEmailIgnoreCase generates upper(email) = upper(?), which a plain index on email cannot serve, so the database scans every row. Logging the SQL and running EXPLAIN ANALYZE confirms the sequential scan. We fix it with an expression index matching upper(email), a citext column with findByEmail, or a normalised column queried directly, then check the new plan and bind parameters, remembering that functions on indexed columns hide them from ordinary indexes.
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.
IgnoreCase makes Spring Data JPA compare upper(email) with upper(?), and a plain B-tree index on email stores the original values, so it cannot be used for that expression and PostgreSQL scans all 10 million rows. We confirm it by logging the SQL and running EXPLAIN ANALYZE, which shows a sequential scan, and fix it with an index on the same expression, a case-insensitive column type such as citext, or by storing a normalised email and querying it directly.
Why this question is asked
This question looks like a Spring Data question, but it is really about how indexes work. The method name reads naturally, the index exists, and yet each login takes 2 seconds. Interviewers ask it because the same mistake appears everywhere: any function applied to an indexed column hides it from the index. In this answer, we will recap how a B-tree index is used, see what SQL the derived query generates, confirm the problem with EXPLAIN ANALYZE, and go through three fixes and how to check each one.
1A Quick Recap of How an Index Is Used
A B-tree index on email is a sorted structure of the email values, each pointing to its row. For WHERE email = 'ada@example.com', the database walks down the tree to that value in a few page reads, even with 10 million rows. The index helps because the database can compare the search value directly with the stored, sorted values.
Now consider WHERE upper(email) = 'ADA@EXAMPLE.COM'. The index stores email, not upper(email), and the sort order of the original values says nothing useful about where the upper-cased value would be. So the database cannot use the index and has to compute upper(email) for every row, which is a full sequential scan.
2What SQL the Derived Query Generates
Spring Data builds a query from the method name. For findByEmailIgnoreCase, it creates a JPQL comparison that wraps both sides in upper(), and Hibernate turns that into SQL. Logging the SQL shows it directly.
Notice that the comparison uses upper(), not lower(). This detail matters for the fix: an expression index only helps when it matches the exact expression in the query.
3Confirming It With EXPLAIN ANALYZE
EXPLAIN ANALYZE runs the query and shows the plan the database chose, with real timings. We run it with the SQL from the log and a real value.
The first plan reads every row and throws away 9,999,999 of them, which is our 2 seconds. The second plan, with no function on the column, uses the index in a fraction of a millisecond. Together they prove that the function, not the index, is the problem.
4Three Ways to Fix It
The first fix is an expression index that matches the generated expression. Since Spring Data uses upper(), we index upper(email). If we prefer lower(), we must also write the query with lower(), for example with @Query, because an index on lower(email) does nothing for upper(email).
The second fix is a case-insensitive column type. In PostgreSQL, the citext extension provides a text type whose comparisons ignore case, so WHERE email = ? is case-insensitive and can use an ordinary index on the column. We then switch the method to findByEmail, because keeping IgnoreCase would wrap the column in upper() again. A nondeterministic ICU collation is another option in recent PostgreSQL versions.
The third fix is to normalise on write: store the email lower-cased (or keep a separate normalised column), and lower-case the input before querying. The query becomes a plain equality on an indexed column, which is the cheapest and most portable option, and it also lets a unique index stop two accounts with the same email in different cases.
| Fix | Query | Notes |
|---|---|---|
Expression index on upper(email) | keep findByEmailIgnoreCase | must match the generated expression exactly |
| citext column | findByEmail | PostgreSQL only; ALTER rewrites the table |
| Normalised column | findByEmailNormalized(lower) | portable, enables a case-insensitive unique index |
Notice CONCURRENTLY: on a live 10 million row table, it builds the index without blocking writes, at the cost of taking longer. And Locale.ROOT avoids surprises such as the Turkish dotless i, where toLowerCase() with a Turkish default locale gives a different character.
5Checking the Result
After the change, we run EXPLAIN ANALYZE again with the SQL the application actually sends and expect an Index Scan or Index Only Scan with a time well under a millisecond. We also check that the query uses a bind parameter, the ? in the logged SQL, rather than a string built into the query, so the database can reuse plans and we are safe from SQL injection. Derived queries always use bind parameters, but a hand-written native query may not.
The general lesson is worth saying in the interview: wrapping an indexed column in a function, such as upper(), date(created_at) or a cast, prevents the index from being used unless an index exists on that exact expression. The same applies to implicit casts, for example comparing a varchar column with a numeric parameter.
6Follow-up questions
Why does Spring Data use upper() and not lower()?
It is simply the convention its query derivation uses; either would work for comparison. What matters is that any expression index or hand-written query matches whichever function is actually generated, which we confirm from the SQL log.
Would a LIKE query with IgnoreCase have the same problem?
Yes. findByEmailContainingIgnoreCase generates upper(email) like upper(?) with wildcards on both sides, and a leading wildcard prevents a normal B-tree index from being used even without the function. That needs a trigram index (pg_trgm) or a search engine.
Is it safe to add an index on a large live table?
In PostgreSQL, CREATE INDEX CONCURRENTLY avoids blocking writes, but it takes longer, cannot run inside a transaction, and can leave an invalid index if it fails, which we must check for and drop. Migration tools need to be told not to wrap it in a transaction.
7Conclusion
In this answer, we saw that findByEmailIgnoreCase generates upper(email) = upper(?), which a plain index on email cannot serve, so the database scans every row. Logging the SQL and running EXPLAIN ANALYZE confirms the sequential scan. We fix it with an expression index matching upper(email), a citext column with findByEmail, or a normalised column queried directly, then check the new plan and bind parameters, remembering that functions on indexed columns hide them from ordinary indexes.
Open with
The IgnoreCase keyword makes Spring Data generate where upper(email) = upper(?). The index on email stores the original values, so it cannot be used for the upper of the column, and PostgreSQL has to compute upper for all 10 million rows, which is a sequential scan of about 2 seconds.
Then, if they want more
I would confirm it by turning on Hibernate's SQL logging to see the generated query, and then running EXPLAIN ANALYZE with that SQL; I expect Seq Scan with almost ten million rows removed by the filter, while a plain email equality shows an Index Scan. For the fix, there are three options.
I can create an expression index on upper(email), concurrently on a live table, and it has to match the exact function the query uses. I can change the column to citext so equality is case-insensitive and switch to findByEmail.
Or, my usual preference, I store a normalised lower-case email with a unique index and query it with the input lower-cased using Locale.ROOT, which is portable and also prevents duplicate accounts that differ only in case. Afterwards I check the plan again and confirm the query uses bind parameters.
The general rule is that a function on an indexed column hides it from the index unless the index is on that expression.
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
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
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 method findByEmailIgnoreCase(String email) takes 2 seconds on a...": https://scrivio-interview-library.netlify.app/databases/database-scaling/e683e58b77/