How I Made My Queries 40% Faster: Indexing and JOIN Optimization in Spring Data JPA
Measure first, then fix: spotting N+1 queries, writing one aggregate query instead of many, and designing composite indexes that MySQL can actually use.
In Votezy, election results are aggregated per candidate in real time. Working on indexed queries and JOIN optimization in Spring Data JPA cut that query time by about 40%. This post explains the techniques behind that kind of improvement, using a small voting schema, so you can apply them to your own app.
Step 0: measure before you optimise
You can't improve what you can't see. JPA hides SQL, so the first job is to make it visible:
yaml
# application-dev.ymllogging: level: org.hibernate.SQL: debug # every SQL statement org.hibernate.orm.jdbc.bind: trace # the bound parameter valuesspring: jpa: properties: hibernate.generate_statistics: true # query counts and timings per session
Then, for any slow query, ask MySQL how it plans to run it:
sql
EXPLAIN ANALYZESELECT candidate_id, COUNT(*) FROM vote WHERE election_id = 12 GROUP BY candidate_id;
EXPLAIN ANALYZE (MySQL 8.0.18+) actually runs the query and shows the real time spent in each step.
The example schema
election (id, title, status)a few rows
candidate (id, election_id, name)tens of rows per election
vote (id, election_id, candidate_id, voter_id, created_at)grows fast: the hot table
Problem 1: the N+1 query
The first version of a results endpoint often looks like this:
c.getVotes() is a lazy collection, so Hibernate runs one more query per candidate and loads every vote row into memory just to count them. With 20 candidates that's 21 queries and potentially millions of entities. This is the classic N+1 problem, and the SQL log makes it obvious: the same select ... from vote where candidate_id=? repeated over and over.
Fix 1: let the database aggregate (one query, DTO projection)
Counting is what databases are good at. Ask for exactly the answer, not the raw rows:
java
public record CandidateTally(Long candidateId, String name, Long votes) {}public interface VoteRepository extends JpaRepository<Vote, Long> { @Query(""" select new com.example.votezy.results.CandidateTally(c.id, c.name, count(v.id)) from Candidate c left join Vote v on v.candidateId = c.id where c.electionId = :electionId group by c.id, c.name order by count(v.id) desc """) List<CandidateTally> tally(@Param("electionId") long electionId);}
One round trip instead of N+1.
No entities are created, so there's no persistence-context overhead. The constructor expression builds the record directly.
left join keeps candidates with zero votes in the result.
When you do need entities with their associations, fetch them in the same query with join fetch or @EntityGraph:
Even with one query, MySQL still has to find the votes for election 12. Without a suitable index it reads the whole table:
code
-> Table scan on vote (rows=1000000) ← reads everything -> Filter: (vote.election_id = 12) -> Aggregate using temporary table ← extra work for GROUP BY
A composite index on the columns the query filters by, then groups by fixes both problems:
sql
CREATE INDEX idx_vote_election_candidate ON vote (election_id, candidate_id);
code
-> Group aggregate: count(0) -> Covering index lookup on vote using idx_vote_election_candidate (election_id=12) ← only this election
Two things improved:
Index lookup instead of a table scan: only election 12's entries are read.
Covering index: everything the query needs (election_id, candidate_id) is in the index. MySQL never touches the table rows, and because the index is already sorted by candidate_id, the GROUP BY needs no temporary table.
The leftmost-prefix rule
A composite index (election_id, candidate_id) is like a phone book sorted by last name, then first name:
Query filters on
Can use the index?
election_id
✅ yes
election_id and candidate_id
✅ yes
candidate_id only
❌ no (it's not the leftmost column)
So column order matters: put the column you filter on with = first.
Declare indexes alongside the entity so the intent is visible in the code, and create them for real in a migration:
Paginate with a key, not a big OFFSET.OFFSET 100000 still walks past 100,000 rows. "Keyset" pagination (WHERE id > :lastSeenId ORDER BY id LIMIT 50) jumps straight there using the primary key.
Don't over-index
Every index speeds up reads but slows down writes: each INSERT into vote must also update every index on it. For a write-heavy table like votes, add indexes for the queries you actually run, check them with EXPLAIN, and drop the ones nothing uses.
The checklist I now follow
Turn on SQL logging and count the queries per request.
Kill N+1 with aggregate queries, projections, join fetch or entity graphs.
EXPLAIN ANALYZE the slow ones and look for table scans and temporary tables.
Add a composite index matching WHERE (equality first), then GROUP BY / ORDER BY.
Aim for covering indexes on hot read paths.
Measure again. Keep the change only if the numbers improved.