ReinDBby Bandlei

1. How to start

Brett Rosenblatt · Database tuning guide v1.3 · PDF

Most tuning goes wrong before anyone touches a query. Someone picks a suspect, applies a favorite fix, and declares victory because the next run was faster (often only because the cache was warm by then). Good tuning is calmer than that. It is a loop you can repeat, explain and defend.

1.1Start with a symptom and a target

Write the problem down in the words of the people who feel it, then turn it into a number.

Symptom as reportedMeasurable target
"Order search is slow in the morning."95th percentile of the search statement under 300 ms between 08:00 and 11:00
"The nightly load keeps missing its window."Load finishes by 05:30 on 95% of nights
"The server has been maxed out since Tuesday."Peak CPU under 70%, and load back to its usual level for each hour
"Reports time out at month end."The month-end report set finishes in under 20 minutes with no timeouts

A target tells you when to stop. Without one, tuning never ends, and every extra change carries risk without a clear payoff. Note the scope as well: one statement, one feature, one database, or the whole estate. The scope decides which data you need and who has to be involved.

1.2The tuning loop

  1. Measure. Collect how long things take and where the time goes, over a period that includes the problem.
  2. Find the biggest consumer. Rank by the time it costs, not by how bad it looks.
  3. Explain why. Read the plan, the waits and the statistics until you can say in one sentence why it is slow.
  4. Change one thing. Choose the least invasive fix that addresses the cause.
  5. Verify against the baseline. Compare at the same time of day, under the same kind of workload, with what was normal before.
  6. Record. Write down what you changed, why, the before and after numbers, and how to undo it.

Then go around again with the next biggest consumer, until you reach the target.

The tuning loop: measure, find the biggest consumer, explain why, change one thing, verify against the baseline, record.
The tuning loop: measure, find the biggest consumer, explain why, change one thing, verify against the baseline, record.

1.3Ground rules

  • Tune for time, not ratios. A buffer cache hit ratio of 99.9% says nothing about whether users are waiting. A statement doing ten million logical reads per execution has a wonderful hit ratio and is still the problem. Time, and the work behind it, is what users and budgets feel.
  • Fix the biggest consumer first. If a statement accounts for 2% of database time, making it infinitely fast saves at most 2%.
  • Change one thing at a time. With two changes you can't tell which one helped, or which one hurt.
  • Compare like with like. Same hour, same kind of day, same data volume, warm cache against warm cache, representative parameter values.
  • Make every change reversible. Write the rollback before you deploy.
  • Test with production-like data. Plans depend on data volume and distribution. A query that is fast on 10,000 test rows can choose a completely different plan on 100 million.
  • Measure work, not just elapsed time, when you test. Logical reads per execution, rows examined per row returned and temp usage are stable from run to run. Elapsed time swings with cache state and concurrency.
  • Know when to stop. When the target is met, stop. The last 10% of improvement often carries most of the risk.

1.4Ask "what changed?"

Databases rarely get slower for no reason. Before you measure anything, ask what changed in the days before the problem started.

  • Code: a deployment, a new report, a new ORM version, a feature flag switched on.
  • Data: growth that crossed a threshold, a bulk load, a purge that stopped running, a new customer with ten times the usual data.
  • Statistics: gathered at a bad moment, not gathered after a big load, or locked long ago.
  • Configuration: a parameter change, a new compatibility level, a bigger connection pool.
  • Software: an upgrade or patch that changed optimizer behavior.
  • Infrastructure: a new instance class, storage type or volume size; burst credits used up; a noisy neighbor; a failover to different hardware.
  • Plans: a new execution plan for an existing statement, often after a statistics refresh or a restart.
  • Schedules: batch jobs, backups or maintenance moved into busy hours.
  • Usage: a marketing campaign, month end, a new integration calling one API in a tight loop.

Most sudden regressions trace back to one of four causes: a plan change, data growth, contention, or the infrastructure underneath. Keep those four in mind while you measure.

1.5Your first hour on an unfamiliar database

When you inherit a database, or get paged for one you have never seen, work through this list before you change anything.

CheckWhy it mattersWhere to look
Version, patch level, edition, platformFeatures, known bugs and licensing limitsVersion functions and views; cloud console
Time since the last restartCumulative counters and caches only cover this periodThe engine's instance status views
Size and growthCapacity risk and maintenance costData dictionary sizes; your size history
Top waits and the time modelThe class of problem: CPU, I/O, locks or commitsChapter 5
Top statements by total timeWhere the database actually spends its timeThe engine's statement statistics (Chapter 5)
Blocking and long transactions right nowAcute contention, often the real cause of "slowness"Live session and lock views
Storage headroom: data, log, WAL or redo, temp, archive destinationA full disk is an outage, not a slowdownFile and tablespace usage; OS and cloud metrics
Last good backup and replica lagDon't tune a database you can't restoreBackup catalog; replication views
Statistics freshness and maintenance jobsStale statistics and skipped maintenance cause many bad plansStatistics views; job history
Recent changesThe fastest path to the causeDeploy log, change tickets, parameter history
Settings far from defaultsYesterday's fix can be today's problemNon-default settings queries in Chapter 11

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