Quick revision
The short answer
The SQL log only measures statement execution, so the missing 1.9 seconds is spent elsewhere: waiting for a pool connection, running many small lazy-loading queries, serializing a large response, logging synchronously, calling other services, or pausing for GC or CPU throttling. To find it, we look at a distributed trace of a slow request, including the gaps between spans, profile in wall-clock mode with async-profiler or JFR so that waiting time is visible, check hikaricp.connections.acquire, look for N+1 loading and heavy JSON, and line up GC pauses and CPU throttling with the slow periods.
What to remember
In this answer, we saw that a 100 ms SQL total cannot explain a 2-second p95, because the SQL log measures only execution. A trace of a slow request shows spans and the gaps between them, a wall-clock profile with async-profiler or JFR reveals the waiting that CPU profiles miss, hikaricp.connections.acquire exposes pool waits, and the usual suspects, N+1 loading, large JSON, synchronous logging, remote calls, GC pauses and CPU throttling, account for the rest.
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 SQL log only measures statement execution, so the missing 1.9 seconds is spent elsewhere: waiting for a pool connection, running many small lazy-loading queries, serializing a large response, logging synchronously, calling other services, or pausing for GC or CPU throttling. To find it, we look at a distributed trace of a slow request, including the gaps between spans, profile in wall-clock mode with async-profiler or JFR so that waiting time is visible, check hikaricp.connections.acquire, look for N+1 loading and heavy JSON, and line up GC pauses and CPU throttling with the slow periods.
Why this question is asked
This is one of the most instructive performance puzzles, because it breaks a common assumption: that a slow endpoint means slow queries. The database looks innocent, the code looks simple, and still a p95 of 2 seconds. Interviewers like it because the answer shows whether we know what the usual measurement tools do not see. In this answer, we will see why the SQL log hides most of the time, read a trace of a slow request, use a wall-clock profiler, check the connection wait, and go through the usual suspects one by one.
1Why the SQL Log Hides Most of the Time
When Hibernate or a JDBC proxy logs that a statement took 12 ms, it measures from sending the statement to getting the result. It does not include the time our thread waited for HikariCP to hand it a connection, the time to turn result rows into entities, the time to load related entities lazily later, or anything that happens after the repository returns. So "SQL totals 100 ms" is a true statement about execution and says nothing about the other 1.9 seconds.
Also notice that the question gives a p95. Per-request SQL totals from logs are often looked at for typical requests, while the slow 5 percent may differ, for example by waiting in a queue only under load. We should look at slow requests specifically.
2Read a Trace of a Slow Request
A distributed trace, from Micrometer Tracing or the OpenTelemetry Java agent, records spans for the incoming request, each outgoing HTTP call, each database statement and any custom spans we add. We pick a request from the slow 5 percent and look at the timeline. The spans tell us where measured work happened, and the gaps between spans tell us where unmeasured time went.
| Span | Start | Duration | What it tells us |
|---|---|---|---|
| http GET /customers/{id}/orders | 0 ms | 2,050 ms | the whole request |
| (gap before first query) | 5 ms | 620 ms | waiting for a pool connection? |
| SELECT customer | 625 ms | 8 ms | fast |
| SELECT order x 120 | 640 ms | 84 ms total | N+1: one query per order |
| http GET pricing-service | 900 ms | 410 ms | remote call inside the request |
| (gap after last span) | 1,310 ms | 740 ms | serialization, logging or GC? |
This trace is typical. The queries add up to about 100 ms, matching the log, but there are 121 of them, a 410 ms call to another service that nobody counted as SQL, and two large gaps. Each gap is a question for the next tools.
3Profile in Wall-Clock Mode
A normal CPU profiler samples threads only while they run on a CPU. Time a thread spends waiting, for a lock, a connection, a socket read or a GC pause, does not appear at all, so a CPU profile of this endpoint would show a little Jackson and Hibernate work and miss the problem. A wall-clock profiler samples threads whatever their state, so waiting shows up as clearly as computing.
async-profiler has a wall mode, and JDK Flight Recorder records thread park, monitor wait, socket read and GC events with stack traces. We profile during a period when the p95 is high, then filter to the request threads.
Now the gaps have names. The first gap is threads parked inside HikariPool.getConnection, waiting for a connection, and the gap at the end is Jackson writing a big response plus synchronous log writes.
4Check the Connection Wait and the Usual Suspects
HikariCP publishes hikaricp.connections.acquire, a timer for how long threads wait to get a connection, together with pending, the number of threads waiting. If acquire time at p95 is hundreds of milliseconds, the pool is saturated, and the SQL log will never show it. The fix is usually to hold connections for less time, not to add more of them.
The other suspects are worth checking one by one. N+1 lazy loading shows as many identical short queries; it multiplies round trips and holds the connection longer, and we fix it with a fetch join, an entity graph or batch fetching. Large JSON serialization, such as returning entities with deep object graphs, shows in the profile under the ObjectMapper; we return a DTO with only the needed fields and paginate. Synchronous logging at DEBUG or with large payloads blocks request threads on I/O; we reduce it or use an async appender. Calls to other services inside the request show as client spans; we parallelize them, cache them or move them out of the critical path.
5Rule Out GC Pauses and CPU Throttling
Two causes stop every thread at once and appear as gaps in every trace at the same moment. Garbage collection pauses show in jvm.gc.pause and in GC logs; a 300 ms pause hits every request in flight. CPU throttling happens when a container exhausts its CPU quota within a scheduling period and is paused until the next one; the JVM cannot see it, but container metrics such as throttled periods can. We line both up with the times of the slow requests. If they match, the fix is heap tuning, fewer allocations, or a higher CPU limit, not anything in the endpoint.
- Trace a slow requestspans and gaps
- Long gap before first query?YesCheck hikaricp.connections.acquireNoContinue
- Many identical queries?YesFix N+1 with fetch join or batchNoContinue
- Client spans to other services?YesParallelize, cache or move outNoContinue
- Wall-clock profileserialization, logging, locks
- Line up GC pauses and throttlingwith the slow periods
6 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Why does N+1 matter if each query takes less than a millisecond?
Each query also costs a network round trip, driver and Hibernate overhead, and entity creation, which the per-statement time does not show, and the connection is held for the whole sequence, which increases pool waiting for other requests. 120 queries at 0.7 ms of execution can easily cost 100 to 200 ms of request time, plus more queuing elsewhere.
How is wall-clock profiling different from CPU profiling?
CPU profiling samples only running threads, so it answers where CPU time is spent. Wall-clock profiling samples all threads whatever their state, so it answers where elapsed time is spent, including waiting on locks, I/O, pools and sleeps. For latency problems in services that mostly wait, wall-clock is the right view, usually filtered to request threads.
Would raising the pool size fix the connection wait?
Only if the database has spare capacity and the wait is truly from too few connections. More often connections are held too long, by N+1 queries, remote calls inside transactions or open-in-view keeping them until the response is written, and fixing that removes the wait without adding load to the database.
7Conclusion
In this answer, we saw that a 100 ms SQL total cannot explain a 2-second p95, because the SQL log measures only execution. A trace of a slow request shows spans and the gaps between them, a wall-clock profile with async-profiler or JFR reveals the waiting that CPU profiles miss, hikaricp.connections.acquire exposes pool waits, and the usual suspects, N+1 loading, large JSON, synchronous logging, remote calls, GC pauses and CPU throttling, account for the rest.
Open with
The SQL log measures only statement execution, so the other 1.9 seconds are somewhere it cannot see. I would start with a distributed trace of one of the slow requests, not an average one, and look at both the spans and the gaps between them.
Then, if they want more
Spans show remote calls and database statements; gaps show unmeasured time, such as waiting for a connection, serialization or a GC pause. Then I would profile in wall-clock mode, with async-profiler's wall event or a JFR recording, because a CPU profile misses threads that are waiting, and in latency problems waiting is usually the story.
In the flame graph filtered to request threads, I would look for time parked in HikariPool.getConnection, in Jackson, in logging appenders, or in socket reads to other services. I would check hikaricp.connections.acquire and pending, since time waiting for a connection never appears in SQL logs.
Then the usual suspects: N+1 lazy loading, which shows as dozens of identical short queries, each cheap but adding round trips and holding the connection; large JSON responses built from entity graphs; synchronous logging of big payloads; and calls to other services inside the request.
Finally, I would line up jvm.gc.pause and container CPU throttling with the slow periods, because both stop all threads at once and show as gaps in every trace. The fix depends on what I find: fetch joins for N+1, DTOs and pagination for serialization, shorter connection holding rather than a bigger pool.
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 official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
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 in a Spring Boot service takes 2 seconds at p95, but the SQL statements it...": https://scrivio-interview-library.netlify.app/spring-boot/service-performance/ac39c8de5a/