Quick revision
The short answer
I would work from the outside in. Traces and metrics tell me whether the extra 850 ms is spent in JDBC calls at all; a statement count per request, from Hibernate statistics or datasource-proxy, tells me whether the release added queries, such as an N+1 pattern; slow query logs point to individual slow statements. Then on the database, pg_stat_statements shows which statements changed in calls or mean time, and EXPLAIN (ANALYZE, BUFFERS) on the suspect query, compared with the plan before the release, shows whether the cause is a missing index, different parameters or a new join.
What to remember
In this answer, we traced a 50 ms to 900 ms regression from the application to the database. Traces told us whether the time was in JDBC and whether the number of statements changed, statement counting from Hibernate statistics or datasource-proxy exposed N+1 patterns, and slow query logs found individually slow statements. On the database, pg_stat_statements showed which statement changed and EXPLAIN (ANALYZE, BUFFERS) showed why, and comparing with the old plan pointed to a missing index, changed parameters or a new join.
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.
I would work from the outside in. Traces and metrics tell me whether the extra 850 ms is spent in JDBC calls at all; a statement count per request, from Hibernate statistics or datasource-proxy, tells me whether the release added queries, such as an N+1 pattern; slow query logs point to individual slow statements. Then on the database, pg_stat_statements shows which statements changed in calls or mean time, and EXPLAIN (ANALYZE, BUFFERS) on the suspect query, compared with the plan before the release, shows whether the cause is a missing index, different parameters or a new join.
Why this question is asked
A latency regression after a release is one of the most common production problems, and the database is a frequent cause. But jumping straight to EXPLAIN on a guessed query often wastes hours, because the problem may be a hundred fast queries rather than one slow one, or not the database at all. Interviewers ask this to see a methodical path from symptom to cause. In this answer, we will narrow the problem down with traces, count statements per request, find individually slow statements, move to the database with pg_stat_statements and EXPLAIN, and finally compare plans from before and after the release.
1Is the Time Really in the Database?
The first question is where the 850 extra milliseconds go. A distributed trace of one slow request, for example from OpenTelemetry or Micrometer Tracing with JDBC instrumentation, shows a span per database call alongside HTTP calls and the rest of the work. Comparing a trace from before and after the release usually answers the question in a minute. If tracing is not available, the Hikari metric for connection usage time and a timer around the service method give a rough split.
Notice the two numbers that changed: the count went from 3 to 204, and the time went with it. That already suggests an N+1 pattern rather than one slow query. If the count had stayed at 3 but one span had grown from 10 ms to 800 ms, we would be looking for a slow statement instead. If the JDBC time had not changed at all, the database is not the cause and we look elsewhere.
2Counting Statements Per Request
To confirm and locate the extra queries, we count statements per request in a test or staging environment. Hibernate statistics report the number of statements per session, and datasource-proxy can log each statement with a query count per request. Running the endpoint once with SQL logging on shows the pattern directly.
This is the N+1 signature: one query for the list, then one per row for each association the new code touches. Usually the release added a field to the response, such as the customer name, that the mapper reads from a lazy association. The fix is in the query: a fetch join, an entity graph, a batch fetch size, or a DTO projection that selects the needed columns in one statement. We add a test that asserts the statement count, so the regression cannot come back silently.
3Finding Individually Slow Statements
If the count is unchanged, one or two statements became slower. The application can log statements above a threshold: Hibernate 6.2 and later have a slow query log setting in milliseconds (check your version for the property and logger name), and datasource-proxy has a slow query listener. On the database side, PostgreSQL's log_min_duration_statement logs every statement slower than a threshold from any client, with its duration.
4Pg_stat_statements and EXPLAIN on the Database
pg_stat_statements is a PostgreSQL extension that aggregates every normalised statement: calls, total and mean time, rows and buffer usage. Sorting by total time shows what the database spends its time on, and comparing calls or mean time with a snapshot from before the release shows exactly which statement changed. Then we run EXPLAIN (ANALYZE, BUFFERS) on the suspect query with realistic parameter values. ANALYZE really executes the query and reports actual times and row counts per plan node; BUFFERS shows how many pages came from memory and how many from disk.
The plan explains the time: a sequential scan reads 4 million rows from disk, mostly read rather than hit, to keep 58,000. EXPLAIN ANALYZE runs the query for real, so for a slow update or delete we wrap it in a transaction and roll back, or run it on a replica.
5Comparing the Plan Before and After
The last step explains why the plan changed with the release. We compare the new plan with the old one, from a staging run of the previous version, from auto_explain logs if enabled, or by reasoning from the query diff. Three causes are common. A missing index: the release added a filter on region, and no index covers status and region together, so the planner falls back to a sequential scan. Changed parameters: the new code passes a value with very different selectivity, or a type that does not match the column, so an index cannot be used. A new join: the query now joins a large table, sometimes added by an entity mapping change rather than by the query itself.
- Compare traces before and aftertime in JDBC spans, number of spans
- Statement count went up?YesN+1: count statements, fix the fetch planNoFind the slow statement
- Slow query log, pg_stat_statementswhich statement changed in mean time
- EXPLAIN (ANALYZE, BUFFERS)actual rows, scans, disk reads
- Compare with the old planmissing index, parameters, new join
- Fix and verify with the same tools
6 steps in order. Play them, or step through them one at a time.
From 790 ms to under 1 ms. We confirm the fix the same way we found the problem: the trace shows the endpoint back near 50 ms and pg_stat_statements shows the statement's mean time drop.
6Follow-up questions
What if traces show the time in JDBC but the database says the query is fast?
Then the time is spent between the two: waiting for a connection from the pool, network latency, or fetching a large result over the wire. Check Hikari's acquire time and pending threads, and the number of rows and bytes returned; a query that returns 50,000 rows can be fast in the database and slow in the application.
Why might the same query have a different plan in production than in staging?
The planner uses table statistics, so different data volumes and distributions produce different plans. Stale statistics after a big data change can also mislead it; running ANALYZE on the table refreshes them. Always compare with production-like data.
7Conclusion
In this answer, we traced a 50 ms to 900 ms regression from the application to the database. Traces told us whether the time was in JDBC and whether the number of statements changed, statement counting from Hibernate statistics or datasource-proxy exposed N+1 patterns, and slow query logs found individually slow statements. On the database, pg_stat_statements showed which statement changed and EXPLAIN (ANALYZE, BUFFERS) showed why, and comparing with the old plan pointed to a missing index, changed parameters or a new join.
Open with
I would go from the outside in. First I compare a trace of the endpoint before and after the release, with JDBC spans. That tells me whether the extra time is in the database at all, and whether it is more statements or slower statements.
Then, if they want more
If the span count jumped from 3 to 200, it is almost certainly N+1, usually from a new field the mapper reads through a lazy association. I confirm with Hibernate statistics or datasource-proxy counting statements per request, fix it with a fetch join, entity graph or projection, and add a test asserting the statement count.
If the count is the same, I look for the slow statement, with Hibernate's slow query log or PostgreSQL's log_min_duration_statement, and pg_stat_statements sorted by total time, comparing mean time with before the release. Then I run EXPLAIN ANALYZE BUFFERS on that query with realistic parameters, which shows actual rows, scans and disk reads per node.
Finally I compare the plan with the previous one. The usual causes are a missing index for a new filter, parameters with different selectivity or a type mismatch that disables an index, or a new join. I would fix it, for example with an index created concurrently, and verify with the same trace and statistics.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Hibernate 6
- 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 "An endpoint's latency rose from 50 ms to 900 ms after a release. Describe how you would...": https://scrivio-interview-library.netlify.app/databases/jpa-performance/22167b4f17/