Choosing a Database · Lesson 68 of 112

Database OLTP and OLAP

What you will learn

  • What OLTP and OLAP mean
  • Why the two jobs want different designs
  • What row-oriented and column-oriented storage are
  • Why many organisations keep two systems

1. Two jobs

OLTP (online transaction processing) OLAP (online analytical processing)
Purpose run the business analyse the business
Typical action save an order, update a balance total sales per product per month
Rows touched a few a very large share of the table
Speed goal each action in a blink, many at once one big question answered in reasonable time
Rules strict, transactions (chapter 6) data is mostly loaded once and read

This lesson runs on the sample database you chose in lesson 4.1 (use the Example data picker at the top). Here is the same database asked both ways:

The first query is OLTP-style: fetch one order by its key, touching one row. The second is OLAP-style: read every order line to add up money per category.

SELECT * FROM orders WHERE order_id = 7;
SELECT cat.name AS category, ROUND(SUM(oi.quantity * oi.unit_price), 2) AS revenue
FROM order_items oi
JOIN products p ON p.product_id = oi.product_id
JOIN categories cat ON cat.category_id = p.category_id
GROUP BY cat.name
ORDER BY revenue DESC
LIMIT 4;
order_id  customer_id  employee_id  order_date  status     shipped_on  ship_city
--------  -----------  -----------  ----------  ---------  ----------  ---------
       7           62            4  2025-03-24  delivered  2025-03-28  Osaka
(1 row)

category     revenue
----------  --------
Kitchen     44832.29
Garden      43035.76
Stationery  41386.72
Outdoor     40286.54
(4 rows)

The first query is OLTP-style: fetch one ledger row by its key, touching one row. The second is OLAP-style: read every ledger row to add up money per kind.

SELECT * FROM transactions WHERE txn_id = 7;
SELECT kind, COUNT(*) AS rows_read, SUM(amount_cents) AS total_cents
FROM transactions
GROUP BY kind
ORDER BY total_cents DESC;
txn_id  account_id  txn_date    kind  amount_cents  balance_after  description
------  ----------  ----------  ----  ------------  -------------  -----------
     7         309  2018-03-28  fee            500          52700  Monthly fee
(1 row)

kind          rows_read  total_cents
------------  ---------  -----------
deposit            3107     99441800
withdrawal         2519     37089800
transfer_out        539      7891800
transfer_in         539      7891800
fee                 403       225700
interest            308        72830
(6 rows)

The first query is OLTP-style: fetch one enrollment by its key, touching one row. The second is OLAP-style: read every enrollment to average the grades per section.

SELECT * FROM enrollments WHERE student_id = 1 AND section_id = 30;
SELECT section_id, COUNT(grade) AS graded, ROUND(AVG(grade), 1) AS average_grade
FROM enrollments
GROUP BY section_id
ORDER BY average_grade DESC, section_id
LIMIT 4;
student_id  section_id  enrolled_on  grade
----------  ----------  -----------  -----
         1          30  2025-01-14    49.2
(1 row)

section_id  graded  average_grade
----------  ------  -------------
        37       2           86.4
        29       6           76.9
        31      10           76.5
        30      30           76.3
(4 rows)

The first query touches one row; the second has to read a large share of a table. That difference is the whole story of the next section.

2. Rows or columns?

A row-oriented database stores each row's values together, ideal for "get or change one whole row". A column-oriented database stores each column's values together, ideal for "add up one column across millions of rows", because it reads only the columns the question needs and compresses similar values well. The same four made-up sales in both layouts, in Python:

rows = [("rice", 5, 10), ("oil", 1, 20), ("tea", 2, 50), ("salt", 3, 10)]      # four made-up sales, one tuple per row
columns = {"product": [r[0] for r in rows], "qty": [r[1] for r in rows], "price": [r[2] for r in rows]}
print(sum(columns["qty"]), len(rows))
print(rows[:2])
print({k: v[:2] for k, v in columns.items()})
11 4
[('rice', 5, 10), ('oil', 1, 20)]
{'product': ['rice', 'oil'], 'qty': [5, 1], 'price': [10, 20]}

Summing qty with the column layout touches one list and nothing else. With the row layout every row, including product and price, must be passed over. (In Python both are fast on four rows; the point is the shape, which matters at scale.)

3. Two systems

Running a big report on the same database that takes customers' orders can slow the orders down. So many organisations copy data from the OLTP database into a separate analytical database (often column-oriented) on a schedule, and run reports there. Small shops can skip this and run reports on a copy or at a quiet hour.

Worked example: sort the queries

Query OLTP or OLAP?
Add 2 shirts to order 1042 OLTP
Average basket size by month for three years OLAP
Show the status of order 1042 OLTP
Which products sell best in each city? OLAP

The orders are made up.

Common mistakes

  • Running huge reports on the live OLTP database at the busiest hour.
  • Thinking OLAP is "just a bigger database". The storage layout and goals differ.
  • Building a second system before you need it. A small business can usually report from the main database.

Key takeaways

  • OLTP runs the business with many small, quick, strict operations; OLAP analyses it with big scans.
  • Row storage suits whole-row work; column storage suits scanning a few columns of many rows.
  • Large organisations often keep separate OLTP and OLAP systems.

Practice

1. What do OLTP and OLAP stand for?

Show answer

Online transaction processing and online analytical processing.

2. Which of the two demo queries was OLTP-style?

Show answer

SELECT * FROM sales WHERE id = 7, which touches one row by its key.

3. Why does a column layout help for summing one column?

Show answer

The values of that column are stored together, so only that column has to be read.

4. Why might a report slow down a shop's order system?

Show answer

It reads a huge share of the data and competes with order operations for the same resources.

5. Classify: "change the delivery address of order 5".

Show answer

OLTP.

6. Does a very small shop need a separate OLAP system?

Show answer

Usually not; it can run reports on the main database or a copy, at a quiet time.