OLTP vs OLAP: Two Kinds of Database Work and Why They Need Different Tools
Your online shop has two kinds of users of its data. Customers place orders, update their address and check their order status, thousands of tiny operations every minute. The finance team wants to know revenue per region per month for the last three years, which means reading millions of rows to produce a small table.
These are two different workloads, with two names: OLTP (Online Transaction Processing) and OLAP (Online Analytical Processing). Mixing them in one database is one of the most common causes of "the database is slow and we do not know why".
OLTP: running the business
OLTP is the day-to-day work of an application:
- insert an order, update stock, record a payment,
- read one customer, one order, one product page,
- many concurrent users, each doing small operations,
- every operation must be correct and durable: no lost payment, no double booking.
A typical OLTP query touches a handful of rows and finds them through an index:
UPDATE products SET stock = stock - 1 WHERE id = 812; INSERT INTO orders (user_id, product_id, total) VALUES (42, 812, 2400); What matters here: low latency per request (milliseconds), high concurrency, and ACID transactions. The schema is usually normalized: each fact is stored once (customers in one table, orders in another), which keeps writes simple and avoids contradictory copies.
Typical engines: PostgreSQL, MySQL, SQL Server, Oracle. They store data by row, which suits reading and writing whole records.
OLAP: understanding the business
OLAP is the work of analysis and reporting:
- revenue per month per region, top products per category, cohort retention,
- a few users (analysts, dashboards, scheduled reports),
- each query reads a large part of the data, but only a few columns,
- data is mostly appended in batches, rarely updated.
A typical OLAP query aggregates a lot:
SELECT region, date_trunc('month', order_date) AS month, sum(total) FROM orders WHERE order_date >= '2023-01-01' GROUP BY region, month; What matters here: throughput when scanning large volumes, compression, and parallelism. A slow minute is acceptable; a wrong number is not, but a few seconds of staleness usually is.
The schema is often denormalized into a star schema: a central fact table (one row per sale, with numbers like quantity and amount) surrounded by dimension tables (date, product, customer, store) that describe it. This makes analytical queries simpler and faster to join.
Typical engines: ClickHouse, BigQuery, Snowflake, Amazon Redshift, DuckDB. They store data by column, which lets a query read only the columns it needs.
Side by side
| OLTP | OLAP | |
|---|---|---|
| Main job | Record and serve transactions | Analyse history |
| Typical query | Read or write a few rows by key | Scan and aggregate millions of rows |
| Users | Many, concurrent | Few, analysts and dashboards |
| Latency goal | Milliseconds | Seconds to minutes |
| Writes | Constant small inserts and updates | Large batch loads |
| Schema | Normalized | Star or snowflake, denormalized |
| Storage | Row-oriented | Column-oriented |
| Freshness | Live | Minutes to a day behind, often |
Why not one database for both?
At small scale, you can. A PostgreSQL instance with a few million rows can serve the app and run reports without trouble. Problems appear as data grows:
- Heavy reports compete with users. A query scanning 200 million rows uses CPU, memory and disk bandwidth that checkout requests need. Customers feel it as slow pages.
- Indexes pull in opposite directions. OLTP wants a few precise indexes to keep writes fast. Reports want many indexes for many shapes of query. Adding them slows every insert.
- Locks and long transactions. A long analytical query keeps an old snapshot open. In PostgreSQL this can delay cleanup of dead rows (vacuum), which makes tables bloat.
- The storage layout is wrong for one side. Row storage wastes most of its reading on analytical queries; column storage is poor at single-row updates.
How data gets from OLTP to OLAP
The standard approach is to keep them separate and copy data across:
- Batch ETL or ELT: a job runs every night or every hour, extracts new and changed rows, and loads them into the warehouse. Simple and common.
- Change data capture (CDC): a tool reads the database's change log (for example the PostgreSQL write-ahead log or the MySQL binlog) and streams every insert, update and delete to the warehouse within seconds. Debezium is a well-known open-source option.
- Read replica as a first step: before a warehouse, send reports to a read replica of the OLTP database. It protects the primary, though it keeps the row layout and its limits.
The warehouse is then free to reshape the data: build fact and dimension tables, pre-aggregate, and keep years of history.
A middle ground: HTAP
Some systems aim to do both in one product, called HTAP (Hybrid Transactional/Analytical Processing). They usually keep a row copy for transactions and a column copy for analytics inside the same system. They can remove the pipeline, at the cost of a more specialised database. Most teams still start with the separate approach because each part is well understood.
Common mistakes
- Running dashboards against the production primary and wondering why checkout slows down every morning at 9.
- Moving to a warehouse too early. If reports are fast enough on a replica, a pipeline may be work you do not need yet.
- Copying the OLTP schema as-is into the warehouse. It works, but analysts then write huge joins. Model facts and dimensions.
- Forgetting freshness. Tell users how old the numbers are. "Revenue as of 06:00" avoids confusion.
- Treating the warehouse as the source of truth for operations. It is a copy for analysis; orders and payments live in the OLTP database.
Watch the animated version: https://youtu.be/E1slQUTgNRM
In short
- OLTP runs the business: many small, concurrent, transactional reads and writes. Row stores, normalized schemas.
- OLAP explains the business: few large scans and aggregations. Column stores, star schemas.
- One database can do both while you are small; as data grows, separate them and move data with ETL or CDC.
- Pick the setup by the workload, not by the product name.
Originally published at abdeldjalilchougui.biz.