Introduction
Fixing N+1 queries (see the previous post) gets your Hibernate app down to a handful of queries per request. The next bottleneck is what each of those queries costs once your tables have millions of rows — and that is almost always a question of indexing.
An index turns "scan every row" into "look it up directly." Get the index wrong — or skip it — and a query that took 2ms in development takes 4 seconds in production once real data volume shows up.
How Indexes Work: The B-Tree Intuition
Without an index, a WHERE clause forces a sequential scan: the database reads every row and checks the condition. That's O(n) — cost grows linearly with table size.






