ReinDBby Bandlei

12. Baselining: our approach and why it matters

Brett Rosenblatt · Database tuning guide v1.3 · PDF

"Is this normal?" is the first question in every performance incident, and the one most teams can't answer quickly. A baseline answers it. It is a description of what normal looks like for a particular database, at a particular time, so you can tell a real change from the usual rhythm of the day.

12.1Why baselines matter

  • Averages lie. A database that is busy at 09:00 and idle at 03:00 has a daily average that describes neither. A load of 4 active sessions is alarming at 03:00 and perfectly normal at 09:00.
  • Static thresholds are either noisy or blind. Set "CPU above 80%" and you get paged every busy morning; set it at 95% and you miss the slow climb that matters.
  • Baselines turn opinions into evidence. "It feels slower" becomes "this statement takes 3.2 times its usual time for this hour, since Tuesday 14:00".
  • They prove a fix worked, by comparing the same hours before and after the change.
  • They catch regressions early, before a statement climbs into the top ten.
  • They feed capacity planning with a trend instead of a guess.

12.2Kinds of baseline

KindWhat it compares againstGood for
Static thresholdA fixed numberHard limits: disk full, connections at the maximum, replication lag beyond the recovery point objective
Rolling windowThe last N hours or daysSudden changes in steady workloads
SeasonalThe same hour of the day (and optionally the same day of the week) over recent weeksMost real workloads, which follow daily and weekly cycles
EventA named period before a release, an upgrade or a parameter changeProving that a change helped, or finding what it broke

12.3What to baseline

  • Load: database time, or average active sessions, per hour.
  • Throughput: executions, transactions and commits per second.
  • Per statement: time per execution, executions per hour, logical reads per execution, rows per execution, and the plan in use.
  • Waits: time by wait class.
  • I/O and memory: physical reads, temp usage and spills.
  • Log volume: redo, WAL or transaction log generated per hour.
  • Sessions and contention: active sessions, sessions idle inside an open transaction, and blocked sessions; lock wait time; deadlocks.
  • Capacity: database and table sizes and their growth.
  • Replication: lag in seconds and bytes.

12.4Our approach in ReinDB

ReinDB is built around a seasonal baseline, because that is what matches how real databases behave.

  1. Measure every hour from the engine's own statistics. ReinDB reads the cumulative statement and wait statistics each engine already keeps (pg_stat_statements, performance_schema, Query Store or the plan cache, and Oracle's V$ views) and stores the differences between runs. Nothing is installed in the database, and it reads only.
  2. Define normal as the median and spread over the last 14 days at the same hour of the day. Monday's 09:00 is compared with the previous two weeks of 09:00s, not with last night.
  3. Flag what drifts from normal, ranked by the extra time it costs. A statement that is ten times slower but adds one second per hour ranks below one that is 20% slower and adds an hour of database time.
  4. Track each flag's life. Flags are NEW when they first appear, ONGOING while the drift continues and RESOLVED when it ends, so a recurring problem looks different from a one-off.
  5. Explain with evidence. Each flag shows what drifted, by how much, since when, and the extra time per hour, with the statement's plan history and the analysis behind it.

Why these choices:

  • The median, not the mean. One bad hour doesn't redefine normal; outliers barely move a median.
  • Spread, not just a center. Statements with naturally noisy timings need a bigger change before they are flagged, so steady statements can be flagged on smaller changes. That keeps the noise down without hiding real drift.
  • Fourteen days. Two full weekly cycles: long enough to be stable, short enough to adapt as the data grows.
  • Hour of the day. Daily cycles dominate most workloads. For strong weekly patterns, such as a Monday batch, compare with the same hour last week as well, which the "Vs baseline" report and the Baselines page both offer.
  • Ranked by extra time. It focuses attention where fixing something actually returns capacity.

12.5Build a simple seasonal baseline yourself

If you are building your own repository, the core is small. Snapshot the cumulative statement statistics every hour, store the differences, and compute the usual value for each statement and hour of the day. The example is written for PostgreSQL, and the engine note covers the functions that differ elsewhere; the idea works in any engine.

Then flag a statement in the current hour when its time per call is well above its usual range (for example above both 1.5 times the median and the 90th percentile), and the extra time it costs (the excess per call multiplied by the calls) is above a floor that matters to you, such as a minute per hour. Require a minimum number of samples before you trust a baseline. A robust alternative to percentiles is the median absolute deviation (MAD): scaled by 1.4826, it estimates the standard deviation without letting a few bad hours distort it, so "more than three MADs above the median" makes a sturdy test.

12.6Baselines around changes

  • Take, or name, a baseline before every deploy, upgrade, parameter change and index change. "Before release 4.2" is a baseline worth keeping.
  • After the change, compare the same hours of comparable days, not the hour after against the hour before.
  • Annotate dashboards with deploys and changes, so drift lines up with its cause at a glance.

12.7Baseline pitfalls

  • Normal isn't good. A baseline says what is usual, not what is acceptable. A database that has been slow for a month has a slow baseline. Pair baselines with targets (Chapter 1).
  • Counter resets. Restarts, statistics resets and plan cache eviction make one interval look strange. Detect resets (a counter that goes down) and skip those intervals.
  • Calendar effects. Month end, quarter end, holidays and campaigns break the pattern. Compare them with the same event last time, not with an ordinary week.
  • Slow drift. Growth shifts normal gradually, and a 14-day window follows it. That is why capacity and growth need their own trend lines and projections, separate from drift detection.
  • Too little history. A baseline built from two days is a guess. Wait for enough samples before acting on the flags.

This is one chapter of the free database tuning guide for PostgreSQL, MySQL, SQL Server and Oracle. Download the whole guide as a PDF.