What you will be able to do
- Explain why a good average latency can coexist with an unusable tail, and which percentile catches it.
- Connect rising utilisation to non-linear latency through queueing theory - the reason the last 20 percent of capacity costs most of the tail.
- Take a wait-statistics baseline and a delta, and turn a wait type into a named resource and a next step.
- State the reasons a workload may not reach millisecond latency even when the engine is healthy.
Every performance conversation that goes wrong goes wrong in the same place: it is conducted in averages. The average latency of a healthy workload and a sick one can be identical while the experiences are completely different - because the failure is in the tail, and the tail is where the users are.
The analogy for the whole lesson is a hospital emergency department. The average wait can look fine while a few patients wait all night - the tail is where the suffering is, and it is invisible in the mean. As the department fills, the queue does not grow gently: each occupied bed adds a little delay until the last few beds turn a short wait into a long one, which is exactly how queue time explodes near full utilisation. Asking each waiting patient what they are waiting for - a bed, a scan, a doctor - is the wait-statistics method: you stop arguing about “slow” and start naming the resource.
The most common mistake is reading one cumulative snapshot of sys.dm_os_wait_stats and treating
its top wait as the bottleneck. The totals count every idle wait since the instance started, so a
single reading names what has accumulated most since boot, not what hurt during the window the user
felt. The delta over a known interval is the diagnosis; the snapshot alone is not.
The average hides the failure
A 2 ms average built from a thousand 1 ms queries and a few 900 ms stalls looks fine on a dashboard and feels broken to the user who caught the stall. The p95 and p99 percentiles are how you see the tail: p95 is the latency the slowest five requests in a hundred experience; p99 the slowest one in a hundred. A service-level objective that says “p99 under 50 ms” can be defended; “average under 2 ms” cannot, because the average does not say whose request it describes.
Latency is not one number - it is a queue
A request’s total latency has two parts: service time - how long the engine takes to do the work - and queue time - how long it waits for the resource doing the work. Queueing theory has one uncomfortable property: utilisation and queue delay are not linear. As utilisation climbs toward 100 percent, queue delay grows toward infinity.
| Utilisation | What the tail looks like | The practical rule |
|---|---|---|
| Under ~50 percent | Tight; queue time near zero | Headroom is real - you can absorb a spike |
| ~70 percent | p99 starts separating from the mean | The first warning - variance is arriving |
| ~90 percent+ | The tail explodes; small service-time wobble becomes big queue delay | You are operating on the cliff edge; a single slow query stalls the whole queue |
This is why “CPU at 85 percent” is not a healthy steady state but a pending incident: at that utilisation, any additional work queues, and queue time - not service time - is what the p99 user feels.
Why a request can be slow while the engine is fine
Arrive
The request lands in the workload
Arrival rate is set by the application, not the engine - it can exceed service rate at peak
Queue
Wait for a scheduler, a lock, a latch, or a page
This is where the p99 lives. Waits are the queue made visible
Service
The plan actually executes
Bounded by the plan and the hardware - the part indexes and query tuning can shorten
Return
Result over the network
Round trips add latency no engine tuning removes
Why milliseconds are sometimes impossible
The prompt “make it millisecond” is a target, not a property. Each of these puts a floor under latency that query tuning cannot break:
- Network round trips - each logical operation across the wire pays latency the engine does not control. A workload that chatters pays it per statement.
- Durable commit -
COMMITwaits on the log flush.WRITELOGis the storage device, not the CPU; a slow log device puts a hard floor under write latency. - Lock and latch contention - the request is correct and the plan is fine; it is simply waiting behind another session’s work.
- Storage latency - a cold page is a physical read at whatever the device costs.
- Query complexity and data volume - a plan that must scan a billion rows has a service time no measurement method can wish away.
- Resource saturation - at high utilisation the queue component dominates, so latency grows even though service time did not change.
The wait-statistics method
SQL Server records why sessions wait in sys.dm_os_wait_stats, cumulative since the instance
started. The diagnostic method is always the same three steps:
-- 1. Snapshot a baseline
SELECT wait_type, wait_time_ms, waiting_tasks_count
FROM sys.dm_os_wait_stats
ORDER BY wait_time_ms DESC;
-- 2. Let the workload run for a known interval
-- 3. Snapshot again and take the delta
-- The waits that grew during the window are your bottleneck, named.
The delta over a window is the evidence. The cumulative totals are misleading - they include every
idle wait since boot. The top few wait types in your delta give you the resource to chase, and
every wait type has a meaning: PAGEIOLATCH is storage latency, LCK_M_* is lock contention,
PAGELATCH is an in-memory page fight, WRITELOG is the log device, CXPACKET/CXCONSUMER is
parallelism, SOS_SCHEDULER_YIELD is runnable tasks queueing for CPU.
This lesson is deliberately short because the method is short: measure the wait delta, name the resource, go to the lesson that owns it. The rest of the curriculum is the detail of what each named resource is and how it is fixed.
Guided practice
A beginner exercise, worked in a scratch database. The scenario: a colleague reports that a query “feels slow”, and your job is to turn that into a named resource using only the lesson’s method. Work each step and check yourself before moving on.
- Take the baseline. Run the lesson’s query -
SELECT wait_type, wait_time_ms, waiting_tasks_count FROM sys.dm_os_wait_stats ORDER BY wait_time_ms DESC- and save the output. Check: you have the full result set, including waits you do not recognise. If you skipped this step, nothing later can be a delta - revisit the three-step method; the baseline is what makes the second snapshot meaningful. - Generate a known workload. In your scratch database, create a small table, insert a few
thousand rows, then run a loop that scans it repeatedly - a
WHILEloop aroundSELECT COUNT_BIG(*) FROM your_tableis enough - for a known interval. Check: the workload ran and you can say what the interval was. If you cannot name the window, the delta is meaningless - waits are measured over an interval, not at a point. - Snapshot again and take the delta. Re-run the same wait query and compare
wait_time_msper wait type with the baseline. Write down the waits that grew most during the window. Check: your list is the growing waits, not the top of the cumulative list. If you just re-read the top of the snapshot, you repeated the most common mistake - the totals include every idle wait since boot. - Translate the top wait into a resource. Map your biggest delta wait with the lesson’s list:
PAGEIOLATCHis storage,LCK_M_*is locks,PAGELATCHis an in-memory page fight,WRITELOGis the log device,CXPACKET/CXCONSUMERis parallelism,SOS_SCHEDULER_YIELDis runnable tasks queueing for CPU. Check: the answer names a resource - “storage latency” or “CPU queueing” - not a feeling. If you could not map the wait type, revisit the wait list; the method is wait type to named resource, and the lesson that owns each one is the next step. - State what the average hid. During your workload, which moved first - the mean latency or the p99? Answer in one sentence. Check: the p99. Queue time grows non-linearly with utilisation, so the tail separates before the mean moves - the reason “average under 2 ms” cannot be defended as a target. If you answered the mean, reread the utilisation table.
Check your understanding
Ordered easy to hard. Answer each before reading the answer; a wrong answer names where to look.
- Why can a healthy-looking average latency coexist with an unusable workload? The failure lives in the tail. A mean built from many fast requests and a few stalls looks fine while the users who caught the stalls feel a broken system - the p95 and p99 exist precisely to make that tail visible. If this was unclear, reread the opening section.
- What does “p99 under 50 ms” actually promise? That the slowest request in a hundred completes under 50 ms - a statement about a specific request a real user made, which is why it can be defended where “average under 2 ms” cannot: the average does not say whose request it describes.
- Why does latency rise non-linearly as utilisation climbs toward 100 percent? Total latency is service time plus queue time, and queue delay grows toward infinity as utilisation approaches saturation - at high utilisation any extra work queues, so the p99 feels the queue even when service time is unchanged. If you attributed it to slower hardware, revisit the queueing table - the engine’s service time did not change; the wait did.
- A single snapshot of
sys.dm_os_wait_statsshowsCXPACKETon top. Why is blaming parallelism premature? The counters are cumulative since the instance started - the top wait may have accumulated over a long uptime of idle background work and say nothing about the incident window. The method is baseline, interval, delta: the waits that grew during the window are the bottleneck, named. If you trusted the snapshot, revisit the most common mistake above. WRITELOGtops your wait delta and a colleague proposes adding an index. Why is that the wrong fix, and what is?WRITELOGis the log device - durable commit waits on the log flush, which is a storage property, not a plan property, so no index tuning changes it. The levers are faster log storage, fewer and larger commits, or delayed durability - the write-path lesson owns the detail. If you agreed with the index, revisit the “why milliseconds are sometimes impossible” list and the wait-type meanings.
Where SQL Server sits
How this component relates to the rest of the stack. Items without a link are not covered here yet.
Deployed with
Commonly run alongside
- Windows Failover Clustering