ReinDBby Bandlei

3. Build a data profile

Brett Rosenblatt · Database tuning guide v1.3 · PDF

Before you decide what to tune, or where data should live, know what you have. A data profile is a short, factual description of each database: how big it is, how fast it grows, what is hot, and how it is used. It takes an afternoon, and it replaces weeks of guessing.

3.1What goes in the profile

AreaWhat to captureWhy it matters
InventoryEngine, version, edition, platform, high availability and DR setup, total size, growth per monthSets the constraints for every later decision
ObjectsLargest tables and indexes (including LOB or TOAST storage), row counts, growth per tableShows where space and maintenance time go
ActivityReads and writes per table, full scans against index access, the hottest tables by timeShows where the work goes
TemperatureHow much data is touched each day, week and month; how old the rows are that queries actually readDecides what can move to cheaper storage
ShapeColumn cardinality and skew, null rates, wide columns, LOB and JSON use, data types of join keysExplains misestimates and poor index choices
WorkloadTransaction types (OLTP, reporting, batch, ETL), peak hours and days, concurrency, share of time taken by the top statements, read/write mixDecides how to tune and what to protect
ConstraintsRetention rules and legal holds, RPO and RTO, data classification (PII), residency, service levelsLimits what you're allowed to change or move

3.2Sizes and activity

For each database, capture the largest tables and indexes with their row counts, including large-object storage, and the activity on each table: rows read, inserted, updated and deleted, and full scans against index access. Activity counters are cumulative since the last restart or statistics reset, so compare two snapshots to get a rate. The engine notes give a starting query for each engine.

3.3Column shape: cardinality, skew and nulls

Skewed columns are behind many bad plans: the optimizer assumes an even spread unless statistics say otherwise. Check the columns your important statements filter and join on.

3.4Growth and temperature

Growth needs history. If your monitoring doesn't keep size history, snapshot sizes once a day into a small table of your own; a year of daily rows per table is tiny.

Temperature tells you how much of the data is actually used. Where tables carry creation or event timestamps, two simple checks go a long way:

-- How rows are spread over time (standard SQL; see the note for SQL Server)
SELECT EXTRACT(YEAR FROM order_date)  AS yr,
       EXTRACT(MONTH FROM order_date) AS mon,
       COUNT(*)                       AS order_rows
FROM   orders
GROUP  BY EXTRACT(YEAR FROM order_date), EXTRACT(MONTH FROM order_date)
ORDER  BY yr, mon;

The second check is in the workload: look at the date predicates in your top statements. If nearly every query reads the last 90 days while the table holds seven years, you have found a tiering opportunity (Chapter 4) and probably a partitioning key (Chapter 10).

TierTypical ageAccess patternWhere it usually belongs
HotDays to weeksFrequent reads and updates; latency mattersPrimary OLTP storage, fully indexed
WarmMonthsReporting and occasional lookupsOlder partitions in the same database, compressed; a replica or a warehouse
ColdYearsRare reads, compliance, analyticsArchive tables, or open file formats in object storage queried through external tables or a federated engine

3.5The workload profile

Data at rest is half the picture. The other half is how it is used.

  • Peaks. Database time per hour across a typical week. Most systems have a clear daily shape and a weekly one.
  • Mix. OLTP, reporting, batch and ETL, split by application or module where you can (see the instrumentation tips in Chapter 9).
  • Concentration. The share of database time taken by the top 10 and top 50 statements. In most systems a small number of statements dominate, which is why statement-level tuning pays.
  • Concurrency. Average active sessions against the number of CPU cores, at peak and on average.
  • Read/write mix and commit rate. These drive log, WAL or redo volume, replication load and storage choices.
  • Overlap. Batch windows, backups and statistics jobs, and how they overlap with online peaks.
  • Clients. Which applications, tools and people connect, with connection counts and pool settings.

3.6A one-page profile template

FieldExample
Database, engine and versionorders-prod, PostgreSQL 16.4 on Amazon RDS
Size and growth1.9 TB, growing about 60 GB per month
Largest five tables, share of sizeevents 52%, order_items 14%, orders 8%, audit_log 7%, sessions 4%
Hottest five tables, share of readsorders 31%, customers 18%, order_items 15%, inventory 9%, prices 6%
Workload mixOLTP all day; reporting 07:00 to 09:00; ETL 01:00 to 03:00
Peak windowWeekdays 09:00 to 11:00 local time
Top 10 statements, share of database time64%
Data older than 12 months71% of rows; read by two monthly reports
Retention and complianceOrders 7 years; events 13 months; PII in customers and orders
RPO and RTO5 minutes and 1 hour
Known pain pointsMorning search latency; the events table dominates backup time

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