सामग्री पर जाएँ

लेसन 2 · Getting Started

OLTP vs OLAP: Two Kinds of Database Work

Compare OLTP and OLAP workloads: transactions against analysis, normalized against dimensional design, row against column storage, with runnable SQL.

Databases are used for two very different kinds of work. One kind runs the business: taking an order, updating a stock count, changing an address. This is OLTP, online transaction processing. The other kind studies the business: totals, trends and comparisons across large amounts of data. This is OLAP, online analytical processing.

Knowing the difference explains most design choices in a data warehouse.

What each workload looks like

An OLTP system serves many users who each touch a few rows. "Show order 5812" or "Add an item to this basket" must finish quickly, and the data must stay correct even when thousands of people act at once. This is why OLTP systems care so much about transactions: a payment and the matching stock change must both happen or neither.

An OLAP system serves fewer, heavier questions. "Revenue by product category and month for three years" reads millions of rows but only a few columns, and returns a small summary.

Aspect OLTP OLAP (warehouse)
Typical question What is in order 5812? Which categories grew this quarter?
Rows per query Few Very many
Writes Constant small inserts and updates Mostly scheduled or streaming loads
History Often only the current state Kept deliberately
Design Normalized Often dimensional (star schema)
Common storage Row-oriented Column-oriented
Success means Correct, quick transactions Correct, understandable, fast analysis

These are tendencies, not laws. Some databases are built to handle both, and a small company can report directly from its OLTP database for a long time.

Why not report from the OLTP database?

Three problems usually appear as data grows:

  1. Load. A long report competes with customers for memory, disk and CPU.
  2. History. If a customer moves, the application updates the address. Last year's sales now appear to come from the new city.
  3. Complexity. Normalized schemas can need many joins, and each report writer may join them differently.

A read replica solves the first problem, but not the other two. A warehouse is designed to address all three.

Normalized against dimensional

OLTP design uses normalization: each fact is stored once, in the table where it belongs. This protects against update mistakes. (See normalization in the SQL tutorial.)

Analytical design often denormalizes on purpose, so that a query needs fewer joins. In the example below, the same sales are stored both ways.

SQL The same question, two designs

Press Run (or Ctrl + Enter) to see the result here.

The report on sales_wide needs no joins. The price is repetition: the word "Footwear" appears on many rows, so renaming a category means rebuilding or updating many rows. In a warehouse that cost is acceptable because a controlled process writes the data. Later you will use a star schema, which sits between these two extremes.

Row storage and column storage

A row store keeps all the values of one row together. That suits "fetch this whole order". A column store keeps the values of each column together. A query that sums revenue can then skip the other columns, and similar values stored side by side tend to compress well.

Row store

Whole rows together Great for single records
Cheap small updates
1 / 2

Many analytical platforms use columnar storage. SQLite is a row store, so the runnable blocks cannot show the speed difference.

Getting data from one to the other

A pipeline copies changes from OLTP sources into the warehouse. It may run once a night, every few minutes or continuously. The gap between a change in the source and its appearance in the warehouse is called latency or data freshness. Agree on how fresh each dataset must be before you build, because fresher data usually costs more to run.

For another angle on the same split, see OLTP vs OLAP for data engineers.

Follow one change through both systems

Suppose a customer at Harbor Outfitters changes their address on 3 May.

In the OLTP system, the application runs one short update. The old address is gone, unless the application was designed to keep it. Shipping now uses the new address, which is exactly what the application needs.

In the warehouse, the next load notices the change. Your design then decides what happens. You might overwrite the address, because only the current one matters. Or you might add a new version of the customer and keep the old one, so that April's sales still count toward the old region. Neither is automatically right. It depends on the questions people ask, and later lessons show both.

Clues that a query is analytical

When you are unsure where a job belongs, ask these questions:

Question Points toward OLTP Points toward OLAP
Does a user wait for it in order to finish a task, such as paying? Yes No
Does it look up records by their ID? Often Rarely
Does it scan months or years of data? Rarely Often
Must it see a change made one second ago? Usually Usually not
Does it combine several source systems? Rarely Often

A screen that shows a customer their own last five orders is operational, even though it reads history. A weekly report of repeat-purchase rates is analytical, even if the data set is small. The purpose matters more than the size.

Hybrid systems

Some database products aim to serve both workloads, sometimes called HTAP (hybrid transactional and analytical processing). Others add column-oriented indexes or analytical replicas next to a row store. These can reduce the number of moving parts. They do not remove the need to decide how history is kept, how sources are combined and what each measure means. Evaluate such features with your own workload, and read the product's official documentation about their limits.

Common mistakes

Do not assume the warehouse is "the real-time system". If a dashboard shows yesterday's data, say so on the dashboard. Also do not let applications use the warehouse as their transaction database: it is usually not designed for many tiny concurrent writes.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which task is the best example of OLTP?

    It is a small, immediate transaction that must be correct.
  2. 2. Why can a read replica alone not replace a warehouse?

    A replica moves the load elsewhere but does not add history or combine sources.
  3. 3. Why does column-oriented storage help many analytical queries?

    Analytics usually reads few columns of many rows, which columnar layouts suit.
  4. 4. What is the usual cost of denormalizing?

    Fewer joins come with repeated values that the loading process has to maintain.

Summary

  • OLTP runs daily operations with many small, safe transactions.
  • OLAP studies large amounts of data and returns summaries.
  • Reporting straight from OLTP creates load, history and complexity problems.
  • Analytical designs trade some repetition for simpler, faster queries.
  • Column storage suits wide scans of a few columns.

Next: Warehouse Layers: Staging, Core and Marts

आपकी प्रगति इसी ब्राउज़र में सेव होती है। खाते की ज़रूरत नहीं है।