Percentile
A percentile is the value below which a given percentage of a dataset falls — for example, the 95th percentile (p95) of response times is the value that 95% of requests were faster than.
Overview
Percentiles describe the distribution of a dataset by identifying the value at a given rank. The 50th percentile (p50) is the median; the 95th percentile (p95) and 99th percentile (p99) are especially common in performance and reliability monitoring because they capture the experience of the slowest requests, which averages hide. A system with a fast average latency can still have a terrible p99 if a small fraction of requests are very slow — percentiles surface that in a way a mean cannot.
Computing percentiles in DuckDB
DuckDB supports exact percentile calculation through quantile_cont (continuous, interpolated between values) and quantile_disc (discrete, returns an actual value from the dataset), along with a dedicated median function:
Copy code
SELECT
quantile_cont(latency_ms, 0.5) AS p50,
quantile_cont(latency_ms, 0.95) AS p95,
quantile_cont(latency_ms, 0.99) AS p99
FROM requests;
SELECT median(latency_ms) FROM requests; -- shorthand for the 0.5 quantile
quantile_cont and quantile_disc also accept a list of fractions to compute multiple percentiles from a single pass over the data:
Copy code
SELECT quantile_cont(latency_ms, [0.5, 0.95, 0.99]) AS percentiles
FROM requests;
Exact versus approximate
Exact percentile calculation requires sorting (or an equivalent ordering operation) over the full column, which can be expensive on very large datasets. When an estimate is acceptable — dashboards, monitoring, exploratory analysis — DuckDB's approx_quantile function (based on a t-digest sketch) computes percentiles much faster and with bounded memory, at the cost of a small, generally negligible amount of accuracy.
Related terms
Approximate aggregation refers to computing statistics like distinct counts, quantiles, or samples using algorithms that trade a small, bounded amount of accuracy for large gains in speed and memory efficiency.
Latency →Latency is the time delay between initiating a request or operation and receiving its result — for example, the time from sending a database query to getting the first byte of its response back.
HyperLogLog →HyperLogLog is a probabilistic algorithm for estimating the number of distinct elements in a large dataset (cardinality) using a small, fixed amount of memory, at the cost of a small, predictable error margin.
FAQS
quantile_cont interpolates between two adjacent values when the requested percentile falls between them, producing a continuous estimate. quantile_disc always returns one of the actual values present in the dataset, without interpolation.
Averages can mask a small but significant fraction of slow requests. p95 and p99 specifically describe the tail of the distribution, which is often what actually determines whether users perceive a system as slow, even when the mean response time looks fine.
