Database Warehousing and Analytics · Lesson 95 of 112

Database Operational Versus Analytical Data

What you will learn

  • The difference between operational (OLTP) and analytical (OLAP) work
  • Why one design rarely suits both
  • What a data warehouse is
  • How the same data is asked different kinds of questions
  • When a separate analytical copy is worth it

1. Two kinds of questions

Think of a shop's database. Two very different kinds of questions arrive.

Operational questions keep the shop running:

  • "What is the status of order 7?"
  • "Customer Rina just paid. Record it."
  • "Is there stock for 2 kettles?"

Each question touches one row or a few rows, must answer in a blink, and many people ask at the same time all day. This work is called OLTP, online transaction processing (lesson 9.5 introduced the term).

Analytical questions help people decide:

  • "Which month had the highest revenue?"
  • "Which customers bring in the most money?"
  • "Did sales fall after we raised prices?"

Each question reads thousands or millions of rows, produces a small summary, and may take seconds or minutes. Only a few analysts ask them. This work is called OLAP, online analytical processing.

2. Seeing both on the same table

This lesson runs on the sample database you chose in lesson 4.1 (use the Example data picker at the top). Run the examples from top to bottom; Reset data starts again.

First, operational work: look up one row, change it, add a new one.

SELECT order_id, customer_id, status FROM orders WHERE order_id = 7;
UPDATE orders SET status = 'shipped' WHERE order_id = 7;
INSERT INTO orders (customer_id, order_date, status, ship_city) VALUES (1, '2026-10-01', 'pending', 'Leeds');
SELECT order_id, status FROM orders WHERE order_id IN (7, 2601) ORDER BY order_id;
order_id  customer_id  status
--------  -----------  ---------
       7           62  delivered
(1 row)

order_id  status
--------  -------
       7  shipped
    2601  pending
(2 rows)
SELECT transfer_id, from_account, to_account, status FROM transfers WHERE transfer_id = 7;
UPDATE transfers SET status = 'failed' WHERE transfer_id = 7;
INSERT INTO transfers (from_account, to_account, amount_cents, created_on, status) VALUES (1, 2, 500, '2026-10-01', 'done');
SELECT transfer_id, status FROM transfers WHERE transfer_id IN (7, 601) ORDER BY transfer_id;
transfer_id  from_account  to_account  status
-----------  ------------  ----------  ------
          7           112          25  done
(1 row)

transfer_id  status
-----------  ------
          7  failed
        601  done
(2 rows)
SELECT student_id, section_id, grade FROM enrollments WHERE student_id = 1 AND section_id = 30;
UPDATE enrollments SET grade = 90 WHERE student_id = 1 AND section_id = 30;
INSERT INTO enrollments (student_id, section_id, enrolled_on) VALUES (360, 1, '2026-10-01');
SELECT student_id, section_id, grade FROM enrollments WHERE (student_id = 1 AND section_id = 30) OR student_id = 360 ORDER BY student_id;
student_id  section_id  grade
----------  ----------  -----
         1          30   49.2
(1 row)

student_id  section_id  grade
----------  ----------  -----
         1          30   90.0
       360           1   NULL
       360          21   54.3
       360          24   60.5
       360          28   73.0
       360          33   67.7
(6 rows)

Each statement used the primary key, so the database can jump straight to the row (lesson 7.1).

Now analytical work: summarise everything.

SELECT substr(paid_on, 1, 7) AS month, COUNT(*) AS payments, ROUND(SUM(amount), 2) AS revenue
FROM payments GROUP BY month ORDER BY month LIMIT 4;
SELECT o.customer_id, ROUND(SUM(p.amount), 2) AS spent
FROM payments p JOIN orders o ON o.order_id = p.order_id
GROUP BY o.customer_id ORDER BY spent DESC, o.customer_id LIMIT 4;
month    payments  revenue
-------  --------  -------
2024-01        12   977.47
2024-02        16  1675.12
2024-03        24  3105.49
2024-04        23  2379.93
(4 rows)

customer_id    spent
-----------  -------
        134  1571.75
         59  1561.28
        128  1509.55
        111  1490.89
(4 rows)
SELECT substr(created_on, 1, 7) AS month, COUNT(*) AS transfers, SUM(amount_cents) AS cents_moved
FROM transfers WHERE status = 'done' GROUP BY month ORDER BY month LIMIT 4;
SELECT from_account, SUM(amount_cents) AS cents_sent
FROM transfers WHERE status = 'done' GROUP BY from_account ORDER BY cents_sent DESC, from_account LIMIT 4;
month    transfers  cents_moved
-------  ---------  -----------
2021-07          1        13900
2021-11          1        25800
2022-02          1        12000
2022-05          1         7000
(4 rows)

from_account  cents_sent
------------  ----------
         388      104000
         186       77700
         334       75100
         172       72600
(4 rows)
SELECT substr(submitted_on, 1, 7) AS month, COUNT(*) AS submissions, ROUND(AVG(points), 1) AS average_points
FROM submissions WHERE submitted_on IS NOT NULL GROUP BY month ORDER BY month LIMIT 4;
SELECT student_id, ROUND(SUM(points), 1) AS total_points
FROM submissions GROUP BY student_id ORDER BY total_points DESC, student_id LIMIT 4;
month    submissions  average_points
-------  -----------  --------------
2025-02          213            31.8
2025-03          682            33.3
2025-04          674            33.8
2025-05           55            39.7
(4 rows)

student_id  total_points
----------  ------------
        81        1188.1
       235        1091.0
       181        1008.5
       319         953.3
(4 rows)

These queries must read every row of the table. With a few thousand rows this is instant; with 100 million rows it is a heavy job that could slow down the people trying to take orders at the same time.

3. Why one design rarely fits both

Operational (OLTP) Analytical (OLAP)
Typical question one order, one customer totals and trends over months
Rows touched a few millions
Operations many small reads and writes mostly big reads
Users thousands at once (customers, staff) a few analysts and dashboards
Data the current state the history, kept for years
Design normalized (chapter 5), few duplicates often denormalized (lesson 5.7), shaped for summaries
Speed goal milliseconds per request seconds to minutes per report, however large
Priority correctness of every write (chapter 6) convenient, fast summaries

A design that is perfect for one is awkward for the other. Normalization spreads facts across many tables, which keeps writes safe but makes a report join six tables. A report-friendly layout repeats data, which makes writes risky.

4. The data warehouse

Because the two jobs clash, many organisations keep a second database just for analysis, called a data warehouse:

  • It is loaded from the operational databases (and other sources) on a schedule, for example every night (lesson 13.4).
  • It keeps history, even after the operational system has changed or deleted the data.
  • It is shaped for reporting (lessons 13.2 and 13.3).
  • Analysts query it freely without slowing down the shop.
 Shop database ─┐
 Website logs  ─┼──► load ──► Data warehouse ──► reports, dashboards
 Accounting    ─┘

A warehouse is not always needed. A small business with a few thousand orders can run reports straight on the operational database (as we just did) and be fine. A warehouse becomes worth the extra work when reports are slow, when they disturb daily operations, when data from several systems must be combined, or when history must be kept longer than the operational system keeps it.

Worked example: which database should answer this?

A tailor shop (made up) has one operational database. Decide for each request whether it is operational or analytical, and where it should run once the shop has grown.

Request Kind Why
"Show Rina's measurements while she is at the counter." Operational one customer, needs an instant answer
"Mark order 88 as ready." Operational one write that must be correct
"Compare suit sales for each month of the last three years." Analytical reads years of history, produces a small table
"Which fabric colour earns the most per metre?" Analytical joins and sums across many orders

The first two stay on the operational database. The last two should move to a warehouse (or a copy of the data) once they become slow enough to bother the counter staff.

Common mistakes

  • Running heavy reports on the live database at the busiest time. One big summary can slow every customer down.
  • Building a warehouse too early. If reports finish quickly and nobody is disturbed, a separate system is extra work for no gain.
  • Treating the warehouse as a backup. A warehouse is shaped and filtered for analysis; it is not an exact, restorable copy (chapter 8).
  • Thinking OLAP is "just SQL on a big table". The design, storage and loading are different (the next lessons).

Key takeaways

  • OLTP answers many small questions about current data and must write safely; OLAP answers a few huge questions over history.
  • The two jobs pull the design in opposite directions.
  • A data warehouse is a separate database, loaded from operational systems, that keeps history in a report-friendly shape.
  • Use a warehouse when reports are slow, disturb operations, need several sources, or need long history.
  • A small system may do both jobs in one database.

Practice

1. Is "How many orders did Sam place in February?" operational or analytical?

Show answer

Analytical: it reads many rows (all of Sam's orders) and produces a summary number. (A single-order lookup would be operational.)

2. Give two reasons running a huge report on the live shop database can be a problem.

Show answer

It reads a large part of the data, which uses time and memory that customers' small requests also need, and it can hold resources that slow or block other users.

3. Why is a normalized design good for OLTP but awkward for reports?

Show answer

Normalization stores each fact once, so writes are safe and small; but a report then has to join many tables to put the facts back together.

4. What does a data warehouse keep that the operational database may not?

Show answer

Long history (for example data about customers or prices as they were years ago), combined from several systems.

5. A bakery has 800 orders in total. Does it need a data warehouse? Why or why not?

Show answer

Probably not: reports over 800 rows are instant and cannot disturb anyone. A separate system would add work with no benefit.

6. Name two differences between OLTP and OLAP from the table in section 3.

Show answer

For example: OLTP touches a few rows while OLAP touches millions; OLTP has many small writes while OLAP is mostly big reads; OLTP keeps the current state while OLAP keeps history.